Fix title and folder deletion
This commit is contained in:
parent
c6e5bb1bdd
commit
18dcfd53e8
36
app.py
36
app.py
@ -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__':
|
||||||
|
@ -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()])
|
||||||
|
@ -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,18 +26,20 @@
|
|||||||
<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 %}
|
||||||
|
{% for title in folder.titles %}
|
||||||
<option {{ 'selected' if title.id == link.title.id }}
|
<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 }}">
|
data-keyword="{{ title.keyword }}">
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
{% endfor %}
|
||||||
</datalist>
|
</datalist>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
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,10 +80,12 @@
|
|||||||
<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 %}
|
||||||
|
{% for title in folder.titles %}
|
||||||
<option {{ 'selected' if title.keyword == link.title.keyword }}
|
<option {{ 'selected' if title.keyword == link.title.keyword }}
|
||||||
value="{{ title.keyword }}">
|
value="{{ title.keyword }}">
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
{% endfor %}
|
||||||
</datalist>
|
</datalist>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -28,6 +28,7 @@
|
|||||||
<tbody>
|
<tbody>
|
||||||
|
|
||||||
{% for folder in folders %}
|
{% for folder in folders %}
|
||||||
|
{% if folder.titles|length > 0 %}
|
||||||
<th colspan="5" id="{{ folder.name }}">{{ folder.name }}</th>
|
<th colspan="5" id="{{ folder.name }}">{{ folder.name }}</th>
|
||||||
|
|
||||||
{% for title in folder.titles %}
|
{% for title in folder.titles %}
|
||||||
@ -71,6 +72,7 @@
|
|||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
{% endif %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
||||||
</tbody>
|
</tbody>
|
||||||
|
Reference in New Issue
Block a user