2024-06-02 22:37:55 +02:00
|
|
|
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
|
2024-06-17 22:36:23 +02:00
|
|
|
from distribusikan.distribusis_info import DistribusisInfo
|
2024-06-02 22:37:55 +02:00
|
|
|
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):
|
2024-06-17 22:36:23 +02:00
|
|
|
distribusis = DistribusisInfo.get_user_distribusis(user.email)
|
2024-06-02 22:37:55 +02:00
|
|
|
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):
|
2024-06-17 22:36:23 +02:00
|
|
|
distribusis = DistribusisInfo.visible_distribusis()
|
2024-06-02 22:37:55 +02:00
|
|
|
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
|