main
lza_menace 2 years ago
parent 328f710734
commit b215843185

@ -9,6 +9,7 @@ help:
setup: ## Establish local environment with dependencies installed setup: ## Establish local environment with dependencies installed
python3 -m venv .venv python3 -m venv .venv
.venv/bin/pip install -r requirements.txt .venv/bin/pip install -r requirements.txt
mkdir -p data/uploads
shell: shell:
FLASK_SECRETS=config.py QUART_APP="flipbook:create_app()" .venv/bin/quart shell FLASK_SECRETS=config.py QUART_APP="flipbook:create_app()" .venv/bin/quart shell

@ -1,5 +1,7 @@
from datetime import datetime from datetime import datetime
from uuid import uuid4 from uuid import uuid4
from pathlib import Path
from os.path import splitext
from peewee import * from peewee import *
from PIL import Image from PIL import Image
@ -61,8 +63,24 @@ class Upload(Model):
title = CharField() title = CharField()
text = CharField(null=True) text = CharField(null=True)
wallet = ForeignKeyField(Wallet) wallet = ForeignKeyField(Wallet)
image_name = CharField() image_name = CharField(null=False)
upload_date = DateTimeField(default=datetime.utcnow) upload_date = DateTimeField(default=datetime.utcnow)
def get_thumbnail_name(self):
s = splitext(self.image_name)
return s[0] + '.thumbnail' + s[1]
def save_thumbnail(self):
try:
image = Image.open(Path(config.UPLOADS_PATH, self.image_name))
image.thumbnail((200,200), Image.ANTIALIAS)
image.save(Path(config.UPLOADS_PATH, self.get_thumbnail_name()), format=image.format, quality=90)
image.close()
return True
except Exception as e:
print(e)
return False
class Meta: class Meta:
database = db database = db

@ -15,40 +15,41 @@ bp = Blueprint('manage', 'manage', url_prefix='/manage')
@bp.route('') @bp.route('')
@login_required @login_required
async def index(): async def index():
return await render_template('manage.html') uploads = Upload.select().where(
Upload.wallet == current_user
)
return await render_template(
'manage.html',
uploads=uploads
)
@bp.route('/upload', methods=['GET', 'POST']) @bp.route('/upload', methods=['GET', 'POST'])
@login_required @login_required
async def upload(): async def upload():
if request.method == 'POST': if request.method == 'POST':
data = await request.form data = await request.form
file = await request.files file = list((await request.files).items())[0][1]
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 == '': if file.filename == '':
await flash('You didn\'t upload a caliente meme, bro! You\'re fuckin up!', 'is-danger') await flash('Must upload an image.', 'is-danger')
return await redirect(request.url) return redirect(request.url)
if post_title == '': if data['title'] == '':
await flash('You didn\'t give your meme a spicy title, bro! You\'re fuckin up!', 'is-danger') await flash('Must set a title', 'is-danger')
return await redirect(request.url) return redirect(request.url)
if file and allowed_file(file.filename): if file and allowed_file(file.filename):
filename = '{}-{}'.format( filename = '{}-{}'.format(
token_urlsafe(12), token_urlsafe(12),
secure_filename(file.filename) secure_filename(file.filename)
) )
file.save(Path(config.UPLOADS_PATH, filename)) await file.save(Path(config.UPLOADS_PATH, filename))
upload = Upload( upload = Upload(
token_id=0, token_id=0,
title=post_title, title=data['title'],
image_name=filename, image_name=filename,
text=data['text'], text=data['text'],
wallet=current_user, wallet=current_user
) )
upload.save() upload.save()
# upload.save_thumbnail() upload.save_thumbnail()
await flash('Uploaded successfully!', 'is-success') await flash('Uploaded successfully!', 'is-success')
return redirect(url_for('manage.index')) return redirect(url_for('manage.index'))
return render_template('upload.html') return render_template('upload.html')

@ -1,4 +1,6 @@
{% extends 'base.html' %} {% extends 'base.html' %}
{% block content %} {% block content %}
<p>manage</p> {% for upload in uploads %}
<p>{{ upload }}</p>
{% endfor %}
{% endblock %} {% endblock %}

@ -15,7 +15,7 @@
</div> </div>
<div class="field"> <div class="field">
<label class="label">Upload</label> <label class="label">Upload</label>
<input type="file" name="file" multiple> <input type="file" name="file">
</div> </div>
<div class="field is-grouped"> <div class="field is-grouped">
<div class="control"> <div class="control">

Loading…
Cancel
Save