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(): return await render_template('manage.html') @bp.route('/upload', methods=['GET', 'POST']) @login_required async def upload(): if request.method == 'POST': data = await request.form file = await request.files post_title = data['title'] if 'file' not in request.files: await flash('You didn\'t upload a caliente meme, bro! You\'re fuckin up!', 'is-danger') return await redirect(request.url) file = request.files['file'] if file.filename == '': await flash('You didn\'t upload a caliente meme, bro! You\'re fuckin up!', 'is-danger') return await redirect(request.url) if post_title == '': await flash('You didn\'t give your meme a spicy title, bro! You\'re fuckin up!', 'is-danger') return await redirect(request.url) if file and allowed_file(file.filename): filename = '{}-{}'.format( token_urlsafe(12), secure_filename(file.filename) ) file.save(Path(config.UPLOADS_PATH, filename)) upload = Upload( token_id=0, title=post_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')