add support for thumbnails

graphs-n-shit
lza_menace 4 years ago
parent f5508a012d
commit bcc7601d89

@ -1,5 +1,7 @@
from peewee import *
from os import path
from datetime import datetime
from PIL import Image
from suchwow import config
@ -21,6 +23,28 @@ class Post(Model):
to_discord = BooleanField(default=False)
approved = BooleanField(default=False)
def get_image_path(self, thumbnail=False):
save_path_base = path.join(config.DATA_FOLDER, "uploads")
if thumbnail:
save_path = path.join(save_path_base, self.get_thumbnail_name())
else:
save_path = path.join(save_path_base, self.image_name)
return save_path
def save_thumbnail(self):
try:
image = Image.open(self.get_image_path())
image.thumbnail((200,200), Image.ANTIALIAS)
image.save(self.get_image_path(True), format=image.format, quality=90)
image.close()
return True
except:
return False
def get_thumbnail_name(self):
s = path.splitext(self.image_name)
return s[0] + '.thumbnail' + s[1]
def show(self):
return {
'id': self.id,
@ -28,6 +52,9 @@ class Post(Model):
'text': self.text,
'submitter': self.submitter,
'image_name': self.image_name,
'image_path': self.get_image_path(),
'thumbnail_name': self.get_thumbnail_name(),
'thumbnail_path': self.get_image_path(True),
'readonly': self.readonly,
'hidden': self.hidden,
'account_index': self.account_index,

@ -109,6 +109,7 @@ def create():
address_index=0
)
post.save()
post.save_thumbnail()
url = url_for('post.read', id=post.id, _external=True)
post_webhook(f"New post :doge2: [{post.id}]({url}) by `{submitter}` :neckbeard:")
return redirect(url)

@ -14,6 +14,7 @@
<th>Date</th>
<th>ID</th>
<th>Title</th>
<th>Thumbnail</th>
<th>Submitter</th>
</tr>
{% for post in posts %}
@ -21,6 +22,11 @@
<td>{{ post.timestamp.strftime('%Y-%m-%d %H:%M') }}</td>
<td>{{ post.id }}</td>
<td><a href="{{ url_for('post.read', id=post.id) }}">{{ post.title }}</a></td>
<td>
<a href="{{ url_for('post.read', id=post.id) }}">
<img src="{{ url_for('post.uploaded_file', filename=post.get_thumbnail_name()) }}" width=200 style="max-height:200px;">
</a>
</td>
<td><a href="/?submitter={{ post.submitter }}">{{ post.submitter }}</a></td>
</tr>
{% endfor %}

Loading…
Cancel
Save