|
@ -1,9 +1,7 @@ |
|
|
import os |
|
|
import os |
|
|
import bleach |
|
|
|
|
|
import shutil |
|
|
import shutil |
|
|
from flask import render_template |
|
|
from flask import render_template |
|
|
|
|
|
|
|
|
from werkzeug.utils import secure_filename |
|
|
|
|
|
from sqlalchemy.exc import ( |
|
|
from sqlalchemy.exc import ( |
|
|
DataError, |
|
|
DataError, |
|
|
DatabaseError, |
|
|
DatabaseError, |
|
@ -12,11 +10,7 @@ from sqlalchemy.exc import ( |
|
|
) |
|
|
) |
|
|
from app import db |
|
|
from app import db |
|
|
|
|
|
|
|
|
from statuspengguna.helper import ( |
|
|
from usermodel import User |
|
|
IsZipUploaded, |
|
|
|
|
|
CurrentDistribusi, |
|
|
|
|
|
IsDistribusiLive, |
|
|
|
|
|
) |
|
|
|
|
|
from distribusimodel import Distribusis |
|
|
from distribusimodel import Distribusis |
|
|
from distribusisinfo import DistribusisInfo |
|
|
from distribusisinfo import DistribusisInfo |
|
|
|
|
|
|
|
@ -25,11 +19,106 @@ from forms.admindistribusiform import AdminDistribusiForm |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def AdminPage(): |
|
|
def AdminPage(): |
|
|
adminuserform = AdminUserForm() |
|
|
adminuserform = AddUsersToForm(AdminUserForm()) |
|
|
admindistribusiform = AdminDistribusiForm() |
|
|
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: |
|
|
|
|
|
MakeUsersTutors(adminuserform) |
|
|
|
|
|
|
|
|
template = render_template( |
|
|
template = render_template( |
|
|
"admin.html", |
|
|
"admin.html", |
|
|
adminuserform=adminuserform, |
|
|
adminuserform=adminuserform, |
|
|
admindistribusiform=admindistribusiform, |
|
|
admindistribusiform=admindistribusiform, |
|
|
) |
|
|
) |
|
|
return template |
|
|
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 MakeUsersTutors(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() |
|
|
|
|
|
MakeUserTutorinDb(user) |
|
|
|
|
|
userform.errors.append(f"User {useremail} is now a tutor!") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def MakeUserTutorinDb(user): |
|
|
|
|
|
try: |
|
|
|
|
|
user.tutor = True |
|
|
|
|
|
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) |
|
|
|
|
|
shutil.rmtree(userfolder) |
|
|
|
|
|
cssfolder = os.path.join("themes/userthemes", distribusiname) |
|
|
|
|
|
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 |
|
|