Use a proper SelectField
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Michel Roux 2021-07-10 17:28:45 +02:00
parent ecdb4cc06e
commit a349ad9fc9
5 changed files with 39 additions and 32 deletions

View File

@ -151,25 +151,28 @@ def admin():
@mysql_required
@auth.login_required
def admin_edit(link_id=None):
if link_id:
link = AnimeLink.query.filter_by(id=link_id).first()
else:
link = utils.clean_model(AnimeLink())
link.title = utils.clean_model(AnimeTitle())
link.vf = False
folders = AnimeFolder.query.all()
form = EditForm(request.form)
form = EditForm(request.form, folder=link.title.folder.id, name=link.title.name, link=link.link, season=link.season,
keyword=link.title.keyword)
form.folder.choices = [('', '')] + [(g.id, g.name) for g in folders]
if form.validate_on_submit():
# Instance for VF tag
instance = get_instance(form.link.data)
# Folder
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)
db.session.commit()
# Title
link = AnimeLink.query.filter_by(id=form.id.data).first()
link = link if link else AnimeLink()
title = AnimeTitle.query.filter_by(id=link.title_id).first()
title = title if title else AnimeTitle.query.filter_by(name=form.name.data).first()
title = title if title else AnimeTitle()
title.folder_id = folder.id
title.folder_id = form.folder.data
title.name = form.name.data
title.keyword = form.keyword.data.lower() if form.keyword.data else title.keyword
db.session.add(title)
@ -185,14 +188,6 @@ def admin_edit(link_id=None):
remove_garbage(link)
return redirect(url_for('admin'))
if link_id:
link = AnimeLink.query.filter_by(id=link_id).first()
form.folder.data = link.title.folder.id
else:
link = utils.clean_model(AnimeLink())
link.title = utils.clean_model(AnimeTitle())
link.vf = False
return render_template('admin/edit.html', search_form=SearchForm(), link=link, folders=folders, action_form=form)

View File

@ -48,6 +48,9 @@ if db_host:
'pool_recycle': 200
}
db = SQLAlchemy(app)
from .models import *
create_all()
cache_host = environ.get('REDIS_SERVER')
if cache_host:

View File

@ -1,5 +1,5 @@
from flask_wtf import FlaskForm
from wtforms import HiddenField, StringField
from wtforms import HiddenField, StringField, SelectField
from wtforms.fields.html5 import SearchField, URLField
from wtforms.validators import DataRequired
@ -17,9 +17,22 @@ class DeleteForm(FlaskForm):
class EditForm(FlaskForm):
id = HiddenField('id')
folder = StringField('folder', validators=[DataRequired()])
folder = SelectField('folder', validators=[DataRequired()])
name = StringField('name', validators=[DataRequired()])
link = URLField('link', validators=[DataRequired()])
season = StringField('season', validators=[DataRequired()])
comment = StringField('comment')
keyword = StringField('keyword', validators=[DataRequired()])
class FolderEditForm(FlaskForm):
id = HiddenField('id')
name = StringField('name', validators=[DataRequired()])
path = StringField('path', validators=[DataRequired()])
class FolderDeleteForm(FlaskForm):
class Meta:
csrf = False
id = HiddenField('id', validators=[DataRequired()])

View File

@ -4,6 +4,7 @@ from .config import db
class AnimeFolder(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(length=100, collation='utf8mb4_general_ci'), unique=True, nullable=False)
path = db.Column(db.String(length=100, collation='utf8mb4_general_ci'))
titles = db.relationship("AnimeTitle", backref="folder")
@ -24,4 +25,5 @@ class AnimeLink(db.Model):
title_id = db.Column(db.Integer, db.ForeignKey('anime_title.id'))
def create_all():
db.create_all()

View File

@ -10,13 +10,7 @@
<div class="field column">
<div class="control is-expanded">
<div class="select is-fullwidth">
{{ 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>
{{ action_form.folder(class='input') }}
</div>
</div>
</div>
@ -24,12 +18,12 @@
<div class="field column is-6">
<div class="control is-expanded">
<div class="select is-fullwidth">
{{ action_form.name(value=link.title.name, list='animes', class='input', placeholder='Name') }}
{{ action_form.name(list='animes', class='input', placeholder='Name') }}
<datalist id="animes">
{% for folder in folders %}
{% for title in folder.titles %}
<option {{ 'selected' if title.id == link.title.id }}
data-folder="{{ title.folder.name }}" value="{{ title.name }}"
data-folder="{{ title.folder.id }}" value="{{ title.name }}"
data-keyword="{{ title.keyword }}">
{% endfor %}
{% endfor %}
@ -55,13 +49,13 @@
<div class="field-body">
<div class="field column is-6">
<div class="control is-expanded">
{{ action_form.link(value=link.link, class='input', placeholder='Link') }}
{{ action_form.link(class='input', placeholder='Link') }}
</div>
</div>
<div class="field column">
<div class="control is-expanded">
{{ action_form.season(value=link.season, class='input', placeholder='Season') }}
{{ action_form.season(class='input', placeholder='Season') }}
</div>
</div>
</div>
@ -71,14 +65,14 @@
<div class="field-body">
<div class="field column is-6">
<div class="control is-expanded">
{{ action_form.comment(value=link.comment, class='input', placeholder='Comment') }}
{{ action_form.comment(class='input', placeholder='Comment') }}
</div>
</div>
<div class="field column">
<div class="control is-expanded">
<div class="select is-fullwidth">
{{ action_form.keyword(value=link.title.keyword, list='keywords', class='input', placeholder='Keyword') }}
{{ action_form.keyword(list='keywords', class='input', placeholder='Keyword') }}
<datalist id="keywords">
{% for folder in folders %}
{% for title in folder.titles %}