mirror of
https://codeberg.org/Mo8it/AdvLabDB.git
synced 2024-12-04 22:40:30 +00:00
Add deactivate assistants action
This commit is contained in:
parent
4747f8a073
commit
ffcbda905d
3 changed files with 71 additions and 10 deletions
43
advlabdb/actions.py
Normal file
43
advlabdb/actions.py
Normal file
|
@ -0,0 +1,43 @@
|
|||
from flask import flash
|
||||
from flask_login import current_user
|
||||
from sqlalchemy import select
|
||||
|
||||
from .models import Assistant, Semester, User, db
|
||||
|
||||
|
||||
def update_final_experiment_and_part_marks():
|
||||
for semesterExperiment in current_user.active_semester.semester_experiments:
|
||||
semesterExperiment.updateFinalExperimentAndPartMarks()
|
||||
|
||||
flash("Manually updated all final experiment and part marks.", "success")
|
||||
|
||||
|
||||
def deactivate_assistants():
|
||||
user_ids_to_deactivate = db.session.scalars(
|
||||
select(Assistant.user_id)
|
||||
.join(User)
|
||||
.where(User.active == True)
|
||||
.except_(
|
||||
select(Assistant.user_id).join(Assistant.semester_experiments).join(Semester).where(Semester.done == False)
|
||||
)
|
||||
)
|
||||
|
||||
no_users_deactivated = True
|
||||
|
||||
try:
|
||||
for user_id in user_ids_to_deactivate:
|
||||
user = db.session.get(User, user_id)
|
||||
user.active = False
|
||||
|
||||
flash(f"User {user} deactivated!", "warning")
|
||||
no_users_deactivated = False
|
||||
|
||||
db.session.commit()
|
||||
except Exception as ex:
|
||||
flash(str(ex), "danger")
|
||||
|
||||
db.session.rollback()
|
||||
no_users_deactivated = True
|
||||
|
||||
if no_users_deactivated:
|
||||
flash("No users to deactivate.", "info")
|
|
@ -32,6 +32,7 @@ from wtforms.validators import URL, DataRequired, Email, NumberRange, Optional
|
|||
from wtforms.widgets import NumberInput
|
||||
|
||||
from . import data_dir, user_datastore
|
||||
from .actions import deactivate_assistants, update_final_experiment_and_part_marks
|
||||
from .admin_link_formatters import (
|
||||
admin_formatter,
|
||||
appointment_date_formatter,
|
||||
|
@ -1343,8 +1344,15 @@ class ImportView(SecureAdminBaseView):
|
|||
|
||||
class ActionsView(SecureAdminBaseView):
|
||||
class ActionsForm(FlaskForm):
|
||||
manualUpdateFinalExperimentAndPartMarksSubmit = SubmitField(
|
||||
label="Manually update final experiment and part marks",
|
||||
update_final_experiment_and_part_marks = BooleanField(
|
||||
label="Manually update all final experiment and part marks in the active semester",
|
||||
)
|
||||
deactivate_assistants = BooleanField(
|
||||
label="Deactivate assistants that do not have experiments in a semester that is not set as done",
|
||||
)
|
||||
submit = SubmitField(
|
||||
label="Submit",
|
||||
render_kw={"class": "btn btn-primary btn-block"},
|
||||
)
|
||||
|
||||
@expose("/", methods=("GET", "POST"))
|
||||
|
@ -1352,13 +1360,12 @@ class ActionsView(SecureAdminBaseView):
|
|||
form = ActionsView.ActionsForm()
|
||||
|
||||
if form.validate_on_submit():
|
||||
if form.manualUpdateFinalExperimentAndPartMarksSubmit.data:
|
||||
for semesterExperiment in current_user.active_semester.semester_experiments:
|
||||
semesterExperiment.updateFinalExperimentAndPartMarks()
|
||||
if form.update_final_experiment_and_part_marks.data:
|
||||
update_final_experiment_and_part_marks()
|
||||
if form.deactivate_assistants.data:
|
||||
deactivate_assistants()
|
||||
|
||||
flash("Manually updated all final experiment and part marks", "success")
|
||||
|
||||
return redirect(url_for("main.index"))
|
||||
return redirect(self.url)
|
||||
|
||||
return self.render("actions.jinja.html", form=form)
|
||||
|
||||
|
|
|
@ -7,8 +7,19 @@
|
|||
<hr>
|
||||
|
||||
<form method="POST">
|
||||
{{ form.csrf_token }}
|
||||
{{ form.manualUpdateFinalExperimentAndPartMarksSubmit }}
|
||||
{% for field in form %}
|
||||
{% if field.widget.input_type == "checkbox" %}
|
||||
<div class="form-check">
|
||||
{{ field(class="form-check-input") }}
|
||||
|
||||
<label class="form-check-label" for="{{ field.id }}">
|
||||
{{ field.label }}
|
||||
</label>
|
||||
</div>
|
||||
{% else %}
|
||||
{{ field() }}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
</form>
|
||||
|
||||
{{ footer|safe }}
|
||||
|
|
Loading…
Reference in a new issue