from secrets import token_urlsafe from pathlib import Path from quart import Blueprint, render_template, request, redirect, url_for, flash, send_from_directory from flask_login import login_required, current_user from werkzeug.utils import secure_filename from flipbook.models import Upload from flipbook.helpers import allowed_file from flipbook import config bp = Blueprint('manage', 'manage', url_prefix='/manage') @bp.route('') @login_required async def index(): uploads = Upload.select().where( Upload.wallet == current_user ) return await render_template( 'manage.html', uploads=uploads ) @bp.route('/upload', methods=['GET', 'POST']) @login_required async def upload(): if request.method == 'POST': data = await request.form file = list((await request.files).items())[0][1] if file.filename == '': await flash('Must upload an image.', 'is-danger') return redirect(request.url) if data['title'] == '': await flash('Must set a title', 'is-danger') return redirect(request.url) if file and allowed_file(file.filename): filename = '{}-{}'.format( token_urlsafe(12), secure_filename(file.filename) ) await file.save(Path(config.UPLOADS_PATH, filename)) upload = Upload( token_id=0, title=data['title'], image_name=filename, text=data['text'], wallet=current_user ) upload.save() upload.save_thumbnail() await flash('Uploaded successfully!', 'is-success') return redirect(url_for('manage.index')) return render_template('upload.html')