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
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/<id>/')
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/<path:path>')
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
pass

@ -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):

@ -8,9 +8,16 @@
<!-- <img src="/static/profile.jpg" alt=""> -->
<div class="id">Id: {{ post.id }}</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="source">Source: <a href="{{ post.source }}">{{ post.source }}</a></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>
{% if post.filetype.name == 'png' %}
<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