admin page for bulk delete and making users into tutors

This commit is contained in:
crunk 2022-03-20 19:23:39 +01:00
parent 847f4b234d
commit 2b082165c3
7 changed files with 183 additions and 42 deletions

View File

@ -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

View File

@ -20,7 +20,17 @@ class DistribusisInfo:
for distribusi in distribusis: for distribusi in distribusis:
publictheme = ( publictheme = (
f"{distribusi.distribusiname}/{distribusi.publictheme}", f"{distribusi.distribusiname}/{distribusi.publictheme}",
f"{distribusi.publictheme} used in {distribusi.distribusiname}", f"{distribusi.publictheme} used in {distribusi.distribusiname}", # noqa: E501
) )
publicthemes.append(publictheme) publicthemes.append(publictheme)
return publicthemes return publicthemes
def visibledistribusis():
distribusis = Distribusis.query.filter(
Distribusis.visible.isnot(False)
).all()
return distribusis
def getuserdistribusis(useremail):
user = User.query.filter_by(email=useremail).first()
return Distribusis.query.filter_by(userid=user.id).all()

View File

@ -1,7 +1,7 @@
"""Form object declaration.""" """Form object declaration."""
from flask_wtf import FlaskForm from flask_wtf import FlaskForm
from wtforms import ( from wtforms import (
RadioField, BooleanField,
SubmitField, SubmitField,
) )
@ -10,3 +10,16 @@ class AdminDistribusiForm(FlaskForm):
"""Admin Distribusi form.""" """Admin Distribusi form."""
delete = SubmitField("Delete") 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()

View File

@ -1,13 +1,27 @@
"""Form object declaration.""" """Form object declaration."""
from flask_wtf import FlaskForm from flask_wtf import FlaskForm
from wtforms import ( from wtforms import (
RadioField,
SubmitField, SubmitField,
BooleanField,
) )
class AdminUserForm(FlaskForm): class AdminUserForm(FlaskForm):
"""Admin Userform form.""" """Admin Userform form."""
update = SubmitField("update")
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()
tutors = SubmitField("Are Tutors")
delete = SubmitField("Delete") delete = SubmitField("Delete")

View File

@ -73,9 +73,7 @@ def session_handler():
def index(): def index():
ResetUserState() ResetUserState()
uploadform = UploadForm() uploadform = UploadForm()
distribusis = Distribusis.query.filter( distribusis = DistribusisInfo.visibledistribusis()
Distribusis.visible.isnot(False)
).all()
distribusies = {} distribusies = {}
for distribusi in distribusis: for distribusi in distribusis:
user = User.query.filter_by(id=distribusi.userid).first() user = User.query.filter_by(id=distribusi.userid).first()
@ -275,7 +273,7 @@ def distribusistash(path):
return send_from_directory("stash", path) return send_from_directory("stash", path)
@APP.route("/admin") @APP.route("/admin", methods=["GET", "POST"])
@login_required @login_required
def admin(): def admin():
return AdminPage() return AdminPage()

View File

@ -137,6 +137,11 @@ input[type="submit"]:disabled:focus {
background-color: #62b264; background-color: #62b264;
} }
#tutors {
color: black;
background-color: #62b264;
}
/* unvisited link */ /* unvisited link */
a:link { a:link {

View File

@ -1,21 +1,9 @@
{% extends "base.html" %} {% extends "base.html" %}
{% block main %} {% block main %}
<div id="buttons"> <div id="buttons">
{% if not current_user.is_authenticated %} <div class="overview">
<div class="signin"> <a href="/">
<a href="/login"> <input type="button" name="button" value="Overview"></input>
<input type="button" name="button" value="Sign in"></input>
</a>
</div>
<div class="signin">
<a href="/register">
<input type="button" name="button" value="Register"></input>
</a>
</div>
{% else %}
<div class="distribusi">
<a href="/distribusi">
<input type="button" name="button" value="Distribusi"></input>
</a> </a>
</div> </div>
<div class="logout"> <div class="logout">
@ -23,28 +11,52 @@
<input type="button" name="button" value="Logout"></input> <input type="button" name="button" value="Logout"></input>
</a> </a>
</div> </div>
{% endif %}
</div> </div>
<div class="maincontent"> <div class="maincontent">
<!-- a div with all the distribusis listed in the distribusiverse --> <h2>Admin Page</h2>
<p>Here you can bulk delete distribusis and users or make users into tutors
<strong> These actions cannot be undone! </strong>
</p>
<div id="distribusiverse" class="maincontent"> <div id="distribusiverse" class="maincontent">
<h2>List of distribusis</h2> <h2>List of distribusis</h2>
<form method="POST" enctype="multipart/form-data" action="{{ url_for('theme') }}"> <form method="POST" enctype="multipart/form-data" action="{{ url_for('admin') }}">
{{ admindistribusiform.csrf_token }} {{ admindistribusiform.csrf_token }}
<fieldset class="button required multiselect delete"> {% for distribusiform in admindistribusiform %}
{{ admindistribusiform.delete }} {% if "distribusi" in distribusiform.id %}
</fieldset> <fieldset class="required">
{{ distribusiform }}
{{ distribusiform.label }}
{% for message in distribusiform.errors %}
<div class="error">{{ message }}</div>
{% endfor %}
</fieldset>
{% endif %}
{% endfor %}
<fieldset class="button required delete">
{{ admindistribusiform.delete }}
</fieldset>
</form> </form>
</div> </div>
<div id="distribusiverse" class="maincontent"> <div id="distribusiverse" class="maincontent">
<h2>List of users</h2> <h2>List of users</h2>
<form method="POST" enctype="multipart/form-data" action="{{ url_for('theme') }}"> <form method="POST" enctype="multipart/form-data" action="{{ url_for('admin') }}">
{{ adminuserform.csrf_token }} {{ adminuserform.csrf_token }}
<fieldset class="button required multiselect update"> {% for userform in adminuserform %}
{{ adminuserform.update }} {% if "user" in userform.id %}
<fieldset class="required">
{{ userform }}
{{ userform.label }}
{% for message in userform.errors %}
<div class="error">{{ message }}</div>
{% endfor %}
</fieldset>
{% endif %}
{% endfor %}
<fieldset class="button required">
{{ adminuserform.tutors }}
</fieldset> </fieldset>
<fieldset class="button required multiselect delete"> <fieldset class="button required">
{{ adminuserform.delete }} {{ adminuserform.delete }}
</fieldset> </fieldset>
</form> </form>