From ff09a96fed97790404faa61c45037ec220dbe848 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Ku=C5=BE=C3=ADlek?= Date: Wed, 18 Mar 2020 02:41:35 +0100 Subject: [PATCH] User tag blacklisting --- yadc/bp/post.py | 16 +++++++++++----- yadc/bp/user.py | 4 ++-- yadc/templates/user/settings.html | 8 ++++++++ 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/yadc/bp/post.py b/yadc/bp/post.py index ce6e454..9ce8994 100644 --- a/yadc/bp/post.py +++ b/yadc/bp/post.py @@ -42,12 +42,18 @@ def posts(page): m_ratings = f_rating.matched if f_rating else RATING.safe.matched # filter user's blacklist - if current_user.is_authenticated: - f_tags = list(t for t in f_tags if t not in [tb.content for tb in current_user.tag_blacklist]) + # if current_user.is_authenticated: + # f_tags = list(t for t in f_tags if t not in [tb.content for tb in current_user.tag_blacklist]) + blacklist_tags = [tb.content for tb in current_user.tag_blacklist] if current_user.is_authenticated else None posts_query = Post.query - if f_tags: - 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)) + if f_tags or blacklist_tags: + posts_query = posts_query.join(Post.tags).group_by(Post.id) + if blacklist_tags: + subquery = db.session.query(Post.id).join(Tag.posts).filter(Tag.content.in_(blacklist_tags)).subquery() + posts_query = posts_query.filter(~Post.id.in_(subquery)) + if f_tags: + posts_query = posts_query.filter(Tag.content.in_(f_tags)).having(func.count(Post.id)==len(f_tags)) posts_query = posts_query.filter(Post.rating.in_(m_ratings)).order_by(Post.created.desc()) posts = posts_query.paginate(page, current_app.config.get('POSTS_PER_PAGE')) @@ -79,7 +85,7 @@ def post_show(id): id=post.id, referer=post_show.__name__, source=post.source, - tags=" ".join([t.content for t in post.tags]), + tags=' '.join(t.content for t in post.tags), rating=post.rating, status=post.status ), diff --git a/yadc/bp/user.py b/yadc/bp/user.py index 75a1dce..f137223 100644 --- a/yadc/bp/user.py +++ b/yadc/bp/user.py @@ -4,7 +4,7 @@ from flask_login import current_user, login_required from yadc.forms import ChangeUserInfoForm, ChangePassForm, ChangeMailForm, ChangeUserRatingForm, ChangeTagBlacklistForm, DeleteUserDataForm from yadc import db -from yadc.models import User, Post, Comment +from yadc.models import User, Post, Comment, Tag from yadc.utils import flasherrors bp = Blueprint('user', __name__) @@ -23,7 +23,7 @@ def settings(): pass_form=ChangePassForm(), mail_form=ChangeMailForm(), rating_form=ChangeUserRatingForm(rating=current_user.rating.name), - tags_form=ChangeTagBlacklistForm(), + tags_form=ChangeTagBlacklistForm(tags=' '.join(t.content for t in current_user.tag_blacklist)), delete_form=DeleteUserDataForm() ) diff --git a/yadc/templates/user/settings.html b/yadc/templates/user/settings.html index 82a6ce0..0007757 100644 --- a/yadc/templates/user/settings.html +++ b/yadc/templates/user/settings.html @@ -1,4 +1,5 @@ {% extends 'layout/settings.html' %} +{% from '_includes.html' import render_tag_input %} {% block main_content %}
@@ -50,6 +51,13 @@

Tag blacklist

+
+ {{ tags_form.csrf_token }} + + {{ render_tag_input(tags_form.tags) }} + + {{ tags_form.tags_submit() }} +

Delete user data