1
1
Fork 0

Purged and flashed errors :D,

management endpoint bugfix,
taginput selected autofill fixed
dev
Jan Kužílek 5 years ago
parent 305a4cea30
commit 3b568fa8a1

@ -1,14 +1,14 @@
// https://stackoverflow.com/questions/8486099/how-do-i-parse-a-url-query-parameters-in-javascript // // https://stackoverflow.com/questions/8486099/how-do-i-parse-a-url-query-parameters-in-javascript
function getJsonFromUrl(url) { // function getJsonFromUrl(url) {
if(!url) url = location.search; // if(!url) url = location.search;
var query = url.substr(1); // var query = url.substr(1);
var result = {}; // var result = {};
query.split("&").forEach(function(part) { // query.split("&").forEach(function(part) {
var item = part.split("="); // var item = part.split("=");
result[item[0]] = decodeURIComponent(item[1]); // result[item[0]] = decodeURIComponent(item[1]);
}); // });
return result; // return result;
} // }
function create_selection_tag(tagname) { // CLEAR function create_selection_tag(tagname) { // CLEAR
let tag = document.createElement("a") let tag = document.createElement("a")
@ -67,7 +67,6 @@ taginputs.forEach(function (tinput) {
suggest_input.removeAttribute('required') suggest_input.removeAttribute('required')
hidden_input.setAttribute('required', '') hidden_input.setAttribute('required', '')
} }
suggest_input.removeAttribute('value') // Disable prefill by browser
let suggest_dropdown = suggestroot.querySelector('.suggest-dropdown') let suggest_dropdown = suggestroot.querySelector('.suggest-dropdown')
@ -134,12 +133,19 @@ taginputs.forEach(function (tinput) {
} }
// Generate selected by url query // Generate selected by url query
let query = getJsonFromUrl() // let query = getJsonFromUrl()
if (query['tags']) { // if (query['tags']) {
query['tags'].split('+').forEach((tag) => { // 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) add_selected(tag)
}) });
} suggest_input.removeAttribute('value') // Disable prefill by browser
function render_suggestions(data) { // 3 references function render_suggestions(data) { // 3 references
let drop = suggest_dropdown let drop = suggest_dropdown

@ -4,7 +4,7 @@ from flask import Blueprint, flash, redirect, render_template, request, url_for,
from yadc import db from yadc import db
from yadc.forms import LoginForm, RegisterForm, ResetPasswordForm from yadc.forms import LoginForm, RegisterForm, ResetPasswordForm
from yadc.models import User from yadc.models import User
from yadc.utils import nextpage from yadc.utils import nextpage, flasherrors
bp = Blueprint('auth', __name__) bp = Blueprint('auth', __name__)
@ -27,6 +27,7 @@ def login():
return redirect(nextpage()) return redirect(nextpage())
flasherrors(form)
return render_template('auth/login.html', form=form) return render_template('auth/login.html', form=form)
@bp.route('/logout') @bp.route('/logout')
@ -54,6 +55,7 @@ def reset_password():
flash('Password successfully reset. Check your email.') flash('Password successfully reset. Check your email.')
return redirect(url_for('.login')) return redirect(url_for('.login'))
flasherrors(form)
return render_template('auth/reset_password.html', form=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.') flash('Your account has been successfully registered. You can now login.')
return redirect(url_for('.login')) return redirect(url_for('.login'))
flasherrors(form)
return render_template('auth/register.html', form=form) return render_template('auth/register.html', form=form)

@ -5,6 +5,7 @@ from yadc.forms import UserForm, PostForm, TagForm, CommentForm
from yadc import db from yadc import db
from yadc.models import User, USER_STATUS, moderator_required, admin_required, Post, Tag, TAG_CATEGORY, Comment 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__) bp = Blueprint('manage', __name__)
@ -91,8 +92,7 @@ def modify_user():
db.session.commit() db.session.commit()
flash('{} deleted.'.format(str(el))) flash('{} deleted.'.format(str(el)))
elif form.edit.data: 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.email.data: el.email = form.email.data
if form.user_status.data: el.user_status = form.user_status.data if form.user_status.data: el.user_status = form.user_status.data
@ -101,6 +101,7 @@ def modify_user():
db.session.commit() db.session.commit()
flash('Changes to {} have been applied.'.format(str(el))) flash('Changes to {} have been applied.'.format(str(el)))
flasherrors(form)
return redirect(url_for('.manage_users')) return redirect(url_for('.manage_users'))
@ -140,6 +141,7 @@ def modify_post():
flash('Approved post {}'.format(str(post))) flash('Approved post {}'.format(str(post)))
redirect(url_for('post.post_show', id=post.id)) redirect(url_for('post.post_show', id=post.id))
flasherrors(form)
return redirect(url_for('.manage_posts')) return redirect(url_for('.manage_posts'))
# Example perfect create/edit/delete form endpoint # Example perfect create/edit/delete form endpoint
@ -171,6 +173,7 @@ def modify_tag():
db.session.commit() db.session.commit()
flash('Changes to {} have been applied.'.format(str(el))) flash('Changes to {} have been applied.'.format(str(el)))
flasherrors(form)
return redirect(url_for('.manage_tags')) return redirect(url_for('.manage_tags'))
# return redirect(url_for('main.index')) # return redirect(url_for('main.index'))

@ -10,7 +10,7 @@ from sqlalchemy.orm import aliased
from yadc import db from yadc import db
from yadc.forms import UploadForm, CommentForm, PostForm from yadc.forms import UploadForm, CommentForm, PostForm
from yadc.models import FILETYPE, RATING, POST_STATUS, Post, Tag, Comment, moderator_required 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__) bp = Blueprint('post', __name__)
@ -94,6 +94,7 @@ def upload():
return redirect(url_for('.posts')) return redirect(url_for('.posts'))
flasherrors(form)
return render_template('post/upload.html', form=form) return render_template('post/upload.html', form=form)
@bp.route('/tags') @bp.route('/tags')

