crunk
6 months ago
7 changed files with 176 additions and 17 deletions
@ -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 |
@ -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 |
@ -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() |
@ -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") |
Loading…
Reference in new issue