diff --git a/yadc/bp/main.py b/yadc/bp/main.py index 3b454cd..ce3c3ee 100644 --- a/yadc/bp/main.py +++ b/yadc/bp/main.py @@ -1,5 +1,16 @@ -from flask import Blueprint, render_template, flash, redirect, url_for, request, abort -from flask_login import login_required +import io +import os + +from flask import (Blueprint, abort, current_app, flash, redirect, + render_template, request, send_from_directory, url_for) +from flask_login import current_user, login_required +from PIL import Image +from sqlalchemy import func +from sqlalchemy.orm import aliased + +from yadc import db +from yadc.forms import UploadPostForm +from yadc.models import FILETYPE, RATING, Post, Tag, post_tags bp = Blueprint('main', __name__) @@ -26,17 +37,12 @@ def posts(): tags = list(tagset) tags.sort(key=lambda x: (x.category.value, x.content) ) # flash([tag.count for tag in tags]) - 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).first() - flash(post) + # 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() @@ -46,17 +52,8 @@ def post_show(id): tag.count = tag_count[1] tags.append(tag) - return render_template('post.html', post=post, tags=post.tags) - -from flask_login import current_user -from yadc import db -from yadc.forms import UploadPostForm -from yadc.models import Post, RATING, FILETYPE + return render_template('post.html', post=post, tags=tags) -from flask import current_app -from PIL import Image -import io -import os @bp.route('/post/upload/', methods=['GET', 'POST']) @login_required @@ -95,7 +92,6 @@ def post_upload(): return render_template('upload.html', form=form) -from flask import send_from_directory @bp.route('/i/') def uploaded_img(path, store='img', exten=None): @@ -128,4 +124,4 @@ def user_profile(username): @bp.route('/user/settings/') @login_required def user_settings(): - pass \ No newline at end of file + pass diff --git a/yadc/models.py b/yadc/models.py index 27ba413..f9886a3 100644 --- a/yadc/models.py +++ b/yadc/models.py @@ -1,10 +1,16 @@ import enum +import hashlib +import os from datetime import datetime -from sqlalchemy_utc import UtcDateTime, utcnow -# from sqlalchemy.sql.functions import now as current_timestamp -from yadc import db, login +from flask import current_app, url_for +from flask_login import UserMixin, login_user, logout_user +from PIL import Image +from sqlalchemy_utc import UtcDateTime, utcnow +from werkzeug.security import check_password_hash, generate_password_hash +from werkzeug.utils import cached_property +from yadc import db, login, utils class OP_LEVEL(enum.Enum): user = 0 @@ -42,9 +48,6 @@ class TimestampMixin(object): created = db.Column(UtcDateTime, nullable=False, default=utcnow()) updated = db.Column(UtcDateTime, nullable=False, onupdate=utcnow(), default=utcnow()) -from flask_login import UserMixin, login_user, logout_user -from werkzeug.security import check_password_hash, generate_password_hash - class User(UserMixin, TimestampMixin, db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(128), unique=True, nullable=False) @@ -89,13 +92,6 @@ post_tags = db.Table('post_tags', db.metadata, db.Column('post_id', db.Integer, db.ForeignKey('post.id')), db.Column('tag_id', db.Integer, db.ForeignKey('tag.id')) ) - -from PIL import Image -import hashlib -import os - -from werkzeug.utils import cached_property -from flask import current_app, url_for class Post(TimestampMixin, db.Model): id = db.Column(db.Integer, primary_key=True) @@ -170,18 +166,18 @@ class Post(TimestampMixin, db.Model): def thumb_path(self): jpeg_filename = "{}.{}".format(self.md5, 'jpg') return os.path.join(current_app.instance_path, 'post', 'thumb', jpeg_filename) + + @cached_property + def image_resolution(self): + return "{}x{}".format(self.width, self.height) @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) + return utils.sizeof_fmt(self.filesize) + + @property + def is_approved(self): + return self.status is POST_STATUS.active class Tag(TimestampMixin, db.Model): diff --git a/yadc/templates/post.html b/yadc/templates/post.html index 4642e40..58373ea 100644 --- a/yadc/templates/post.html +++ b/yadc/templates/post.html @@ -8,9 +8,16 @@
Id: {{ post.id }}
Author: {{ post.author.username }}
+ {% if post.is_approved %} +
Approved by: {{ post.approver.username }}
+ {% endif %} + {% if not post.is_approved %} +
Status: {{ post.status.name.capitalize() }}
+ {% endif %}
Posted: 10 hours ago
File size: {{ post.filesize_human }}
+
Image res: {{ post.image_resolution }}
{% if post.filetype.name == 'png' %} diff --git a/yadc/utils.py b/yadc/utils.py new file mode 100644 index 0000000..a63f5bb --- /dev/null +++ b/yadc/utils.py @@ -0,0 +1,8 @@ + +#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) \ No newline at end of file