diff --git a/yadc/bp/main.py b/yadc/bp/main.py index ce3c3ee..a66a022 100644 --- a/yadc/bp/main.py +++ b/yadc/bp/main.py @@ -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/') +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) - - 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]) + # flash(tags) + return render_template('index.html', posts=posts, tags=tags) -@bp.route('/post/show//') +@bp.route('/post/show/') 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//') +@bp.route('/user/show/') def user_profile(username): pass -@bp.route('/user/settings/') +@bp.route('/user/settings') @login_required def user_settings(): pass diff --git a/yadc/models.py b/yadc/models.py index f9886a3..e1549d9 100644 --- a/yadc/models.py +++ b/yadc/models.py @@ -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'): diff --git a/yadc/templates/_includes.html b/yadc/templates/_includes.html new file mode 100644 index 0000000..9dd616d --- /dev/null +++ b/yadc/templates/_includes.html @@ -0,0 +1,22 @@ +{% macro tags(tags) %} + +{% endmacro %} + + \ No newline at end of file diff --git a/yadc/templates/index.html b/yadc/templates/index.html index 9a3f9de..abe0501 100644 --- a/yadc/templates/index.html +++ b/yadc/templates/index.html @@ -1,20 +1,10 @@ {% extends 'base.html' %} +{% from '_includes.html' import tags as tags_block %} {% block content %}
- + {{ tags_block(tags) }}
diff --git a/yadc/templates/post.html b/yadc/templates/post.html index 58373ea..878d0d8 100644 --- a/yadc/templates/post.html +++ b/yadc/templates/post.html @@ -1,4 +1,5 @@ {% extends 'base.html' %} +{% from '_includes.html' import tags as tags_block %} {% block content %}
@@ -24,18 +25,7 @@ {% endif %} - + {{ tags_block(tags) }}