distribusi-verse: medium-tech web app content management system for the web
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.6 KiB

import os
import shutil
from flask import render_template, Blueprint
from flask_login import login_required
from sqlalchemy.exc import (
DatabaseError,
DataError,
InterfaceError,
InvalidRequestError,
)
from app import db
from admin import is_adminuser
from distribusikan.distribusis_info 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.get_user_distribusis(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.visible_distribusis()
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