Less form, more efficiency

This commit is contained in:
Michel Roux 2019-12-15 16:22:39 +01:00
parent 43eab8ab49
commit e5f26622c1
5 changed files with 67 additions and 61 deletions

66
app.py
View File

@ -97,63 +97,35 @@ def list_animes():
return render_template('list.html', search_form=SearchForm(), titles=results)
@app.route('/admin')
@app.route('/admin', methods=['GET', 'POST'])
@auth.login_required
def admin():
folders = AnimeFolder.query.all()
return render_template('admin/list.html', search_form=SearchForm(), folders=folders, delete_form=DeleteForm())
@app.route('/admin/delete', methods=['POST'])
@auth.login_required
def admin_delete():
form = DeleteForm(request.form)
if form.validate_on_submit():
link = AnimeLink.query.filter_by(id=form.id.data).first()
if link:
title = link.title
db.session.delete(link)
if not len(title.links):
db.session.delete(title)
db.session.commit()
form.message = '%s (%s) has been successfully deleted' % (title, link.season)
else:
print(form.errors)
return redirect(url_for('admin'))
form._errors = {'id': ['Id %s was not found in the database' % form.id.data]}
return render_template('admin/list.html', search_form=SearchForm(), folders=folders, action_form=form)
@app.route('/admin/edit/<link_id>')
@app.route('/admin/edit', methods=['GET', 'POST'])
@app.route('/admin/edit/<int:link_id>', methods=['GET', 'POST'])
@auth.login_required
def admin_edit(link_id):
link = AnimeLink.query.filter_by(id=link_id).first()
def admin_edit(link_id=None):
titles = AnimeTitle.query.all()
edit_form = EditForm()
edit_form.folder.choices = [(query.id, query.name) for query in AnimeFolder.query.all()]
return render_template('admin/edit.html', search_form=SearchForm(), link=link, titles=titles, edit_form=edit_form)
@app.route('/admin/add')
@auth.login_required
def admin_add():
edit_form = EditForm()
edit_form.folder.choices = [(0, '')] + [(query.id, query.name) for query in AnimeFolder.query.all()]
titles = AnimeTitle.query.all()
link = AnimeLink()
for attr in dir(link):
if not attr.startswith('_') and getattr(link, attr) is None:
try:
setattr(link, attr, '')
except:
pass
return render_template('admin/edit.html', search_form=SearchForm(), link=link, titles=titles, edit_form=edit_form)
@app.route('/admin/save', methods=['POST'])
@auth.login_required
def admin_save():
form = EditForm(request.form)
form.folder.choices = [(query.id, query.name) for query in AnimeFolder.query.all()]
if form.validate_on_submit():
title = AnimeTitle.query.filter_by(name=form.name.data).first()
title = title if title else AnimeTitle()
@ -170,10 +142,22 @@ def admin_save():
link.vf = form.is_vf.data
db.session.add(link)
db.session.commit()
else:
print(form.errors)
return redirect(url_for('admin'))
if link_id:
link = AnimeLink.query.filter_by(id=link_id).first()
else:
link = AnimeLink()
for attr in dir(link):
if not attr.startswith('_') and getattr(link, attr) is None:
try:
setattr(link, attr, '')
except:
pass
form.folder.choices = [(0, '')] + form.folder.choices
return render_template('admin/edit.html', search_form=SearchForm(), link=link, titles=titles, action_form=form)
if __name__ == '__main__':
app.run('0.0.0.0', APP_PORT, IS_DEBUG)

View File

