main
lza_menace 1 year ago
parent 328f710734
commit b215843185

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

@ -1,5 +1,7 @@
from datetime import datetime
from uuid import uuid4
from pathlib import Path
from os.path import splitext
from peewee import *
from PIL import Image
@ -61,8 +63,24 @@ class Upload(Model):
title = CharField()
text = CharField(null=True)
wallet = ForeignKeyField(Wallet)
image_name = CharField()
image_name = CharField(null=False)
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:
database = db

@ -15,40 +15,41 @@ bp = Blueprint('manage', 'manage', url_prefix='/manage')
@bp.route('')
@login_required
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'])
@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']
file = list((await request.files).items())[0][1]
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)
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(
filename = '{}-{}'.format(
token_urlsafe(12),
secure_filename(file.filename)
)
file.save(Path(config.UPLOADS_PATH, filename))
await file.save(Path(config.UPLOADS_PATH, filename))
upload = Upload(
token_id=0,
title=post_title,
title=data['title'],
image_name=filename,
text=data['text'],
wallet=current_user,
wallet=current_user
)
upload.save()
# upload.save_thumbnail()
upload.save_thumbnail()
await flash('Uploaded successfully!', 'is-success')
return redirect(url_for('manage.index'))
return render_template('upload.html')

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

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

Loading…
Cancel
Save