From 4e90a95a0e8a0319682dee65579b546d148c68f2 Mon Sep 17 00:00:00 2001 From: Michel Roux Date: Sat, 10 Jul 2021 20:59:36 +0200 Subject: [PATCH] Add folders management --- pynyaata/__init__.py | 67 ++++++++++++++++++++--- pynyaata/models.py | 4 +- pynyaata/static/css/styles.css | 6 ++ pynyaata/templates/admin/edit.html | 7 +-- pynyaata/templates/admin/folder/edit.html | 33 +++++++++++ pynyaata/templates/admin/folder/list.html | 42 ++++++++++++++ pynyaata/templates/admin/list.html | 4 ++ 7 files changed, 148 insertions(+), 15 deletions(-) create mode 100644 pynyaata/templates/admin/folder/edit.html create mode 100644 pynyaata/templates/admin/folder/list.html diff --git a/pynyaata/__init__.py b/pynyaata/__init__.py index 06022b3..5d40ff1 100644 --- a/pynyaata/__init__.py +++ b/pynyaata/__init__.py @@ -11,7 +11,7 @@ from .config import app, auth, logger, scheduler, ADMIN_USERNAME, ADMIN_PASSWORD CLOUDPROXY_ENDPOINT from .connectors import get_instance, run_all from .connectors.core import ConnectorLang, ConnectorReturn -from .forms import SearchForm, DeleteForm, EditForm +from .forms import SearchForm, DeleteForm, EditForm, FolderDeleteForm, FolderEditForm if MYSQL_ENABLED: from .config import db @@ -146,6 +146,50 @@ def admin(): return render_template('admin/list.html', search_form=SearchForm(), folders=folders, action_form=form) +@app.route('/admin/folder', methods=['GET', 'POST']) +@mysql_required +@auth.login_required +def folder_list(): + form = FolderDeleteForm(request.form) + + if form.validate_on_submit(): + folder = AnimeFolder.query.filter_by(id=form.id.data).first() + if folder: + form.message = '%s has been successfully deleted' % folder.name + db.session.delete(folder) + db.session.commit() + else: + form._errors = {'id': ['Id %s was not found in the database' % form.id.data]} + + folders = AnimeFolder.query.all() + + return render_template('admin/folder/list.html', search_form=SearchForm(), folders=folders, action_form=form) + + +@app.route('/admin/folder/edit', methods=['GET', 'POST']) +@app.route('/admin/folder/edit/', methods=['GET', 'POST']) +@mysql_required +@auth.login_required +def folder_edit(folder_id=None): + if folder_id: + folder = AnimeFolder.query.filter_by(id=folder_id).first() + else: + folder = utils.clean_model(AnimeFolder()) + + form = FolderEditForm(request.form, id=folder.id, name=folder.name, path=folder.path) + + if form.validate_on_submit(): + # Folder + folder.id = folder_id + folder.name = form.name.data + folder.path = form.path.data + db.session.add(folder) + db.session.commit() + return redirect(url_for('folder_list')) + + return render_template('admin/folder/edit.html', search_form=SearchForm(), action_form=form) + + @app.route('/admin/edit', methods=['GET', 'POST']) @app.route('/admin/edit/', methods=['GET', 'POST']) @mysql_required @@ -160,25 +204,30 @@ def admin_edit(link_id=None): link.vf = False folders = AnimeFolder.query.all() - form = EditForm(request.form, folder=link.title.folder.id, name=link.title.name, link=link.link, season=link.season, - keyword=link.title.keyword) + form = EditForm( + request.form, + id=link.id, + 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) # 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 = AnimeTitle.query.filter_by(name=form.name.data).first() title = title if title else AnimeTitle() title.folder_id = form.folder.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() db.session.add(title) db.session.commit() # Link + link.id = link_id link.title_id = title.id link.link = form.link.data link.season = form.season.data @@ -189,7 +238,7 @@ def admin_edit(link_id=None): remove_garbage(link) return redirect(url_for('admin')) - return render_template('admin/edit.html', search_form=SearchForm(), link=link, folders=folders, action_form=form) + return render_template('admin/edit.html', search_form=SearchForm(), folders=folders, action_form=form) @scheduler.task('interval', id='flaredestroyy', days=1) diff --git a/pynyaata/models.py b/pynyaata/models.py index 9fcdc5d..654d728 100644 --- a/pynyaata/models.py +++ b/pynyaata/models.py @@ -5,7 +5,7 @@ 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") + titles = db.relationship("AnimeTitle", backref="folder", cascade='all,delete-orphan') class AnimeTitle(db.Model): @@ -13,7 +13,7 @@ class AnimeTitle(db.Model): name = db.Column(db.String(length=100, collation='utf8mb4_general_ci'), unique=True, nullable=False) keyword = db.Column(db.Text(collation='utf8mb4_general_ci'), nullable=False) folder_id = db.Column(db.Integer, db.ForeignKey('anime_folder.id')) - links = db.relationship('AnimeLink', backref="title") + links = db.relationship('AnimeLink', backref="title", cascade='all,delete-orphan') class AnimeLink(db.Model): diff --git a/pynyaata/static/css/styles.css b/pynyaata/static/css/styles.css index a0ed5a3..de1b818 100644 --- a/pynyaata/static/css/styles.css +++ b/pynyaata/static/css/styles.css @@ -121,4 +121,10 @@ label.checkbox { .table.is-hoverable tbody tr:not(.is-selected):hover { background-color: #333 !important; } + + .select > select { + border-color: #363636 !important; + background-color: #0a0a0a !important; + color: #dbdbdb !important; + } } diff --git a/pynyaata/templates/admin/edit.html b/pynyaata/templates/admin/edit.html index f73571f..62f04e4 100644 --- a/pynyaata/templates/admin/edit.html +++ b/pynyaata/templates/admin/edit.html @@ -1,9 +1,8 @@ {% extends "layout.html" %} -{% block title %}- Admin Edit {{ link.name }}{% endblock %} +{% block title %}- Admin Edit {{ action_form.name.data }}{% endblock %} {% block body %}
{{ action_form.csrf_token }} - {{ action_form.id(value=link.id) }}
@@ -22,7 +21,7 @@ {% for folder in folders %} {% for title in folder.titles %} -