From 7860622de7ae2ec6ed9329210c617ec1b3f54ff1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Ku=C5=BE=C3=ADlek?= Date: Sun, 15 Mar 2020 10:00:31 +0100 Subject: [PATCH] Fix when checking field values in form endpoints, plus common tag filter/edit field prep --- yadc/bp/manage.py | 21 +++++++++++++-------- yadc/bp/post.py | 21 +++++++++++++++++++-- yadc/forms.py | 4 ++-- yadc/templates/_includes.html | 4 ++-- yadc/templates/post/post.html | 2 +- 5 files changed, 37 insertions(+), 15 deletions(-) diff --git a/yadc/bp/manage.py b/yadc/bp/manage.py index 7854d96..d33f48e 100644 --- a/yadc/bp/manage.py +++ b/yadc/bp/manage.py @@ -94,9 +94,9 @@ def modify_user(): elif form.edit.data: # if form.username.data: el.username = form.username.data - if form.email.data: el.email = form.email.data - if form.user_status.data: el.user_status = form.user_status.data - if form.op_level.data: el.op_level = form.op_level.data + if form.email.raw_data and form.email.data: el.email = form.email.data + if form.user_status.raw_data and form.user_status.data: el.user_status = form.user_status.data + if form.op_level.raw_data and form.op_level.data: el.op_level = form.op_level.data db.session.commit() flash('Changes to {} have been applied.'.format(str(el))) @@ -124,9 +124,14 @@ def modify_post(): db.session.commit() flash('{} deleted.'.format(str(el))) elif form.edit.data: - if form.rating.data: el.rating = form.rating.data - if form.status.data: el.status = form.status.data - if form.source.data: el.source = form.source.data + if form.rating.raw_data and form.rating.data: el.rating = form.rating.data + if form.status.raw_data and form.status.data: el.status = form.status.data + if form.source.raw_data and form.source.data: el.source = form.source.data + + if form.tags.raw_data and form.tags.raw_data: + f_tags = form.tags.data.split() + tags = Tag.query.filter(Tag.content.in_(f_tags)).all() + el.tags = tags db.session.commit() flash('Changes to {} have been applied.'.format(str(el))) @@ -168,7 +173,7 @@ def modify_tag(): flash('{} deleted.'.format(str(el))) elif form.edit.data: # if form.content.data: el.content = form.content.data - if form.category.data: el.category = form.category.data + if form.category.raw_data and form.category.data: el.category = form.category.data db.session.commit() flash('Changes to {} have been applied.'.format(str(el))) @@ -201,7 +206,7 @@ def modify_comment(): db.session.commit() flash('{} deleted.'.format(str(el))) elif form.edit.data: - if form.content.data: el.content = form.content.data.strip() + if form.content.raw_data and form.content.data: el.content = form.content.data.strip() db.session.commit() flash('Changes to {} have been applied.'.format(str(el))) diff --git a/yadc/bp/post.py b/yadc/bp/post.py index 60c901d..485d887 100644 --- a/yadc/bp/post.py +++ b/yadc/bp/post.py @@ -31,7 +31,8 @@ def posts(page): # PARSING ARGUMENTS f_tags = request.args.get('tags', '').split() - f_rating = {r.name : r for r in RATING}.get(request.args.get('rating'), RATING.safe) + # f_rating = {r.name : r for r in RATING}.get(request.args.get('rating'), RATING.safe) + f_rating = current_user.rating if current_user.is_authenticated else RATING.safe m_ratings = f_rating.matched # filter user's blacklist @@ -53,6 +54,9 @@ def posts(page): @bp.route('/show/') def post_show(id): post = Post.query.filter_by(id=id).first() + if not post: + flash('This post does not exist.') + return redirect(url_for('.posts')) 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() for tag,tag.count in tags_count: @@ -61,7 +65,20 @@ def post_show(id): for comment in post.comments: comment.editform = CommentForm(id=comment.id, content=comment.content) - return render_template('post/post.html', post=post, tags=post.tags, comments=post.comments, editform=PostForm(id=post.id), comment_form=CommentForm(post_id=post.id)) + return render_template( + 'post/post.html', + post=post, + tags=post.tags, + comments=post.comments, + editform=PostForm( + id=post.id, + source=post.source, + tags=" ".join([t.content for t in post.tags]), + rating=post.rating, + status=post.status + ), + comment_form=CommentForm(post_id=post.id) + ) from yadc.bp import manage @bp.route('/comment', methods=['POST']) diff --git a/yadc/forms.py b/yadc/forms.py index 854bf1a..68ae433 100644 --- a/yadc/forms.py +++ b/yadc/forms.py @@ -92,7 +92,7 @@ class ChangePassForm(CSRFForm): password = PasswordField('Password', validators=[DataRequired()], render_kw=dict(placeholder="New password")) password_again = PasswordField('Repeat password', validators=[DataRequired(), EqualTo('password')], render_kw=dict(placeholder="Repeat password")) pass_submit = SubmitField('Change password') - + class ChangeMailForm(CSRFForm): email = StringField('Email', validators=[DataRequired(), Email()], render_kw=dict(placeholder="Email")) email_again = StringField('Repeat email', validators=[DataRequired(), EqualTo('email')], render_kw=dict(placeholder="Repeat email")) @@ -104,7 +104,7 @@ class ChangeUserRatingForm(CSRFForm): validators=[DataRequired()], render_kw=dict(onchange="submit()")) rating_submit = SubmitField('Change default rating') - + class ChangeTagBlacklistForm(CSRFForm): tags = StringField('Tags', render_kw=dict(placeholder='Tags', autocomplete='off')) tags_submit = SubmitField('Change blacklisted tags', validators=[DataRequired()]) diff --git a/yadc/templates/_includes.html b/yadc/templates/_includes.html index 91ac568..0fd3acf 100644 --- a/yadc/templates/_includes.html +++ b/yadc/templates/_includes.html @@ -69,8 +69,8 @@ {% endmacro %} -{% macro render_tag_input(input_field) %} -
+{% macro render_tag_input(input_field, param_dict={}) %} +
{##} {{ input_field() }} diff --git a/yadc/templates/post/post.html b/yadc/templates/post/post.html index d541ecc..eec8332 100644 --- a/yadc/templates/post/post.html +++ b/yadc/templates/post/post.html @@ -39,7 +39,7 @@ {{ editform.source }} - {{ render_tag_input(editform.tags) }} + {{ render_tag_input(editform.tags, {'alt-selblock': 'section.sidepanel article.tags .tags-inpage'}) }} {{ editform.edit() }}