@ -1,5 +1,6 @@
section {
overflow-x: auto;
padding-top: 2rem !important;
}
nav, nav > div, nav form {
@ -91,7 +92,7 @@ label.checkbox {
#quick_scroll {
float: right;
margin-top: -2rem;
margin-bottom: 1rem;
}
@media screen and (min-width: 1088px) {

View File

@ -1,16 +1,16 @@
{% extends "layout.html" %}
{% block title %}Admin Edit {{ link.name }}{% endblock %}
{% block body %}
<form method="post" action="{{ url_for('admin_save') }}">
{{ edit_form.csrf_token }}
{{ edit_form.id(value=link.id) }}
<form method="post">
{{ action_form.csrf_token }}
{{ action_form.id(value=link.id) }}
<div class="field is-horizontal">
<div class="field-body">
<div class="field column">
<div class="control is-expanded">
<div class="select is-fullwidth">
{{ edit_form.folder }}
{{ action_form.folder }}
</div>
</div>
</div>
@ -18,7 +18,7 @@
<div class="field column is-6">
<div class="control is-expanded">
<div class="select is-fullwidth">
{{ edit_form.name(value=link.title.name, list='animes', class='input', placeholder='Name') }}
{{ action_form.name(value=link.title.name, list='animes', class='input', placeholder='Name') }}
<datalist id="animes">
{% for title in titles %}
<option {{ 'selected' if title.id == link.title.id }}
@ -47,13 +47,13 @@
<div class="field-body">
<div class="field column is-4">
<div class="control is-expanded">
{{ edit_form.link(value=link.link, class='input', placeholder='Link') }}
{{ action_form.link(value=link.link, class='input', placeholder='Link') }}
</div>
</div>
<div class="field column">
<div class="control is-expanded">
{{ edit_form.season(value=link.season, class='input', placeholder='Season') }}
{{ action_form.season(value=link.season, class='input', placeholder='Season') }}
</div>
</div>
</div>
@ -63,14 +63,14 @@
<div class="field-body">
<div class="field column is-5">
<div class="control is-expanded">
{{ edit_form.comment(value=link.comment, class='input', placeholder='Comment') }}
{{ action_form.comment(value=link.comment, class='input', placeholder='Comment') }}
</div>
</div>
<div class="field column">
<div class="control is-expanded">
<div class="select is-fullwidth">
{{ edit_form.keyword(value=link.title.keyword, list='keywords', class='input', placeholder='Keyword') }}
{{ action_form.keyword(value=link.title.keyword, list='keywords', class='input', placeholder='Keyword') }}
<datalist id="keywords">
{% for title in titles %}
<option {{ 'selected' if title.keyword == link.title.keyword }}
@ -83,7 +83,7 @@
<label class="checkbox">
<b>{{ true|flagify }}</b>
{{ edit_form.is_vf(value=link.vf) }}
{{ action_form.is_vf(value=link.vf) }}
</label>
</div>
</div>

View File

@ -1,7 +1,7 @@
{% extends "layout.html" %}
{% block title %}Admin List{% endblock %}
{% block add_button %}
<a class="navbar-item tooltip has-tooltip-bottom" data-tooltip="Add entry" href="{{ url_for('admin_add') }}">
<a class="navbar-item tooltip has-tooltip-bottom" data-tooltip="Add entry" href="{{ url_for('admin_edit') }}">
<i class="fa fa-plus"></i><i>&nbsp;</i><span class="is-hidden-mobile">Add entry</span>
</a>
{% endblock %}
@ -61,8 +61,8 @@
<i class="fa fa-pencil"></i>
</a>
<i>&nbsp;</i>
<form method="post" action="{{ url_for('admin_delete') }}">
{{ delete_form.id(value=link.id) }}
<form method="post">
{{ action_form.id(value=link.id) }}
<button class="fa fa-trash fa-button"
onclick="return confirm('Are you sure you want to delete this item ?')">
</button>

View File

@ -60,6 +60,27 @@
</nav>
<section class="section" role="main">
{% if action_form %}
{% if action_form.errors %}
<div class="notification is-danger">
<button class="delete"></button>
<ul>
{% for field in action_form.errors %}
{% for error in action_form.errors[field] %}
<li>"{{ field }}" => {{ error }}</li>
{% endfor %}
{% endfor %}
</ul>
</div>
{% endif %}
{% if action_form.message %}
<div class="notification is-success">
<button class="delete"></button>
{{ action_form.message }}
</div>
{% endif %}
{% endif %}
{% block body %}{% endblock %}
</section>