use thumbnails

master
lza_menace 2 years ago
parent b5c7cbcd58
commit 44621fbbdb

@ -54,14 +54,15 @@ def cli(app):
with open(fp, 'wb') as download: with open(fp, 'wb') as download:
download.write(content) download.write(content)
click.echo(f'[+] Downloaded {art}') click.echo(f'[+] Downloaded {art}')
if not Artwork.select().where(Artwork.path == bn).first(): if not Artwork.select().where(Artwork.image == bn).first():
artwork = Artwork( artwork = Artwork(
creator=_user, creator=_user,
path=bn, image=bn,
approved=True, approved=True,
title=f'i made {bn}', title=f'i made {bn}',
description='' description=''
) )
artwork.save() artwork.save()
artwork.generate_thumbnail()
click.echo(f'[+] Created artwork {artwork.id} for {bn}') click.echo(f'[+] Created artwork {artwork.id} for {bn}')
return app return app

@ -16,4 +16,4 @@ def make_wallet_rpc(method, data={}):
res = w._backend.raw_request(method, data) res = w._backend.raw_request(method, data)
return res return res
except Exception as e: except Exception as e:
raise e raise e

@ -1,7 +1,8 @@
from os import path
from datetime import datetime from datetime import datetime
from secrets import token_urlsafe from secrets import token_urlsafe
from flask import url_for from PIL import Image
import peewee as pw import peewee as pw
@ -80,16 +81,31 @@ class Artwork(pw.Model):
""" """
id = pw.AutoField() id = pw.AutoField()
creator = pw.ForeignKeyField(User) creator = pw.ForeignKeyField(User)
path = pw.CharField() image = pw.CharField()
thumbnail = pw.CharField(null=True)
upload_date = pw.DateTimeField(default=datetime.utcnow) upload_date = pw.DateTimeField(default=datetime.utcnow)
last_edit_date = pw.DateTimeField(default=datetime.utcnow) last_edit_date = pw.DateTimeField(default=datetime.utcnow)
approved = pw.BooleanField(default=False) approved = pw.BooleanField(default=False)
hidden = pw.BooleanField(default=False) hidden = pw.BooleanField(default=False)
title = pw.CharField() title = pw.CharField()
description = pw.TextField(null=True) description = pw.TextField(null=True)
def get_image_url(self): def generate_thumbnail(self):
return url_for('main.uploaded_file', filename=self.path) _t = f'thumbnail-{self.image}'
i = f'{config.DATA_PATH}/uploads/{self.image}'
t = f'{config.DATA_PATH}/uploads/{_t}'
if path.exists(t):
return True
try:
image = Image.open(i)
image.thumbnail((150,150), Image.ANTIALIAS)
image.save(t, format=image.format, quality=75)
image.close()
self.thumbnail = _t
self.save()
return True
except:
return False
class Meta: class Meta:
database = db database = db

@ -1,7 +1,7 @@
<head> <head>
<title>{{ config.SITE_NAME }}</title> <title>{{ config.SITE_NAME }}</title>
<meta charset="utf-8" /> <meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" /> <meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="shortcut icon" href="/static/images/favicon.ico" type="image/png"> <link rel="shortcut icon" href="/static/images/favicon.ico" type="image/png">
<meta itemprop="name" content="MyThing app"> <meta itemprop="name" content="MyThing app">
<meta itemprop="description" content="This is a sample application making using of Flask, a Flask replacement for Python"> <meta itemprop="description" content="This is a sample application making using of Flask, a Flask replacement for Python">

@ -7,7 +7,7 @@
<h2>Latest Artworks</h2> <h2>Latest Artworks</h2>
{% for artwork in feed['artwork'] %} {% for artwork in feed['artwork'] %}
<p>{{ artwork.title }}</p> <p>{{ artwork.title }}</p>
<img src="{{ artwork.get_image_url() }}" width="80px"> <img src="{{ url_for('main.uploaded_file', filename=artwork.thumbnail) }}" width="120px">
{% endfor %} {% endfor %}
</div> </div>
<div class="row"> <div class="row">

Loading…
Cancel
Save