refactor: no more main forms folder

This commit is contained in:
crunk 2024-06-02 22:37:55 +02:00
parent 280ce2f196
commit 102032bd6e
7 changed files with 176 additions and 17 deletions

8
verse/admin.py Normal file
View File

@ -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

View File

@ -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

View File

@ -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()

View File

@ -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")

View File

@ -13,7 +13,8 @@ from flask import (
from flask_login import current_user, login_required, logout_user from flask_login import current_user, login_required, logout_user
from flask_wtf.csrf import CSRFError 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 app import create_app, login_manager, db
from describer.describe_files import describer from describer.describe_files import describer
from distribusikan.distribusikan import distribusikan 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(login_section, url_prefix="/login")
APP.register_blueprint(register_user, url_prefix="/register") APP.register_blueprint(register_user, url_prefix="/register")
APP.register_blueprint(forgot_password, url_prefix="/login/forgotpassword") APP.register_blueprint(forgot_password, url_prefix="/login/forgotpassword")
APP.register_blueprint (admin_page, url_prefix="/admin")
APP.register_blueprint(distribusikan) APP.register_blueprint(distribusikan)
@ -90,14 +92,6 @@ def shortstashurl():
return redirect(url_for("index")) 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") @APP.route("/logout")
@login_required @login_required
def logout(): def logout():
@ -115,13 +109,6 @@ def load_user(user_id):
return db.session.get(User, int(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__": if __name__ == "__main__":
APP.debug = True APP.debug = True
APP.run(port=5000) APP.run(port=5000)

View File

@ -11,7 +11,7 @@ from sqlalchemy.exc import (
) )
from app import db, get_app from app import db, get_app
from forms.forgotpasswordform import ForgotPasswordForm from statuspengguna.forms.forgotpasswordform import ForgotPasswordForm
from models.user_model import User from models.user_model import User
mail = Mail(get_app()) mail = Mail(get_app())