From 18dcfd53e851f5502c4ef74ca86dcdb9d8d7ad8e Mon Sep 17 00:00:00 2001 From: Michel Roux Date: Sat, 21 Mar 2020 20:17:31 +0100 Subject: [PATCH] Fix title and folder deletion --- app.py | 38 +++++++++++++------- models.py | 4 +-- templates/admin/edit.html | 28 ++++++++++----- templates/admin/list.html | 76 ++++++++++++++++++++------------------- 4 files changed, 85 insertions(+), 61 deletions(-) diff --git a/app.py b/app.py index 3ef18fe..4e96738 100644 --- a/app.py +++ b/app.py @@ -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() + 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/', 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__': diff --git a/models.py b/models.py index 7881f9e..2e6f72a 100644 --- a/models.py +++ b/models.py @@ -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()]) diff --git a/templates/admin/edit.html b/templates/admin/edit.html index d113f2e..ae70b0e 100644 --- a/templates/admin/edit.html +++ b/templates/admin/edit.html @@ -10,7 +10,13 @@
- {{ action_form.folder }} + {{ action_form.folder(value=link.title.folder.name, list='folders', class='input', placeholder='Folder') }} + + {% for folder in folders %} +
@@ -20,10 +26,12 @@
{{ action_form.name(value=link.title.name, list='animes', class='input', placeholder='Name') }} - {% for title in titles %} - @@ -31,7 +39,7 @@ 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,9 +80,11 @@
{{ action_form.keyword(value=link.title.keyword, list='keywords', class='input', placeholder='Keyword') }} - {% for title in titles %} -
diff --git a/templates/admin/list.html b/templates/admin/list.html index 860dfe2..8e3c961 100644 --- a/templates/admin/list.html +++ b/templates/admin/list.html @@ -28,49 +28,51 @@ {% for folder in folders %} - {{ folder.name }} + {% if folder.titles|length > 0 %} + {{ folder.name }} - {% for title in folder.titles %} - {% for link in title.links %} - - {% if not loop.index0 %} - - {{ title.name }} + {% for title in folder.titles %} + {% for link in title.links %} + + {% if not loop.index0 %} + + {{ title.name }} + + {% endif %} + + + {{ link.vf|flagify }} + {{ link.link|urlize(30, target='_blank') }} - {% endif %} - - {{ link.vf|flagify }} - {{ link.link|urlize(30, target='_blank') }} - + + {{ link.season }} + - - {{ link.season }} - + + {{ link.comment|urlize(target='_blank') }} + - - {{ link.comment|urlize(target='_blank') }} - - - - - - -   - - - -   -
- {{ action_form.id(value=link.id) }} - -
- - + + + + +   + + + +   +
+ {{ action_form.id(value=link.id) }} + +
+ + + {% endfor %} {% endfor %} - {% endfor %} + {% endif %} {% endfor %}