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:
download.write(content)
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(
creator=_user,
path=bn,
image=bn,
approved=True,
title=f'i made {bn}',
description=''
)
artwork.save()
artwork.generate_thumbnail()
click.echo(f'[+] Created artwork {artwork.id} for {bn}')
return app

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

@ -1,7 +1,8 @@
from os import path
from datetime import datetime
from secrets import token_urlsafe
from flask import url_for
from PIL import Image
import peewee as pw
@ -80,16 +81,31 @@ class Artwork(pw.Model):
"""
id = pw.AutoField()
creator = pw.ForeignKeyField(User)
path = pw.CharField()
image = pw.CharField()
thumbnail = pw.CharField(null=True)
upload_date = pw.DateTimeField(default=datetime.utcnow)
last_edit_date = pw.DateTimeField(default=datetime.utcnow)
approved = pw.BooleanField(default=False)
hidden = pw.BooleanField(default=False)
title = pw.CharField()
description = pw.TextField(null=True)
def get_image_url(self):
return url_for('main.uploaded_file', filename=self.path)
def generate_thumbnail(self):
_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:
database = db

@ -1,7 +1,7 @@
<head>
<title>{{ config.SITE_NAME }}</title>
<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">
<meta itemprop="name" content="MyThing app">
<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>
{% for artwork in feed['artwork'] %}
<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 %}
</div>
<div class="row">

Loading…
Cancel
Save