@ -5,6 +5,7 @@ from yadc.forms import ChangeUserInfoForm, ChangePassForm, ChangeMailForm, Chang
from yadc import db from yadc import db
from yadc.models import User, Post, Comment from yadc.models import User, Post, Comment
from yadc.utils import flasherrors
bp = Blueprint('user', __name__) bp = Blueprint('user', __name__)
@ -40,6 +41,7 @@ def change_info():
db.session.commit() db.session.commit()
flash('Your biography was updated.') flash('Your biography was updated.')
flasherrors(form)
return redirect(url_for('.settings')) return redirect(url_for('.settings'))
@bp.route('/change_pass', methods=['POST']) @bp.route('/change_pass', methods=['POST'])
@ -56,6 +58,7 @@ def change_pass():
db.session.commit() db.session.commit()
flash('Password changed successfully.') flash('Password changed successfully.')
flasherrors(form)
return redirect(url_for('.settings')) return redirect(url_for('.settings'))
@bp.route('/change_mail', methods=['POST']) @bp.route('/change_mail', methods=['POST'])
@ -68,6 +71,7 @@ def change_mail():
db.session.commit() db.session.commit()
flash('Your email was updated.') flash('Your email was updated.')
flasherrors(form)
return redirect(url_for('.settings')) return redirect(url_for('.settings'))
@bp.route('/change_rating', methods=['POST']) @bp.route('/change_rating', methods=['POST'])
@ -80,6 +84,7 @@ def change_rating():
db.session.commit() db.session.commit()
flash('Your rating preference was updated.') flash('Your rating preference was updated.')
flasherrors(form)
return redirect(url_for('.settings')) return redirect(url_for('.settings'))
@bp.route('/change_tagblacklist', methods=['POST']) @bp.route('/change_tagblacklist', methods=['POST'])
@ -94,6 +99,7 @@ def change_tagblacklist():
db.session.commit() db.session.commit()
flash('Your tag blacklist was updated.') flash('Your tag blacklist was updated.')
flasherrors(form)
return redirect(url_for('.settings')) return redirect(url_for('.settings'))
@bp.route('/delete_data', methods=['POST']) @bp.route('/delete_data', methods=['POST'])
@ -113,4 +119,5 @@ def delete_data():
return redirect(url_for('main.index')) return redirect(url_for('main.index'))
flasherrors(form)
return redirect(url_for('.settings')) return redirect(url_for('.settings'))

