1
1
Fork 0

Index filtering and tag links

Yeah, still a mess, I guess
dev
Jan Kužílek 5 years ago
parent 221e047d78
commit 642d3d5343

@ -8,54 +8,90 @@ from PIL import Image
from sqlalchemy import func
from sqlalchemy.orm import aliased
from werkzeug.urls import Href
from yadc import db
from yadc.forms import UploadPostForm
from yadc.models import FILETYPE, RATING, Post, Tag, post_tags
def query_replace(dic, base=''):
args = request.args.to_dict()
return Href(base)(args.update(dic) or args)
bp = Blueprint('main', __name__)
@bp.route('/')
def index():
return posts()
return redirect(url_for('main.posts'))
# @bp.route('/')
@bp.route('/post/')
def posts():
posts = Post.query.order_by(Post.created).limit(20).all()
@bp.route('/post', defaults={'page': 1}
)
@bp.route('/post/<int:page>')
def posts(page):
def tags_prepare(posts):
tags = db.session.query(Tag, func.array_agg(posts.c.id)).join(posts, Tag.posts).group_by(Tag.id).all()
# tagset = set()
# taglist = list()
# for post in posts:
# for tag in post.tags:
# tagset.add(tag)
# taglist.append(tag)
# for tag in tagset:
# tag.count = taglist.count(tag)
for tag,p_ids in tags:
tag.count = len(p_ids)
tag.post_ids = p_ids
tag.endpoint = query_replace({'tags': tag.content.replace(' ','_')}, url_for('main.posts'))
# tags = list(tagset)
tags = [t[0] for t in tags]
tags.sort(key=lambda x: (x.category.value, x.content) )
return tags
def parse_args():
args = request.args
tags = (args.get('tags') or '').split(' ')
tags = [t.replace('_',' ') for t in tags] if tags[0] != '' else []
rating = {r.name : r for r in RATING}.get(args.get('rating')) or RATING.safe
matched_ratings = [r for r in RATING if r.value<=rating.value]
# page = int(args.get('page') or 1)
return (tags, matched_ratings)
posts_on_page = 4
f_tags, f_rating = parse_args()
posts_query = Post.query
if len(f_tags)>0:
posts_query = posts_query.join(Post.tags).group_by(Post.id).filter(Tag.content.in_(f_tags)).having(func.count(Post.id)==len(f_tags))
posts_query = posts_query.filter(Post.rating.in_(f_rating)).order_by(Post.created).offset((page-1)*posts_on_page).limit(posts_on_page)
posts = posts_query.all()
tags = tags_prepare(posts_query.subquery())
flash(parse_args())
# flash(posts)
# flash(tags)
tagset = set()
taglist = list()
for post in posts:
for tag in post.tags:
tagset.add(tag)
taglist.append(tag)
for tag in tagset:
tag.count = taglist.count(tag)
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)
@bp.route('/post/show/<id>/')
@bp.route('/post/show/<id>')
def post_show(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)
tag, count = tag_count
tag.count = count
tag.endpoint = query_replace({'tags': tag.content.replace(' ','_')}, url_for('main.posts'))
return render_template('post.html', post=post, tags=tags)
return render_template('post.html', post=post, tags=post.tags)
@bp.route('/post/upload/', methods=['GET', 'POST'])
@bp.route('/post/upload', methods=['GET', 'POST'])
@login_required
def post_upload():
form = UploadPostForm(request.form)
@ -113,15 +149,15 @@ def uploaded_thumb(*args, **kwargs):
return uploaded_img(*args, **kwargs, store='thumb', exten='jpg')
@bp.route('/threads/')
@bp.route('/threads')
def threads():
return render_template('index.html')
@bp.route('/user/show/<username>/')
@bp.route('/user/show/<username>')
def user_profile(username):
pass
@bp.route('/user/settings/')
@bp.route('/user/settings')
@login_required
def user_settings():
pass

@ -138,7 +138,7 @@ class Post(TimestampMixin, db.Model):
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 = "{} - {} {}".format(current_app.config.get('INSTANCE_NAME'), self.id, " ".join(tag.content for tag in self.tags))
filename = "{} - {} {}".format(current_app.config.get('INSTANCE_NAME'), self.id, " ".join(tag.content.replace(' ', '_') for tag in self.tags))
return os.path.join(self.md5, filename)
def url(self, path, endpoint='img'):

@ -0,0 +1,22 @@
{% macro tags(tags) %}
<article class="tags">
<h3>Tags</h3>
<div class="tag_container">
{% for tag in tags %}
<a href="{{ tag.endpoint }}">
<span class="fa fa-tag"></span>
<span class="name">{{ tag.content }}</span>
<span class="count">{{ tag.count }}</span>
<!-- <span class="post_ids">{{ tag.post_ids }}</span> -->
</a>
{% endfor %}
</div>
</article>
{% endmacro %}
<!-- {% macro rating(rating) %}
<article class="rating">
<h3>Rating</h3>
<input type="checkbox" name="" id="">
</article>
{% endmacro %} -->

@ -1,20 +1,10 @@
{% extends 'base.html' %}
{% from '_includes.html' import tags as tags_block %}
{% block content %}
<div class="important_subwrap">
<section class="side_panel">
<article class="tags">
<h3>Tags</h3>
<div class="tag_container">
{% for tag in tags %}
<a href="#">
<span class="fa fa-tag"></span>
<span class="name">{{ tag.content }}</span>
<span class="count">{{ tag.count }}</span>
</a>
{% endfor %}
</div>
</article>
{{ tags_block(tags) }}
</section>
<section class="post_list">
<div class="posts">

@ -1,4 +1,5 @@
{% extends 'base.html' %}
{% from '_includes.html' import tags as tags_block %}
{% block content %}
<div class="important_subwrap">
@ -24,18 +25,7 @@
{% endif %}
</article>
<article class="tags">
<h3>Tags</h3>
<div class="tag_container">
{% for tag in tags %}
<a href="#">
<span class="fa fa-tag"></span>
<span class="name">{{ tag.content }}</span>
<span class="count">{{ tag.count }}</span>
</a>
{% endfor %}
</div>
</article>
{{ tags_block(tags) }}
</section>
<section class="post_single">
<div class="image">

Loading…
Cancel
Save