129 lines
3.9 KiB
Python
129 lines
3.9 KiB
Python
import os
|
|
import shutil
|
|
from flask import render_template
|
|
|
|
from sqlalchemy.exc import (
|
|
DataError,
|
|
DatabaseError,
|
|
InterfaceError,
|
|
InvalidRequestError,
|
|
)
|
|
from app import db
|
|
|
|
from usermodel import User
|
|
from distribusimodel import Distribusis
|
|
from distribusisinfo import DistribusisInfo
|
|
|
|
from forms.adminuserform import AdminUserForm
|
|
from forms.admindistribusiform import AdminDistribusiForm
|
|
|
|
|
|
def AdminPage():
|
|
adminuserform = AddUsersToForm(AdminUserForm())
|
|
admindistribusiform = AddDistribusisToForm(AdminDistribusiForm())
|
|
if admindistribusiform.validate_on_submit():
|
|
DeleteDistribusis(admindistribusiform)
|
|
|
|
if adminuserform.validate_on_submit():
|
|
if adminuserform.delete.data:
|
|
DeleteUsers(adminuserform)
|
|
if adminuserform.tutors.data:
|
|
ToggleUsersAsTutors(adminuserform, True)
|
|
if adminuserform.nottutors.data:
|
|
ToggleUsersAsTutors(adminuserform, False)
|
|
|
|
template = render_template(
|
|
"admin.html",
|
|
adminuserform=adminuserform,
|
|
admindistribusiform=admindistribusiform,
|
|
)
|
|
return template
|
|
|
|
|
|
def DeleteUsers(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()
|
|
DeleteUserDistribusis(user)
|
|
DeleteUserFromDb(user)
|
|
userform.errors.append(f"User {useremail} deleted!")
|
|
|
|
|
|
def ToggleUsersAsTutors(adminuserform, is_tutor):
|
|
for userform in adminuserform:
|
|
if "user" in userform.id:
|
|
if userform.data:
|
|
useremail = userform.label.text
|
|
user = User.query.filter_by(email=useremail).first()
|
|
ToggleUserTutorinDb(user, is_tutor)
|
|
userform.errors.append(f"Is User {useremail} tutor {is_tutor}")
|
|
|
|
|
|
def ToggleUserTutorinDb(user, is_tutor):
|
|
try:
|
|
user.tutor = is_tutor
|
|
db.session.commit()
|
|
except (InvalidRequestError, DataError, InterfaceError, DatabaseError):
|
|
db.session.rollback()
|
|
|
|
|
|
def DeleteUserFromDb(user):
|
|
try:
|
|
db.session.delete(user)
|
|
db.session.commit()
|
|
except (InvalidRequestError, DataError, InterfaceError, DatabaseError):
|
|
db.session.rollback()
|
|
|
|
|
|
def DeleteUserDistribusis(user):
|
|
distribusis = DistribusisInfo.getuserdistribusis(user.email)
|
|
for distribusi in distribusis:
|
|
DeleteDistribusiFiles(distribusi.distribusiname)
|
|
DeleteDistribusiFromDb(distribusi)
|
|
|
|
|
|
def DeleteDistribusis(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()
|
|
DeleteDistribusiFromDb(distribusi)
|
|
DeleteDistribusiFiles(distribusiname)
|
|
distribusiform.errors.append("Deleted distribusi")
|
|
|
|
|
|
def DeleteDistribusiFromDb(distribusi):
|
|
try:
|
|
db.session.delete(distribusi)
|
|
db.session.commit()
|
|
except (InvalidRequestError, DataError, InterfaceError, DatabaseError):
|
|
db.session.rollback()
|
|
|
|
|
|
def DeleteDistribusiFiles(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 AddDistribusisToForm(admindistribusiform):
|
|
distribusis = DistribusisInfo.visibledistribusis()
|
|
admindistribusiform = AdminDistribusiForm.distribusi_list_form_builder(
|
|
distribusis
|
|
)
|
|
return admindistribusiform
|
|
|
|
|
|
def AddUsersToForm(adminuserform):
|
|
users = User.query.all()
|
|
adminuserform = AdminUserForm.user_list_form_builder(users)
|
|
return adminuserform
|