@ -55,17 +55,17 @@ document.getElementById("user-menu").addEventListener('click', (ev) => {
drop.classList.remove("_drop") drop.classList.remove("_drop")
} }
}) })
// https://stackoverflow.com/questions/8486099/how-do-i-parse-a-url-query-parameters-in-javascript // // https://stackoverflow.com/questions/8486099/how-do-i-parse-a-url-query-parameters-in-javascript
function getJsonFromUrl(url) { // function getJsonFromUrl(url) {
if(!url) url = location.search; // if(!url) url = location.search;
var query = url.substr(1); // var query = url.substr(1);
var result = {}; // var result = {};
query.split("&").forEach(function(part) { // query.split("&").forEach(function(part) {
var item = part.split("="); // var item = part.split("=");
result[item[0]] = decodeURIComponent(item[1]); // result[item[0]] = decodeURIComponent(item[1]);
}); // });
return result; // return result;
} // }
function create_selection_tag(tagname) { // CLEAR function create_selection_tag(tagname) { // CLEAR
let tag = document.createElement("a") let tag = document.createElement("a")
@ -124,7 +124,6 @@ taginputs.forEach(function (tinput) {
suggest_input.removeAttribute('required') suggest_input.removeAttribute('required')
hidden_input.setAttribute('required', '') hidden_input.setAttribute('required', '')
} }
suggest_input.removeAttribute('value') // Disable prefill by browser
let suggest_dropdown = suggestroot.querySelector('.suggest-dropdown') let suggest_dropdown = suggestroot.querySelector('.suggest-dropdown')
@ -191,12 +190,19 @@ taginputs.forEach(function (tinput) {
} }
// Generate selected by url query // Generate selected by url query
let query = getJsonFromUrl() // let query = getJsonFromUrl()
if (query['tags']) { // if (query['tags']) {
query['tags'].split('+').forEach((tag) => { // 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) add_selected(tag)
}) });
} suggest_input.removeAttribute('value') // Disable prefill by browser
function render_suggestions(data) { // 3 references function render_suggestions(data) { // 3 references
let drop = suggest_dropdown let drop = suggest_dropdown

@ -1,31 +0,0 @@
{% macro errors(field) %}
{% if field.errors %}
<ul class=errors>
{% for error in field.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
{% endmacro %}
<!-- DEPRECATED + wrong function name, but whatever -->
{% macro management_gen_line(field, formfield) %}
<span class="show">{{ field }}</span>
<span class="edit">{{ formfield() }}</span>
{{ errors(formfield) }}
{% endmacro %}
<!-- COPY OF genfield IN management.html -->
{% macro genfield(formfield=None) %}
{% if not formfield %}
<td>
<span>{{ caller() }}</span>
</td>
{% else %}
<td>
<span class="notedit">{{ caller() }}</span>
<span class="edit">{{ formfield() }}</span>
{{ errors(formfield) }}
</td>
{% endif %}
{% endmacro %}

@ -1,5 +1,4 @@
{% extends 'layout/base.html' %} {% extends 'layout/base.html' %}
{% from "_formhelpers.html" import errors %}
{% block content %} {% block content %}
<div class="pageform"> <div class="pageform">
@ -8,17 +7,14 @@
{{ form.csrf_token }} {{ form.csrf_token }}
{{ form.username(placeholder="Username") }} {{ form.username(placeholder="Username") }}
{{ errors(form.username) }}
{{ form.password(placeholder="Password") }} {{ form.password(placeholder="Password") }}
{{ errors(form.password) }}
<ul> <ul>
<li> <li>
{{ form.remember_me() }}{{ form.remember_me.label }} {{ form.remember_me() }}{{ form.remember_me.label }}
</li> </li>
</ul> </ul>
{{ errors(form.remember_me) }}
<div class="row stretch"> <div class="row stretch">
{{ form.submit() }} {{ form.submit() }}
<a href="{{ url_for('auth.reset_password') }}">Forgotten password?</a> <a href="{{ url_for('auth.reset_password') }}">Forgotten password?</a>

@ -1,5 +1,4 @@
{% extends 'layout/base.html' %} {% extends 'layout/base.html' %}
{% from "_formhelpers.html" import errors %}
{% block content %} {% block content %}
<div class="pageform"> <div class="pageform">
@ -8,16 +7,12 @@
{{ form.csrf_token }} {{ form.csrf_token }}
{{ form.username(placeholder="Username") }} {{ form.username(placeholder="Username") }}
{{ errors(form.username) }}
{{ form.email(placeholder="Email address") }} {{ form.email(placeholder="Email address") }}
{{ errors(form.email) }}
{{ form.password(placeholder="Password") }} {{ form.password(placeholder="Password") }}
{{ errors(form.password) }}
{{ form.password_again(placeholder="Repeat password") }} {{ form.password_again(placeholder="Repeat password") }}
{{ errors(form.password_again) }}
<div> <div>
{{ form.submit() }} {{ form.submit() }}

@ -1,5 +1,4 @@
{% extends 'layout/base.html' %} {% extends 'layout/base.html' %}
{% from "_formhelpers.html" import errors %}
{% block content %} {% block content %}
<div class="pageform"> <div class="pageform">
@ -9,7 +8,6 @@
{{ form.csrf_token }} {{ form.csrf_token }}
{{ form.email(placeholder="Your email address") }} {{ form.email(placeholder="Your email address") }}
{{ errors(form.email) }}
{{ form.submit() }} {{ form.submit() }}
</form> </form>

@ -13,7 +13,7 @@
<tbody> <tbody>
{% for element in elements %} {% for element in elements %}
<tr class="editingable"> <tr class="editingable">
<form action="{{ url_for('manage.modify_tag') }}" method="post"> <form action="{% block form_endpoint %}{% endblock %}" method="post">
{{ element.editform.csrf_token }} {{ element.editform.csrf_token }}
{{ element.editform.id() }} {{ element.editform.id() }}
@ -44,7 +44,6 @@
</table> </table>
{% endblock %} {% endblock %}
{% from "_formhelpers.html" import errors %}
{% macro genfield(formfield=None) %} {% macro genfield(formfield=None) %}
{% if not formfield %} {% if not formfield %}
<td> <td>
@ -54,7 +53,6 @@
<td> <td>
<span class="notedit">{{ caller() }}</span> <span class="notedit">{{ caller() }}</span>
<span class="edit">{{ formfield() }}</span> <span class="edit">{{ formfield() }}</span>
{{ errors(formfield) }}
</td> </td>
{% endif %} {% endif %}
{% endmacro %} {% endmacro %}

@ -9,6 +9,8 @@
</section> </section>
{% endblock %} {% endblock %}
{% block form_endpoint %}{{ url_for('manage.modify_post') }}{% endblock %}
{% block field_headings %} {% block field_headings %}
<th>ID</th> <th>ID</th>
<th>MD5</th> <th>MD5</th>

@ -9,6 +9,8 @@
</section> </section>
{% endblock %} {% endblock %}
{% block form_endpoint %}{{ url_for('manage.modify_tag') }}{% endblock %}
{% block field_headings %} {% block field_headings %}
<th>Content</th> <th>Content</th>
<th>Category</th> <th>Category</th>

@ -9,9 +9,12 @@
</section> </section>
{% endblock %} {% endblock %}
{% block form_endpoint %}{{ url_for('manage.modify_user') }}{% endblock %}
{% block field_headings %} {% block field_headings %}
<th>ID</th> <th>ID</th>
<th>Username</th> <th>Username</th>
<th>Email address</th>
<th>User status</th> <th>User status</th>
<th>Perm level</th> <th>Perm level</th>
<th>Last login</th> <th>Last login</th>
@ -22,10 +25,15 @@
{{ element.id }} {{ element.id }}
{% endcall %} {% endcall %}
{% call genfield(element.editform.username) %} {#{% call genfield(element.editform.username) %}#}
{% call genfield() %}
{{ element.username }} {{ element.username }}
{% endcall %} {% endcall %}
{% call genfield(element.editform.email) %}
{{ element.email }}
{% endcall %}
{% call genfield(element.editform.user_status) %} {% call genfield(element.editform.user_status) %}
{{ element.user_status.name.capitalize() }} {{ element.user_status.name.capitalize() }}
{% endcall %} {% endcall %}

@ -39,10 +39,8 @@
{{ editform.id() }} {{ editform.id() }}
{{ editform.source }} {{ editform.source }}
{{ errors(editform.source) }}
{{ render_tag_input(editform.tags) }} {{ render_tag_input(editform.tags) }}
{{ errors(editform.tags) }}
{{ editform.edit() }} {{ editform.edit() }}
</form> </form>
@ -111,7 +109,6 @@
{{ comment_form.post_id() }} {{ comment_form.post_id() }}
{{ comment_form.content() }} {{ comment_form.content() }}
{{ errors(comment_form.content) }}
{{ comment_form.create(value="Send") }} {{ comment_form.create(value="Send") }}
</form> </form>

@ -1,5 +1,4 @@
{% extends 'layout/base.html' %} {% extends 'layout/base.html' %}
{% from "_formhelpers.html" import errors %}
{% from "_includes.html" import render_tag_input %} {% from "_includes.html" import render_tag_input %}
{% block content %} {% block content %}
@ -11,14 +10,11 @@
{#{{ form.post_img.label }}#} {#{{ form.post_img.label }}#}
{{ form.post_img() }} {{ form.post_img() }}
</div> </div>
{{ errors(form.post_img) }}
{{ form.sauce() }} {{ form.sauce() }}
{{ errors(form.sauce) }}
{{ render_tag_input(form.tags) }} {{ render_tag_input(form.tags) }}
<!-- {{ form.tags(placeholder="Tags") }} --> <!-- {{ form.tags(placeholder="Tags") }} -->
{{ errors(form.tags) }}
<div> <div>
{{ form.rating.label }} {{ form.rating.label }}
@ -30,7 +26,6 @@
{% endfor %} {% endfor %}
</ul> </ul>
</div> </div>
{{ errors(form.rating) }}
{{ form.submit() }} {{ form.submit() }}
</form> </form>

@ -1,5 +1,4 @@
{% extends 'layout/settings.html' %} {% extends 'layout/settings.html' %}
{% from "_formhelpers.html" import errors %}
{% block main_content %} {% block main_content %}
<section class="manage-profile"> <section class="manage-profile">
@ -9,7 +8,6 @@
{{ userinfo_form.csrf_token }} {{ userinfo_form.csrf_token }}
{{ userinfo_form.bio(placeholder="Enter your description") }} {{ userinfo_form.bio(placeholder="Enter your description") }}
{{ errors(userinfo_form.bio) }}
{{ userinfo_form.userinfo_submit() }} {{ userinfo_form.userinfo_submit() }}
</form> </form>
@ -20,13 +18,10 @@
{{ pass_form.csrf_token }} {{ pass_form.csrf_token }}
{{ pass_form.password_current(placeholder="Current password") }} {{ pass_form.password_current(placeholder="Current password") }}
{{ errors(pass_form.password_current) }}
{{ pass_form.password(placeholder="Password") }} {{ pass_form.password(placeholder="Password") }}
{{ errors(pass_form.password) }}
{{ pass_form.password_again(placeholder="Repeat password") }} {{ pass_form.password_again(placeholder="Repeat password") }}
{{ errors(pass_form.password_again) }}
{{ pass_form.pass_submit() }} {{ pass_form.pass_submit() }}
</form> </form>
@ -37,10 +32,8 @@
{{ mail_form.csrf_token }} {{ mail_form.csrf_token }}
{{ mail_form.email() }} {{ mail_form.email() }}
{{ errors(mail_form.email) }}
{{ mail_form.email_again() }} {{ mail_form.email_again() }}
{{ errors(mail_form.email_again) }}
{{ mail_form.mail_submit() }} {{ mail_form.mail_submit() }}
</form> </form>
@ -51,11 +44,13 @@
{{ rating_form.csrf_token }} {{ rating_form.csrf_token }}
{{ rating_form.rating() }} {{ rating_form.rating() }}
{{ errors(rating_form.rating) }}
<noscript>{{ rating_form.rating_submit() }}</noscript> <noscript>{{ rating_form.rating_submit() }}</noscript>
</form> </form>
</div> </div>
<div class="pageform">
<h3>Tag blacklist</h3>
</div>
<div class="pageform"> <div class="pageform">
<h3>Delete user data</h3> <h3>Delete user data</h3>
<form action="{{ url_for('user.delete_data') }}" method="post"> <form action="{{ url_for('user.delete_data') }}" method="post">
@ -63,11 +58,9 @@
<ul> <ul>
<li> <li>
{{ delete_form.all_comments() }}{{ delete_form.all_comments.label }} {{ delete_form.all_comments() }}{{ delete_form.all_comments.label }}
{{ errors(delete_form.all_comments) }}
</li> </li>
<li> <li>
{{ delete_form.all_posts() }}{{ delete_form.all_posts.label }} {{ delete_form.all_posts() }}{{ delete_form.all_posts.label }}
{{ errors(delete_form.all_posts) }}
</li> </li>
</ul> </ul>

@ -22,3 +22,11 @@ def nextpage():
if not nextpg or url_parse(nextpg).netloc != '': if not nextpg or url_parse(nextpg).netloc != '':
nextpg = url_for('main.index') nextpg = url_for('main.index')
return nextpg 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')
Loading…
Cancel
Save