1
1
Fork 0

A bit of refactoring

dev
Jan Kužílek 5 years ago
parent 02ec6964f5
commit 221e047d78

@ -1,5 +1,16 @@
from flask import Blueprint, render_template, flash, redirect, url_for, request, abort import io
from flask_login import login_required 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__) bp = Blueprint('main', __name__)
@ -26,17 +37,12 @@ def posts():
tags = list(tagset) tags = list(tagset)
tags.sort(key=lambda x: (x.category.value, x.content) ) tags.sort(key=lambda x: (x.category.value, x.content) )
# flash([tag.count for tag in tags]) # flash([tag.count for tag in tags])
return render_template('index.html', posts=posts, tags=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/<id>/') @bp.route('/post/show/<id>/')
def post_show(id): def post_show(id):
post = Post.query.filter_by(id=id).first() 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() 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] tag.count = tag_count[1]
tags.append(tag) tags.append(tag)
return render_template('post.html', post=post, tags=post.tags) return render_template('post.html', post=post, tags=tags)
from flask_login import current_user
from yadc import db
from yadc.forms import UploadPostForm
from yadc.models import Post, RATING, FILETYPE
from flask import current_app
from PIL import Image
import io
import os
@bp.route('/post/upload/', methods=['GET', 'POST']) @bp.route('/post/upload/', methods=['GET', 'POST'])
@login_required @login_required
@ -95,7 +92,6 @@ def post_upload():
return render_template('upload.html', form=form) return render_template('upload.html', form=form)
from flask import send_from_directory
@bp.route('/i/<path:path>') @bp.route('/i/<path:path>')
def uploaded_img(path, store='img', exten=None): def uploaded_img(path, store='img', exten=None):

@ -1,10 +1,16 @@
import enum import enum
import hashlib
import os
from datetime import datetime 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): class OP_LEVEL(enum.Enum):
user = 0 user = 0
@ -42,9 +48,6 @@ class TimestampMixin(object):
created = db.Column(UtcDateTime, nullable=False, default=utcnow()) created = db.Column(UtcDateTime, nullable=False, default=utcnow())
updated = db.Column(UtcDateTime, nullable=False, onupdate=utcnow(), 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): class User(UserMixin, TimestampMixin, db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(128), unique=True, nullable=False) username = db.Column(db.String(128), unique=True, nullable=False)
@ -90,13 +93,6 @@ post_tags = db.Table('post_tags', db.metadata,
db.Column('tag_id', db.Integer, db.ForeignKey('tag.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): class Post(TimestampMixin, db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
md5 = db.Column(db.String(32), unique=True, nullable=False) md5 = db.Column(db.String(32), unique=True, nullable=False)
@ -171,17 +167,17 @@ class Post(TimestampMixin, db.Model):
jpeg_filename = "{}.{}".format(self.md5, 'jpg') jpeg_filename = "{}.{}".format(self.md5, 'jpg')
return os.path.join(current_app.instance_path, 'post', 'thumb', jpeg_filename) 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 @cached_property
def filesize_human(self): def filesize_human(self):
#https://stackoverflow.com/questions/1094841/reusable-library-to-get-human-readable-version-of-file-size return utils.sizeof_fmt(self.filesize)
def sizeof_fmt(num, suffix='B'):
for unit in ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']: @property
if abs(num) < 1024.0: def is_approved(self):
return "%3.1f%s%s" % (num, unit, suffix) return self.status is POST_STATUS.active
num /= 1024.0
return "%.1f%s%s" % (num, 'Yi', suffix)
return sizeof_fmt(self.filesize)
class Tag(TimestampMixin, db.Model): class Tag(TimestampMixin, db.Model):

@ -8,9 +8,16 @@
<!-- <img src="/static/profile.jpg" alt=""> --> <!-- <img src="/static/profile.jpg" alt=""> -->
<div class="id">Id: {{ post.id }}</div> <div class="id">Id: {{ post.id }}</div>
<div class="author">Author: {{ post.author.username }}</div> <div class="author">Author: {{ post.author.username }}</div>
{% if post.is_approved %}
<div class="approver">Approved by: {{ post.approver.username }}</div>
{% endif %}
{% if not post.is_approved %}
<div class="status">Status: {{ post.status.name.capitalize() }}</div>
{% endif %}
<div class="time">Posted: 10 hours ago</div> <div class="time">Posted: 10 hours ago</div>
<div class="source">Source: <a href="{{ post.source }}">{{ post.source }}</a></div> <div class="source">Source: <a href="{{ post.source }}">{{ post.source }}</a></div>
<div class="size">File size: {{ post.filesize_human }}</div> <div class="size">File size: {{ post.filesize_human }}</div>
<div class="resolution">Image res: {{ post.image_resolution }}</div>
<div class="jpeg"><a href="{{ post.url(path=post.image_url, endpoint='jpeg') }}">Enlarge image</a></div> <div class="jpeg"><a href="{{ post.url(path=post.image_url, endpoint='jpeg') }}">Enlarge image</a></div>
{% if post.filetype.name == 'png' %} {% if post.filetype.name == 'png' %}
<div class="png"><a href="{{ post.url(path=post.image_url) }}">Original PNG file</a></div> <div class="png"><a href="{{ post.url(path=post.image_url) }}">Original PNG file</a></div>

@ -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)
Loading…
Cancel
Save