1
1
Fork 0

Zvelebená post stránka

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

@ -26,6 +26,7 @@ def create_app():
MAX_CONTENT_LENGTH=10*1024*1024, MAX_CONTENT_LENGTH=10*1024*1024,
POST_LIST_THUMB_HEIGHT=200, POST_LIST_THUMB_HEIGHT=200,
POST_UPLOADS=os.path.join(app.instance_path, 'post'), POST_UPLOADS=os.path.join(app.instance_path, 'post'),
INSTANCE_NAME='YADC',
SQLALCHEMY_ECHO=True, SQLALCHEMY_ECHO=True,
) )

@ -29,14 +29,24 @@ def posts():
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) 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 = 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 flask_login import current_user
from yadc import db from yadc import db
@ -48,8 +58,6 @@ from PIL import Image
import io import io
import os import os
# @bp.route('/post/new/')
# @bp.route('/post/create/')
@bp.route('/post/upload/', methods=['GET', 'POST']) @bp.route('/post/upload/', methods=['GET', 'POST'])
@login_required @login_required
def post_upload(): def post_upload():

@ -141,7 +141,8 @@ class Post(TimestampMixin, db.Model):
@property @property
def image_url(self): def image_url(self):
# filename = "{}.{}".format('maybe_later_generated_cute_filename', 'jpg' if self.filetype is FILETYPE.jpeg else 'png') # 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) return os.path.join(self.md5, filename)
def url(self, path, endpoint='img'): def url(self, path, endpoint='img'):
@ -170,6 +171,18 @@ 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 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): class Tag(TimestampMixin, db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)

@ -6,11 +6,16 @@
<article class="post_desc"> <article class="post_desc">
<!-- <h4>Description</h4> --> <!-- <h4>Description</h4> -->
<!-- <img src="/static/profile.jpg" alt=""> --> <!-- <img src="/static/profile.jpg" alt=""> -->
<div class="id">Id: 51432</div> <div class="id">Id: {{ post.id }}</div>
<div class="author">Author: Kuxa</div> <div class="author">Author: {{ post.author.username }}</div>
<div class="time">Posted: 10 hours ago</div> <div class="time">Posted: 10 hours ago</div>
<div class="source">Source: <a href="https://www.pixiv.net/en/artworks/78136345">https://www.pixiv.net/en/artworks/78136345</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="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>
{% endif %}
</article> </article>
<article class="tags"> <article class="tags">
<h3>Tags</h3> <h3>Tags</h3>
@ -19,7 +24,7 @@
<a href="#"> <a href="#">
<span class="fa fa-tag"></span> <span class="fa fa-tag"></span>
<span class="name">{{ tag.content }}</span> <span class="name">{{ tag.content }}</span>
<span class="count">10</span> <span class="count">{{ tag.count }}</span>
</a> </a>
{% endfor %} {% endfor %}
</div> </div>
@ -28,7 +33,7 @@
<section class="post_single"> <section class="post_single">
<div class="image"> <div class="image">
<!-- <img src="/static/pixiv/illust_72206228_20191026_131238.jpg" alt=""> --> <!-- <img src="/static/pixiv/illust_72206228_20191026_131238.jpg" alt=""> -->
<img src="/static/ryuzu/916500.jpg" alt=""> <img src="{{ post.url(path=post.image_url, endpoint='jpeg') }}" alt="">
</div> </div>
</section> </section>
</div> </div>

Loading…
Cancel
Save