1
1
Fork 0

User tag blacklisting

dev
Jan Kužílek 5 years ago
parent c38ae08894
commit ff09a96fed

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

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

@ -1,4 +1,5 @@
{% extends 'layout/settings.html' %}
{% from '_includes.html' import render_tag_input %}
{% block main_content %}
<section class="manage-profile">
@ -50,6 +51,13 @@
</div>
<div class="pageform">
<h3>Tag blacklist</h3>
<form action="{{ url_for('user.change_tagblacklist') }}" method="post">
{{ tags_form.csrf_token }}
{{ render_tag_input(tags_form.tags) }}
{{ tags_form.tags_submit() }}
</form>
</div>
<div class="pageform">
<h3>Delete user data</h3>

Loading…
Cancel
Save