From a349ad9fc9619cda5741960c0d3a9270fcce040e Mon Sep 17 00:00:00 2001 From: Michel Roux Date: Sat, 10 Jul 2021 17:28:45 +0200 Subject: [PATCH] Use a proper SelectField --- pynyaata/__init__.py | 27 +++++++++++---------------- pynyaata/config.py | 3 +++ pynyaata/forms.py | 17 +++++++++++++++-- pynyaata/models.py | 4 +++- pynyaata/templates/admin/edit.html | 20 +++++++------------- 5 files changed, 39 insertions(+), 32 deletions(-) diff --git a/pynyaata/__init__.py b/pynyaata/__init__.py index 0d84806..81e4d0c 100644 --- a/pynyaata/__init__.py +++ b/pynyaata/__init__.py @@ -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) diff --git a/pynyaata/config.py b/pynyaata/config.py index a291611..f70b6b2 100644 --- a/pynyaata/config.py +++ b/pynyaata/config.py @@ -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: diff --git a/pynyaata/forms.py b/pynyaata/forms.py index 7048985..1b7b30b 100644 --- a/pynyaata/forms.py +++ b/pynyaata/forms.py @@ -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()]) diff --git a/pynyaata/models.py b/pynyaata/models.py index 3841498..9fcdc5d 100644 --- a/pynyaata/models.py +++ b/pynyaata/models.py @@ -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')) -db.create_all() +def create_all(): + db.create_all() diff --git a/pynyaata/templates/admin/edit.html b/pynyaata/templates/admin/edit.html index 360156b..605fa8c 100644 --- a/pynyaata/templates/admin/edit.html +++ b/pynyaata/templates/admin/edit.html @@ -10,13 +10,7 @@
- {{ action_form.folder(value=link.title.folder.name, list='folders', class='input', placeholder='Folder') }} - - {% for folder in folders %} - + {{ action_form.folder(class='input') }}
@@ -24,12 +18,12 @@
- {{ action_form.name(value=link.title.name, list='animes', class='input', placeholder='Name') }} + {{ action_form.name(list='animes', class='input', placeholder='Name') }} {% for folder in folders %} {% for title in folder.titles %}