Add + edit OK

This commit is contained in:
Michel Roux 2019-12-13 16:49:11 +01:00
parent 558e8170da
commit b4fae7b830
3 changed files with 18 additions and 18 deletions

29
app.py
View File

@ -125,18 +125,16 @@ def admin_delete():
@auth.login_required @auth.login_required
def admin_edit(link_id): def admin_edit(link_id):
link = AnimeLink.query.filter_by(id=link_id).first() link = AnimeLink.query.filter_by(id=link_id).first()
edit_form = EditForm()
edit_form.folder.choices = [(query.id, query.name) for query in AnimeFolder.query.all()]
titles = AnimeTitle.query.all() titles = AnimeTitle.query.all()
return render_template('admin/edit.html', search_form=SearchForm(), link=link, titles=titles, edit_form=edit_form) return render_template('admin/edit.html', search_form=SearchForm(), link=link, titles=titles, edit_form=EditForm())
@app.route('/admin/add') @app.route('/admin/add')
@auth.login_required @auth.login_required
def admin_add(): def admin_add():
edit_form = EditForm() edit_form = EditForm()
edit_form.folder.choices = [('', '')] + [(query.id, query.name) for query in AnimeFolder.query.all()] edit_form.folder.choices = [(0, '')] + edit_form.folder.choices
titles = AnimeTitle.query.all() titles = AnimeTitle.query.all()
link = AnimeLink() link = AnimeLink()
for attr in dir(link): for attr in dir(link):
@ -154,18 +152,19 @@ def admin_add():
def admin_save(): def admin_save():
form = EditForm(request.form) form = EditForm(request.form)
if form.validate_on_submit(): if form.validate_on_submit():
folder = AnimeFolder.query.filter_by(id=form.folder).first() title = AnimeTitle.query.filter_by(name=form.name.data).first()
title = AnimeTitle.query.filter_by(name=form.name).first() title = title if title else AnimeTitle()
title.folder = folder title.folder_id = form.folder.data
title.name = form.name title.name = form.name.data
title.keyword = form.keyword.lower() if form.keyword else title.keyword title.keyword = form.keyword.data.lower() if form.keyword.data else title.keyword
db.session.add(title) db.session.add(title)
link = AnimeLink.query.filter_by(id=form.id) link = AnimeLink.query.filter_by(id=form.id.data).first()
link.title = title link = link if link else AnimeLink()
link.link = form.link link.title_id = title.id
link.season = form.season link.link = form.link.data
link.comment = form.comment link.season = form.season.data
link.vf = form.is_vf link.comment = form.comment.data
link.vf = form.is_vf.data
db.session.add(link) db.session.add(link)
db.session.commit() db.session.commit()
else: else:

View File

@ -41,8 +41,9 @@ class DeleteForm(FlaskForm):
class EditForm(FlaskForm): class EditForm(FlaskForm):
id = HiddenField('id', validators=[DataRequired()]) id = HiddenField('id')
folder = SelectField('folder', validators=[DataRequired()]) folder = SelectField('folder', validators=[DataRequired()],
choices=[(query.id, query.name) for query in AnimeFolder.query.all()], coerce=int)
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

@ -92,7 +92,7 @@
<div class="field-body"> <div class="field-body">
<div class="field column"> <div class="field column">
<div class="control is-expanded"> <div class="control is-expanded">
<input class="button is-info" type="submit" onclick="this.disabled=true;this.form.submit()"> <input class="button is-info" type="submit">
</div> </div>
</div> </div>
</div> </div>