From 3b568fa8a15aaea59374c856c4ba5358ac6eb8f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Ku=C5=BE=C3=ADlek?= Date: Sat, 14 Mar 2020 17:12:03 +0100 Subject: [PATCH] Purged and flashed errors :D, management endpoint bugfix, taginput selected autofill fixed --- yadc/assets/js/taginput.js | 42 ++++++++++++++----------- yadc/bp/auth.py | 5 ++- yadc/bp/manage.py | 7 +++-- yadc/bp/post.py | 3 +- yadc/bp/user.py | 7 +++++ yadc/static/all.js | 42 ++++++++++++++----------- yadc/templates/_formhelpers.html | 31 ------------------ yadc/templates/auth/login.html | 4 --- yadc/templates/auth/register.html | 5 --- yadc/templates/auth/reset_password.html | 2 -- yadc/templates/layout/management.html | 4 +-- yadc/templates/manage/posts.html | 2 ++ yadc/templates/manage/tags.html | 2 ++ yadc/templates/manage/users.html | 10 +++++- yadc/templates/post/post.html | 3 -- yadc/templates/post/upload.html | 5 --- yadc/templates/user/settings.html | 13 ++------ yadc/utils.py | 10 +++++- 18 files changed, 92 insertions(+), 105 deletions(-) delete mode 100644 yadc/templates/_formhelpers.html diff --git a/yadc/assets/js/taginput.js b/yadc/assets/js/taginput.js index c45961b..9cd10bf 100644 --- a/yadc/assets/js/taginput.js +++ b/yadc/assets/js/taginput.js @@ -1,14 +1,14 @@ -// https://stackoverflow.com/questions/8486099/how-do-i-parse-a-url-query-parameters-in-javascript -function getJsonFromUrl(url) { - if(!url) url = location.search; - var query = url.substr(1); - var result = {}; - query.split("&").forEach(function(part) { - var item = part.split("="); - result[item[0]] = decodeURIComponent(item[1]); - }); - return result; -} +// // https://stackoverflow.com/questions/8486099/how-do-i-parse-a-url-query-parameters-in-javascript +// function getJsonFromUrl(url) { +// if(!url) url = location.search; +// var query = url.substr(1); +// var result = {}; +// query.split("&").forEach(function(part) { +// var item = part.split("="); +// result[item[0]] = decodeURIComponent(item[1]); +// }); +// return result; +// } function create_selection_tag(tagname) { // CLEAR let tag = document.createElement("a") @@ -67,7 +67,6 @@ taginputs.forEach(function (tinput) { suggest_input.removeAttribute('required') hidden_input.setAttribute('required', '') } - suggest_input.removeAttribute('value') // Disable prefill by browser let suggest_dropdown = suggestroot.querySelector('.suggest-dropdown') @@ -134,12 +133,19 @@ taginputs.forEach(function (tinput) { } // Generate selected by url query - let query = getJsonFromUrl() - if (query['tags']) { - query['tags'].split('+').forEach((tag) => { - add_selected(tag) - }) - } + // let query = getJsonFromUrl() + // if (query['tags']) { + // query['tags'].split('+').forEach((tag) => { + // add_selected(tag) + // }) + // } + let vals = suggest_input.value.split(' ') + console.log(vals) + vals.forEach(tag => { + if (tag == '') return + add_selected(tag) + }); + suggest_input.removeAttribute('value') // Disable prefill by browser function render_suggestions(data) { // 3 references let drop = suggest_dropdown diff --git a/yadc/bp/auth.py b/yadc/bp/auth.py index c8e8639..df9ab24 100644 --- a/yadc/bp/auth.py +++ b/yadc/bp/auth.py @@ -4,7 +4,7 @@ from flask import Blueprint, flash, redirect, render_template, request, url_for, from yadc import db from yadc.forms import LoginForm, RegisterForm, ResetPasswordForm from yadc.models import User -from yadc.utils import nextpage +from yadc.utils import nextpage, flasherrors bp = Blueprint('auth', __name__) @@ -27,6 +27,7 @@ def login(): return redirect(nextpage()) + flasherrors(form) return render_template('auth/login.html', form=form) @bp.route('/logout') @@ -54,6 +55,7 @@ def reset_password(): flash('Password successfully reset. Check your email.') return redirect(url_for('.login')) + flasherrors(form) return render_template('auth/reset_password.html', form=form) @@ -77,4 +79,5 @@ def register(): flash('Your account has been successfully registered. You can now login.') return redirect(url_for('.login')) + flasherrors(form) return render_template('auth/register.html', form=form) diff --git a/yadc/bp/manage.py b/yadc/bp/manage.py index 1a89ac5..7854d96 100644 --- a/yadc/bp/manage.py +++ b/yadc/bp/manage.py @@ -5,6 +5,7 @@ from yadc.forms import UserForm, PostForm, TagForm, CommentForm from yadc import db from yadc.models import User, USER_STATUS, moderator_required, admin_required, Post, Tag, TAG_CATEGORY, Comment +from yadc.utils import flasherrors bp = Blueprint('manage', __name__) @@ -91,8 +92,7 @@ def modify_user(): db.session.commit() flash('{} deleted.'.format(str(el))) elif form.edit.data: - print(form.username) - if form.username.data: el.username = form.username.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 @@ -101,6 +101,7 @@ def modify_user(): db.session.commit() flash('Changes to {} have been applied.'.format(str(el))) + flasherrors(form) return redirect(url_for('.manage_users')) @@ -140,6 +141,7 @@ def modify_post(): flash('Approved post {}'.format(str(post))) redirect(url_for('post.post_show', id=post.id)) + flasherrors(form) return redirect(url_for('.manage_posts')) # Example perfect create/edit/delete form endpoint @@ -171,6 +173,7 @@ def modify_tag(): db.session.commit() flash('Changes to {} have been applied.'.format(str(el))) + flasherrors(form) return redirect(url_for('.manage_tags')) # return redirect(url_for('main.index')) diff --git a/yadc/bp/post.py b/yadc/bp/post.py index 1fabad1..60c901d 100644 --- a/yadc/bp/post.py +++ b/yadc/bp/post.py @@ -10,7 +10,7 @@ from sqlalchemy.orm import aliased from yadc import db from yadc.forms import UploadForm, CommentForm, PostForm from yadc.models import FILETYPE, RATING, POST_STATUS, Post, Tag, Comment, moderator_required -from yadc.utils import query_replace +from yadc.utils import query_replace, flasherrors bp = Blueprint('post', __name__) @@ -94,6 +94,7 @@ def upload(): return redirect(url_for('.posts')) + flasherrors(form) return render_template('post/upload.html', form=form) @bp.route('/tags') diff --git a/yadc/bp/user.py b/yadc/bp/user.py index 4bc20a9..1f4ab57 100644 --- a/yadc/bp/user.py +++ b/yadc/bp/user.py @@ -5,6 +5,7 @@ from yadc.forms import ChangeUserInfoForm, ChangePassForm, ChangeMailForm, Chang from yadc import db from yadc.models import User, Post, Comment +from yadc.utils import flasherrors bp = Blueprint('user', __name__) @@ -40,6 +41,7 @@ def change_info(): db.session.commit() flash('Your biography was updated.') + flasherrors(form) return redirect(url_for('.settings')) @bp.route('/change_pass', methods=['POST']) @@ -56,6 +58,7 @@ def change_pass(): db.session.commit() flash('Password changed successfully.') + flasherrors(form) return redirect(url_for('.settings')) @bp.route('/change_mail', methods=['POST']) @@ -68,6 +71,7 @@ def change_mail(): db.session.commit() flash('Your email was updated.') + flasherrors(form) return redirect(url_for('.settings')) @bp.route('/change_rating', methods=['POST']) @@ -80,6 +84,7 @@ def change_rating(): db.session.commit() flash('Your rating preference was updated.') + flasherrors(form) return redirect(url_for('.settings')) @bp.route('/change_tagblacklist', methods=['POST']) @@ -94,6 +99,7 @@ def change_tagblacklist(): db.session.commit() flash('Your tag blacklist was updated.') + flasherrors(form) return redirect(url_for('.settings')) @bp.route('/delete_data', methods=['POST']) @@ -113,4 +119,5 @@ def delete_data(): return redirect(url_for('main.index')) + flasherrors(form) return redirect(url_for('.settings')) \ No newline at end of file diff --git a/yadc/static/all.js b/yadc/static/all.js index 2383d15..eccb6fb 100644 --- a/yadc/static/all.js +++ b/yadc/static/all.js @@ -55,17 +55,17 @@ document.getElementById("user-menu").addEventListener('click', (ev) => { drop.classList.remove("_drop") } }) -// https://stackoverflow.com/questions/8486099/how-do-i-parse-a-url-query-parameters-in-javascript -function getJsonFromUrl(url) { - if(!url) url = location.search; - var query = url.substr(1); - var result = {}; - query.split("&").forEach(function(part) { - var item = part.split("="); - result[item[0]] = decodeURIComponent(item[1]); - }); - return result; -} +// // https://stackoverflow.com/questions/8486099/how-do-i-parse-a-url-query-parameters-in-javascript +// function getJsonFromUrl(url) { +// if(!url) url = location.search; +// var query = url.substr(1); +// var result = {}; +// query.split("&").forEach(function(part) { +// var item = part.split("="); +// result[item[0]] = decodeURIComponent(item[1]); +// }); +// return result; +// } function create_selection_tag(tagname) { // CLEAR let tag = document.createElement("a") @@ -124,7 +124,6 @@ taginputs.forEach(function (tinput) { suggest_input.removeAttribute('required') hidden_input.setAttribute('required', '') } - suggest_input.removeAttribute('value') // Disable prefill by browser let suggest_dropdown = suggestroot.querySelector('.suggest-dropdown') @@ -191,12 +190,19 @@ taginputs.forEach(function (tinput) { } // Generate selected by url query - let query = getJsonFromUrl() - if (query['tags']) { - query['tags'].split('+').forEach((tag) => { - add_selected(tag) - }) - } + // let query = getJsonFromUrl() + // if (query['tags']) { + // query['tags'].split('+').forEach((tag) => { + // add_selected(tag) + // }) + // } + let vals = suggest_input.value.split(' ') + console.log(vals) + vals.forEach(tag => { + if (tag == '') return + add_selected(tag) + }); + suggest_input.removeAttribute('value') // Disable prefill by browser function render_suggestions(data) { // 3 references let drop = suggest_dropdown diff --git a/yadc/templates/_formhelpers.html b/yadc/templates/_formhelpers.html deleted file mode 100644 index 0267cdc..0000000 --- a/yadc/templates/_formhelpers.html +++ /dev/null @@ -1,31 +0,0 @@ -{% macro errors(field) %} -{% if field.errors %} - -{% endif %} -{% endmacro %} - - -{% macro management_gen_line(field, formfield) %} -{{ field }} -{{ formfield() }} -{{ errors(formfield) }} -{% endmacro %} - - -{% macro genfield(formfield=None) %} -{% if not formfield %} - - {{ caller() }} - -{% else %} - - {{ caller() }} - {{ formfield() }} - {{ errors(formfield) }} - -{% endif %} -{% endmacro %} diff --git a/yadc/templates/auth/login.html b/yadc/templates/auth/login.html index 6167859..288c661 100644 --- a/yadc/templates/auth/login.html +++ b/yadc/templates/auth/login.html @@ -1,5 +1,4 @@ {% extends 'layout/base.html' %} -{% from "_formhelpers.html" import errors %} {% block content %}
@@ -8,17 +7,14 @@ {{ form.csrf_token }} {{ form.username(placeholder="Username") }} - {{ errors(form.username) }} {{ form.password(placeholder="Password") }} - {{ errors(form.password) }} - {{ errors(form.remember_me) }}
{{ form.submit() }} Forgotten password? diff --git a/yadc/templates/auth/register.html b/yadc/templates/auth/register.html index 94fc9ca..47d3374 100644 --- a/yadc/templates/auth/register.html +++ b/yadc/templates/auth/register.html @@ -1,5 +1,4 @@ {% extends 'layout/base.html' %} -{% from "_formhelpers.html" import errors %} {% block content %}
@@ -8,16 +7,12 @@ {{ form.csrf_token }} {{ form.username(placeholder="Username") }} - {{ errors(form.username) }} {{ form.email(placeholder="Email address") }} - {{ errors(form.email) }} {{ form.password(placeholder="Password") }} - {{ errors(form.password) }} {{ form.password_again(placeholder="Repeat password") }} - {{ errors(form.password_again) }}
{{ form.submit() }} diff --git a/yadc/templates/auth/reset_password.html b/yadc/templates/auth/reset_password.html index 49d661a..589500d 100644 --- a/yadc/templates/auth/reset_password.html +++ b/yadc/templates/auth/reset_password.html @@ -1,5 +1,4 @@ {% extends 'layout/base.html' %} -{% from "_formhelpers.html" import errors %} {% block content %}
@@ -9,7 +8,6 @@ {{ form.csrf_token }} {{ form.email(placeholder="Your email address") }} - {{ errors(form.email) }} {{ form.submit() }} diff --git a/yadc/templates/layout/management.html b/yadc/templates/layout/management.html index d8889f8..120efbf 100644 --- a/yadc/templates/layout/management.html +++ b/yadc/templates/layout/management.html @@ -13,7 +13,7 @@ {% for element in elements %} -
+ {{ element.editform.csrf_token }} {{ element.editform.id() }} @@ -44,7 +44,6 @@ {% endblock %} -{% from "_formhelpers.html" import errors %} {% macro genfield(formfield=None) %} {% if not formfield %} @@ -54,7 +53,6 @@ {{ caller() }} {{ formfield() }} - {{ errors(formfield) }} {% endif %} {% endmacro %} \ No newline at end of file diff --git a/yadc/templates/manage/posts.html b/yadc/templates/manage/posts.html index c445d82..d545f4a 100644 --- a/yadc/templates/manage/posts.html +++ b/yadc/templates/manage/posts.html @@ -9,6 +9,8 @@ {% endblock %} +{% block form_endpoint %}{{ url_for('manage.modify_post') }}{% endblock %} + {% block field_headings %} ID MD5 diff --git a/yadc/templates/manage/tags.html b/yadc/templates/manage/tags.html index 8a6143b..ffbd66e 100644 --- a/yadc/templates/manage/tags.html +++ b/yadc/templates/manage/tags.html @@ -9,6 +9,8 @@ {% endblock %} +{% block form_endpoint %}{{ url_for('manage.modify_tag') }}{% endblock %} + {% block field_headings %} Content Category diff --git a/yadc/templates/manage/users.html b/yadc/templates/manage/users.html index e4fa256..e0b181a 100644 --- a/yadc/templates/manage/users.html +++ b/yadc/templates/manage/users.html @@ -9,9 +9,12 @@ {% endblock %} +{% block form_endpoint %}{{ url_for('manage.modify_user') }}{% endblock %} + {% block field_headings %} ID Username +Email address User status Perm level Last login @@ -22,10 +25,15 @@ {{ element.id }} {% endcall %} - {% call genfield(element.editform.username) %} + {#{% call genfield(element.editform.username) %}#} + {% call genfield() %} {{ element.username }} {% endcall %} + {% call genfield(element.editform.email) %} + {{ element.email }} + {% endcall %} + {% call genfield(element.editform.user_status) %} {{ element.user_status.name.capitalize() }} {% endcall %} diff --git a/yadc/templates/post/post.html b/yadc/templates/post/post.html index 94c8375..25a4f66 100644 --- a/yadc/templates/post/post.html +++ b/yadc/templates/post/post.html @@ -39,10 +39,8 @@ {{ editform.id() }} {{ editform.source }} - {{ errors(editform.source) }} {{ render_tag_input(editform.tags) }} - {{ errors(editform.tags) }} {{ editform.edit() }}
@@ -111,7 +109,6 @@ {{ comment_form.post_id() }} {{ comment_form.content() }} - {{ errors(comment_form.content) }} {{ comment_form.create(value="Send") }} diff --git a/yadc/templates/post/upload.html b/yadc/templates/post/upload.html index 61d6ca8..345c017 100644 --- a/yadc/templates/post/upload.html +++ b/yadc/templates/post/upload.html @@ -1,5 +1,4 @@ {% extends 'layout/base.html' %} -{% from "_formhelpers.html" import errors %} {% from "_includes.html" import render_tag_input %} {% block content %} @@ -11,14 +10,11 @@ {#{{ form.post_img.label }}#} {{ form.post_img() }}
- {{ errors(form.post_img) }} {{ form.sauce() }} - {{ errors(form.sauce) }} {{ render_tag_input(form.tags) }} - {{ errors(form.tags) }}
{{ form.rating.label }} @@ -30,7 +26,6 @@ {% endfor %}
- {{ errors(form.rating) }} {{ form.submit() }} diff --git a/yadc/templates/user/settings.html b/yadc/templates/user/settings.html index 7d7663d..856fac7 100644 --- a/yadc/templates/user/settings.html +++ b/yadc/templates/user/settings.html @@ -1,5 +1,4 @@ {% extends 'layout/settings.html' %} -{% from "_formhelpers.html" import errors %} {% block main_content %}
@@ -9,7 +8,6 @@ {{ userinfo_form.csrf_token }} {{ userinfo_form.bio(placeholder="Enter your description") }} - {{ errors(userinfo_form.bio) }} {{ userinfo_form.userinfo_submit() }} @@ -20,13 +18,10 @@ {{ pass_form.csrf_token }} {{ pass_form.password_current(placeholder="Current password") }} - {{ errors(pass_form.password_current) }} {{ pass_form.password(placeholder="Password") }} - {{ errors(pass_form.password) }} {{ pass_form.password_again(placeholder="Repeat password") }} - {{ errors(pass_form.password_again) }} {{ pass_form.pass_submit() }} @@ -37,10 +32,8 @@ {{ mail_form.csrf_token }} {{ mail_form.email() }} - {{ errors(mail_form.email) }} {{ mail_form.email_again() }} - {{ errors(mail_form.email_again) }} {{ mail_form.mail_submit() }} @@ -51,11 +44,13 @@ {{ rating_form.csrf_token }} {{ rating_form.rating() }} - {{ errors(rating_form.rating) }}
+
+

Tag blacklist

+

Delete user data

@@ -63,11 +58,9 @@
  • {{ delete_form.all_comments() }}{{ delete_form.all_comments.label }} - {{ errors(delete_form.all_comments) }}
  • {{ delete_form.all_posts() }}{{ delete_form.all_posts.label }} - {{ errors(delete_form.all_posts) }}
diff --git a/yadc/utils.py b/yadc/utils.py index 4bd61f9..8fa8c23 100644 --- a/yadc/utils.py +++ b/yadc/utils.py @@ -21,4 +21,12 @@ def nextpage(): nextpg = request.args.get('next') if not nextpg or url_parse(nextpg).netloc != '': nextpg = url_for('main.index') - return nextpg \ No newline at end of file + return nextpg + +from flask import flash + +def flasherrors(form): + for key,errs in form.errors.items(): + label = form.__getattribute__(key).label.text + for err in errs: + flash('{}: {}'.format(label,err), category='error') \ No newline at end of file