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

38
app.py
View File

@ -112,10 +112,15 @@ def admin():
link = AnimeLink.query.filter_by(id=form.id.data).first() link = AnimeLink.query.filter_by(id=form.id.data).first()
if link: if link:
title = link.title title = link.title
folder = title.folder
db.session.delete(link) db.session.delete(link)
db.session.commit()
if not len(title.links): if not len(title.links):
db.session.delete(title) 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) form.message = '%s (%s) has been successfully deleted' % (title.name, link.season)
else: else:
form._errors = {'id': ['Id %s was not found in the database' % form.id.data]} 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) 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'])
@app.route('/admin/edit/<int:link_id>', methods=['GET', 'POST']) @app.route('/admin/edit/<int:link_id>', methods=['GET', 'POST'])
@auth.login_required @auth.login_required
def admin_edit(link_id=None): def admin_edit(link_id=None):
titles = AnimeTitle.query.all() folders = AnimeFolder.query.all()
form = EditForm(request.form) form = EditForm(request.form)
form.folder.choices = [(query.id, query.name) for query in AnimeFolder.query.all()]
if form.validate_on_submit(): 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 = AnimeTitle.query.filter_by(name=form.name.data).first()
title = title if title else AnimeTitle() title = title if title else AnimeTitle()
title.folder_id = form.folder.data title.folder_id = folder.id
title.name = form.name.data title.name = form.name.data
title.keyword = form.keyword.data.lower() if form.keyword.data else title.keyword title.keyword = form.keyword.data.lower() if form.keyword.data else title.keyword
db.session.add(title) db.session.add(title)
@ -153,17 +171,11 @@ def admin_edit(link_id=None):
link = AnimeLink.query.filter_by(id=link_id).first() link = AnimeLink.query.filter_by(id=link_id).first()
form.folder.data = link.title.folder.id form.folder.data = link.title.folder.id
else: else:
link = AnimeLink() link = clean_model(AnimeLink())
for attr in dir(link): link.title = clean_model(AnimeTitle())
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.vf = False 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__': if __name__ == '__main__':

View File

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

View File

@ -10,7 +10,13 @@
<div class="field column"> <div class="field column">
<div class="control is-expanded"> <div class="control is-expanded">
<div class="select is-fullwidth"> <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> </div>
</div> </div>
@ -20,10 +26,12 @@
<div class="select is-fullwidth"> <div class="select is-fullwidth">
{{ action_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"> <datalist id="animes">
{% for title in titles %} {% for folder in folders %}
<option {{ 'selected' if title.id == link.title.id }} {% for title in folder.titles %}
data-folder-id="{{ title.folder.id }}" value="{{ title.name }}" <option {{ 'selected' if title.id == link.title.id }}
data-keyword="{{ title.keyword }}"> data-folder="{{ title.folder.name }}" value="{{ title.name }}"
data-keyword="{{ title.keyword }}">
{% endfor %}
{% endfor %} {% endfor %}
</datalist> </datalist>
@ -31,7 +39,7 @@
document.getElementById('name').oninput = function (choice) { document.getElementById('name').oninput = function (choice) {
document.getElementById('animes').childNodes.forEach(function (option) { document.getElementById('animes').childNodes.forEach(function (option) {
if (option.value === choice.target.value) { 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; document.getElementById('keyword').value = option.dataset.keyword;
} }
}); });
@ -72,9 +80,11 @@
<div class="select is-fullwidth"> <div class="select is-fullwidth">
{{ action_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"> <datalist id="keywords">
{% for title in titles %} {% for folder in folders %}
<option {{ 'selected' if title.keyword == link.title.keyword }} {% for title in folder.titles %}
value="{{ title.keyword }}"> <option {{ 'selected' if title.keyword == link.title.keyword }}
value="{{ title.keyword }}">
{% endfor %}
{% endfor %} {% endfor %}
</datalist> </datalist>
</div> </div>

View File

@ -28,49 +28,51 @@
<tbody> <tbody>
{% for folder in folders %} {% for folder in folders %}
<th colspan="5" id="{{ folder.name }}">{{ folder.name }}</th> {% if folder.titles|length > 0 %}
<th colspan="5" id="{{ folder.name }}">{{ folder.name }}</th>
{% for title in folder.titles %} {% for title in folder.titles %}
{% for link in title.links %} {% for link in title.links %}
<tr> <tr>
{% if not loop.index0 %} {% if not loop.index0 %}
<td rowspan="{{ title.links|length }}"> <td rowspan="{{ title.links|length }}">
{{ title.name }} {{ title.name }}
</td>
{% endif %}
<td class="{{ link|colorify }}">
{{ link.vf|flagify }}
{{ link.link|urlize(30, target='_blank') }}
</td> </td>
{% endif %}
<td class="{{ link|colorify }}"> <td>
{{ link.vf|flagify }} {{ link.season }}
{{ link.link|urlize(30, target='_blank') }} </td>
</td>
<td> <td>
{{ link.season }} {{ link.comment|urlize(target='_blank') }}
</td> </td>
<td> <td>
{{ link.comment|urlize(target='_blank') }} <a href="{{ url_for('search', q=link.title.keyword) }}" target="_blank">
</td> <i class="fa fa-search"></i>
</a>
<td> <i>&nbsp;</i>
<a href="{{ url_for('search', q=link.title.keyword) }}" target="_blank"> <a href="{{ url_for('admin_edit', link_id=link.id) }}">
<i class="fa fa-search"></i> <i class="fa fa-pencil"></i>
</a> </a>
<i>&nbsp;</i> <i>&nbsp;</i>
<a href="{{ url_for('admin_edit', link_id=link.id) }}"> <form method="post">
<i class="fa fa-pencil"></i> {{ action_form.id(value=link.id) }}
</a> <button class="fa fa-trash fa-button"
<i>&nbsp;</i> onclick="return confirm('Are you sure you want to delete this item ?')">
<form method="post"> </button>
{{ action_form.id(value=link.id) }} </form>
<button class="fa fa-trash fa-button" </td>
onclick="return confirm('Are you sure you want to delete this item ?')"> </tr>
</button> {% endfor %}
</form>
</td>
</tr>
{% endfor %} {% endfor %}
{% endfor %} {% endif %}
{% endfor %} {% endfor %}
</tbody> </tbody>