diff --git a/yadc/__init__.py b/yadc/__init__.py index 310b53e..4f6af4a 100644 --- a/yadc/__init__.py +++ b/yadc/__init__.py @@ -26,6 +26,7 @@ def create_app(): MAX_CONTENT_LENGTH=10*1024*1024, POST_LIST_THUMB_HEIGHT=200, POST_UPLOADS=os.path.join(app.instance_path, 'post'), + INSTANCE_NAME='YADC', SQLALCHEMY_ECHO=True, ) diff --git a/yadc/bp/main.py b/yadc/bp/main.py index eb2d401..3b454cd 100644 --- a/yadc/bp/main.py +++ b/yadc/bp/main.py @@ -29,14 +29,24 @@ def posts(): return render_template('index.html', posts=posts, tags=tags) +from sqlalchemy import func +from sqlalchemy.orm import aliased +from yadc.models import Tag, post_tags + @bp.route('/post/show//') def post_show(id): - post = Post.query.filter_by(id=id) + post = Post.query.filter_by(id=id).first() flash(post) + tags_count = db.session.query(Tag, func.count(Post.id)).join(Tag.posts).filter(Post.id==id).join(aliased(Post), Tag.posts).group_by(Tag).all() + tags = list() + for tag_count in tags_count: + tag = tag_count[0] + tag.count = tag_count[1] + tags.append(tag) - return render_template('post.html', post=post, tags=tags) + return render_template('post.html', post=post, tags=post.tags) from flask_login import current_user from yadc import db @@ -48,8 +58,6 @@ from PIL import Image import io import os -# @bp.route('/post/new/') -# @bp.route('/post/create/') @bp.route('/post/upload/', methods=['GET', 'POST']) @login_required def post_upload(): diff --git a/yadc/models.py b/yadc/models.py index cd3d067..27ba413 100644 --- a/yadc/models.py +++ b/yadc/models.py @@ -141,7 +141,8 @@ class Post(TimestampMixin, db.Model): @property def image_url(self): # filename = "{}.{}".format('maybe_later_generated_cute_filename', 'jpg' if self.filetype is FILETYPE.jpeg else 'png') - filename = 'maybe_later_generated_cute_filename' + # filename = 'maybe_later_generated_cute_filename' + filename = "{} - {} {}".format(current_app.config.get('INSTANCE_NAME'), self.id, " ".join(tag.content for tag in self.tags)) return os.path.join(self.md5, filename) def url(self, path, endpoint='img'): @@ -170,6 +171,18 @@ class Post(TimestampMixin, db.Model): jpeg_filename = "{}.{}".format(self.md5, 'jpg') return os.path.join(current_app.instance_path, 'post', 'thumb', jpeg_filename) + @cached_property + def filesize_human(self): + #https://stackoverflow.com/questions/1094841/reusable-library-to-get-human-readable-version-of-file-size + def sizeof_fmt(num, suffix='B'): + for unit in ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']: + if abs(num) < 1024.0: + return "%3.1f%s%s" % (num, unit, suffix) + num /= 1024.0 + return "%.1f%s%s" % (num, 'Yi', suffix) + + return sizeof_fmt(self.filesize) + class Tag(TimestampMixin, db.Model): id = db.Column(db.Integer, primary_key=True) diff --git a/yadc/templates/post.html b/yadc/templates/post.html index 664276b..4642e40 100644 --- a/yadc/templates/post.html +++ b/yadc/templates/post.html @@ -6,11 +6,16 @@
-
Id: 51432
-
Author: Kuxa
+
Id: {{ post.id }}
+
Author: {{ post.author.username }}
Posted: 10 hours ago
- - + +
File size: {{ post.filesize_human }}
+ + {% if post.filetype.name == 'png' %} + + {% endif %} +

Tags

@@ -19,7 +24,7 @@ {{ tag.content }} - 10 + {{ tag.count }} {% endfor %} @@ -28,7 +33,7 @@
- +