Fix title and folder deletion

This commit is contained in:
Michel Roux 2020-03-21 20:17:31 +01:00
parent c6e5bb1bdd
commit 18dcfd53e8
4 changed files with 85 additions and 61 deletions

36
app.py
View File

@ -112,10 +112,15 @@ def admin():
link = AnimeLink.query.filter_by(id=form.id.data).first()
if link:
title = link.title
folder = title.folder
db.session.delete(link)
db.session.commit()
if not len(title.links):
db.session.delete(title)
db.session.commit()
if not len(folder.titles):
db.session.delete(folder)
db.session.commit()
form.message = '%s (%s) has been successfully deleted' % (title.name, link.season)
else:
form._errors = {'id': ['Id %s was not found in the database' % form.id.data]}
@ -123,18 +128,31 @@ def admin():
return render_template('admin/list.html', search_form=SearchForm(), folders=folders, action_form=form)
def clean_model(obj):
for attr in dir(obj):
if not attr.startswith('_') and getattr(obj, attr) is None:
try:
setattr(obj, attr, '')
except Exception:
pass
return obj
@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=None):
titles = AnimeTitle.query.all()
folders = AnimeFolder.query.all()
form = EditForm(request.form)
form.folder.choices = [(query.id, query.name) for query in AnimeFolder.query.all()]
if form.validate_on_submit():
folder = AnimeFolder.query.filter_by(name=form.folder.data).first()
folder = folder if folder else AnimeFolder()
folder.name = form.folder.data
db.session.add(folder)
title = AnimeTitle.query.filter_by(name=form.name.data).first()
title = title if title else AnimeTitle()
title.folder_id = form.folder.data
title.folder_id = folder.id
title.name = form.name.data
title.keyword = form.keyword.data.lower() if form.keyword.data else title.keyword
db.session.add(title)
@ -153,17 +171,11 @@ def admin_edit(link_id=None):
link = AnimeLink.query.filter_by(id=link_id).first()
form.folder.data = link.title.folder.id
else:
link = AnimeLink()
for attr in dir(link):
if not attr.startswith('_') and getattr(link, attr) is None:
try:
setattr(link, attr, '')
except Exception:
pass
form.folder.choices = [(0, '')] + form.folder.choices
link = clean_model(AnimeLink())
link.title = clean_model(AnimeTitle())
link.vf = False
return render_template('admin/edit.html', search_form=SearchForm(), link=link, titles=titles, action_form=form)
return render_template('admin/edit.html', search_form=SearchForm(), link=link, folders=folders, action_form=form)
if __name__ == '__main__':

View File

@ -1,5 +1,5 @@
from flask_wtf import FlaskForm
from wtforms import BooleanField, HiddenField, SelectField, StringField
from wtforms import BooleanField, HiddenField, StringField
from wtforms.fields.html5 import SearchField, URLField
from wtforms.validators import DataRequired
@ -42,7 +42,7 @@ class DeleteForm(FlaskForm):
class EditForm(FlaskForm):
id = HiddenField('id')
folder = SelectField('folder', validators=[DataRequired()], coerce=int)
folder = StringField('folder', validators=[DataRequired()])
name = StringField('name', validators=[DataRequired()])
link = URLField('link', validators=[DataRequired()])
season = StringField('season', validators=[DataRequired()])

View File

@ -10,7 +10,13 @@
<div class="field column">
<div class="control is-expanded">
<div class="select is-fullwidth">
{{ action_form.folder }}
{{ action_form.folder(value=link.title.folder.name, list='folders', class='input', placeholder='Folder') }}
<datalist id="folders">
{% for folder in folders %}
<option {{ 'selected' if folder.id == link.title.folder.id }}
value="{{ folder.name }}">
{% endfor %}
</datalist>
</div>
</div>
</div>
@ -20,18 +26,20 @@
<div class="select is-fullwidth">
{{ action_form.name(value=link.title.name, list='animes', class='input', placeholder='Name') }}
<datalist id="animes">
{% for title in titles %}
{% for folder in folders %}
{% for title in folder.titles %}
<option {{ 'selected' if title.id == link.title.id }}
data-folder-id="{{ title.folder.id }}" value="{{ title.name }}"
data-folder="{{ title.folder.name }}" value="{{ title.name }}"
data-keyword="{{ title.keyword }}">
{% endfor %}
{% endfor %}
</datalist>
<script>
document.getElementById('name').oninput = function (choice) {
document.getElementById('animes').childNodes.forEach(function (option) {
if (option.value === choice.target.value) {
document.getElementById('folder').value = option.dataset.folderId;
document.getElementById('folder').value = option.dataset.folder;
document.getElementById('keyword').value = option.dataset.keyword;
}
});
@ -72,10 +80,12 @@
<div class="select is-fullwidth">
{{ action_form.keyword(value=link.title.keyword, list='keywords', class='input', placeholder='Keyword') }}
<datalist id="keywords">
{% for title in titles %}
{% for folder in folders %}
{% for title in folder.titles %}
<option {{ 'selected' if title.keyword == link.title.keyword }}
value="{{ title.keyword }}">
{% endfor %}
{% endfor %}
</datalist>
</div>
</div>

View File

@ -28,6 +28,7 @@
<tbody>
{% for folder in folders %}
{% if folder.titles|length > 0 %}
<th colspan="5" id="{{ folder.name }}">{{ folder.name }}</th>
{% for title in folder.titles %}
@ -71,6 +72,7 @@
</tr>
{% endfor %}
{% endfor %}
{% endif %}
{% endfor %}
</tbody>