From 102032bd6eb10301d28cba4d5ec99471db11178d Mon Sep 17 00:00:00 2001 From: crunk Date: Sun, 2 Jun 2024 22:37:55 +0200 Subject: [PATCH] refactor: no more main forms folder --- verse/admin.py | 8 ++ verse/admin_page/admin_page.py | 118 ++++++++++++++++++ verse/admin_page/forms/admindistribusiform.py | 23 ++++ verse/admin_page/forms/adminuserform.py | 23 ++++ .../base => admin_page/templates}/admin.html | 0 verse/start.py | 19 +-- verse/statuspengguna/forgotpassword.py | 2 +- 7 files changed, 176 insertions(+), 17 deletions(-) create mode 100644 verse/admin.py create mode 100644 verse/admin_page/admin_page.py create mode 100644 verse/admin_page/forms/admindistribusiform.py create mode 100644 verse/admin_page/forms/adminuserform.py rename verse/{templates/base => admin_page/templates}/admin.html (100%) diff --git a/verse/admin.py b/verse/admin.py new file mode 100644 index 0000000..834af10 --- /dev/null +++ b/verse/admin.py @@ -0,0 +1,8 @@ +from flask_login import current_user +from models.user_model import User + +def is_adminuser(): + if not current_user.is_authenticated: + return False + user = User.query.filter_by(email=current_user.email).first() + return user.admin diff --git a/verse/admin_page/admin_page.py b/verse/admin_page/admin_page.py new file mode 100644 index 0000000..3857cfe --- /dev/null +++ b/verse/admin_page/admin_page.py @@ -0,0 +1,118 @@ +import os +import shutil + +from flask import render_template, Blueprint +from flask_login import current_user, login_required +from sqlalchemy.exc import ( + DatabaseError, + DataError, + InterfaceError, + InvalidRequestError, +) + +from app import db +from admin import is_adminuser +from distribusikan.distribusisinfo import DistribusisInfo +from admin_page.forms.admindistribusiform import AdminDistribusiForm +from admin_page.forms.adminuserform import AdminUserForm +from models.distribusi_model import Distribusis +from models.user_model import User + +admin_page = Blueprint( + "admin", + __name__, + template_folder="templates/describe_files", + static_folder="static", +) + + +@admin_page.route("/admin", methods=["GET", "POST"]) +@login_required +def admin(): + if not is_adminuser(): + return redirect(url_for("index")) + adminuserform = add_users_to_form(AdminUserForm()) + admindistribusiform = add_distribusis_to_form(AdminDistribusiForm()) + if admindistribusiform.validate_on_submit(): + delete_distribusis(admindistribusiform) + + if adminuserform.validate_on_submit(): + if adminuserform.delete.data: + delete_users(adminuserform) + + template = render_template( + "admin.html", + adminuserform=adminuserform, + admindistribusiform=admindistribusiform, + ) + return template + + +def delete_users(adminuserform): + for userform in adminuserform: + if "user" in userform.id: + if userform.data: + useremail = userform.label.text + user = User.query.filter_by(email=useremail).first() + delete_User_distribusis(user) + delete_user_from_db(user) + userform.errors.append(f"User {useremail} deleted!") + + +def delete_user_from_db(user): + try: + db.session.delete(user) + db.session.commit() + except (InvalidRequestError, DataError, InterfaceError, DatabaseError): + db.session.rollback() + + +def delete_User_distribusis(user): + distribusis = DistribusisInfo.getuserdistribusis(user.email) + for distribusi in distribusis: + delete_distribusi_files(distribusi.distribusiname) + delete_distribusi_from_db(distribusi) + + +def delete_distribusis(admindistribusiform): + for distribusiform in admindistribusiform: + if "distribusi" in distribusiform.id: + if distribusiform.data: + distribusiname = distribusiform.label.text + distribusi = Distribusis.query.filter_by( + distribusiname=distribusiname + ).first() + delete_distribusi_from_db(distribusi) + delete_distribusi_files(distribusiname) + distribusiform.errors.append("Deleted distribusi") + + +def delete_distribusi_from_db(distribusi): + try: + db.session.delete(distribusi) + db.session.commit() + except (InvalidRequestError, DataError, InterfaceError, DatabaseError): + db.session.rollback() + + +def delete_distribusi_files(distribusiname): + userfolder = os.path.join("stash", distribusiname) + if os.path.exists(userfolder): + shutil.rmtree(userfolder) + cssfolder = os.path.join("themes/userthemes", distribusiname) + if os.path.exists(cssfolder): + shutil.rmtree(cssfolder) + + +def add_distribusis_to_form(admindistribusiform): + distribusis = DistribusisInfo.visibledistribusis() + admindistribusiform = AdminDistribusiForm.distribusi_list_form_builder( + distribusis + ) + return admindistribusiform + + +def add_users_to_form(adminuserform): + users = User.query.all() + adminuserform = AdminUserForm.user_list_form_builder(users) + return adminuserform diff --git a/verse/admin_page/forms/admindistribusiform.py b/verse/admin_page/forms/admindistribusiform.py new file mode 100644 index 0000000..f00f3db --- /dev/null +++ b/verse/admin_page/forms/admindistribusiform.py @@ -0,0 +1,23 @@ +"""Form object declaration.""" + +from flask_wtf import FlaskForm +from wtforms import BooleanField, SubmitField + + +class AdminDistribusiForm(FlaskForm): + """Admin Distribusi form.""" + + delete = SubmitField("Delete") + + def distribusi_list_form_builder(distribusis): + class DistribusiListForm(AdminDistribusiForm): + pass + + for i, distribusi in enumerate(distribusis): + setattr( + DistribusiListForm, + f"distribusi_{i}", + BooleanField(label=distribusi.distribusiname), + ) + + return DistribusiListForm() diff --git a/verse/admin_page/forms/adminuserform.py b/verse/admin_page/forms/adminuserform.py new file mode 100644 index 0000000..aeda2e4 --- /dev/null +++ b/verse/admin_page/forms/adminuserform.py @@ -0,0 +1,23 @@ +"""Form object declaration.""" + +from flask_wtf import FlaskForm +from wtforms import BooleanField, SubmitField + + +class AdminUserForm(FlaskForm): + """Admin Userform form.""" + + def user_list_form_builder(users): + class UserListForm(AdminUserForm): + pass + + for i, user in enumerate(users): + setattr( + UserListForm, + f"user_{i}", + BooleanField(label=user.email), + ) + + return UserListForm() + + delete = SubmitField("Delete") diff --git a/verse/templates/base/admin.html b/verse/admin_page/templates/admin.html similarity index 100% rename from verse/templates/base/admin.html rename to verse/admin_page/templates/admin.html diff --git a/verse/start.py b/verse/start.py index da78a23..7deb7e5 100644 --- a/verse/start.py +++ b/verse/start.py @@ -13,7 +13,8 @@ from flask import ( from flask_login import current_user, login_required, logout_user from flask_wtf.csrf import CSRFError -from adminpage import AdminPage +from admin import is_adminuser +from admin_page.admin_page import admin_page from app import create_app, login_manager, db from describer.describe_files import describer from distribusikan.distribusikan import distribusikan @@ -33,6 +34,7 @@ APP.register_blueprint(describer, url_prefix="/describer") APP.register_blueprint(login_section, url_prefix="/login") APP.register_blueprint(register_user, url_prefix="/register") APP.register_blueprint(forgot_password, url_prefix="/login/forgotpassword") +APP.register_blueprint (admin_page, url_prefix="/admin") APP.register_blueprint(distribusikan) @@ -90,14 +92,6 @@ def shortstashurl(): return redirect(url_for("index")) -@APP.route("/admin", methods=["GET", "POST"]) -@login_required -def admin(): - if not is_adminuser(): - return redirect(url_for("index")) - return AdminPage() - - @APP.route("/logout") @login_required def logout(): @@ -115,13 +109,6 @@ def load_user(user_id): return db.session.get(User, int(user_id)) -def is_adminuser(): - if not current_user.is_authenticated: - return False - user = User.query.filter_by(email=current_user.email).first() - return user.admin - - if __name__ == "__main__": APP.debug = True APP.run(port=5000) diff --git a/verse/statuspengguna/forgotpassword.py b/verse/statuspengguna/forgotpassword.py index dbb8049..3137e1f 100644 --- a/verse/statuspengguna/forgotpassword.py +++ b/verse/statuspengguna/forgotpassword.py @@ -11,7 +11,7 @@ from sqlalchemy.exc import ( ) from app import db, get_app -from forms.forgotpasswordform import ForgotPasswordForm +from statuspengguna.forms.forgotpasswordform import ForgotPasswordForm from models.user_model import User mail = Mail(get_app())