1
0
Fork 0
mirror of https://codeberg.org/Mo8it/AdvLabDB.git synced 2024-11-08 21:21:06 +00:00
AdvLabDB/advlabdb/actions.py

68 lines
1.9 KiB
Python
Raw Normal View History

2023-11-02 17:47:52 +00:00
import subprocess
2023-11-01 22:18:47 +00:00
from datetime import datetime
2023-11-02 17:47:52 +00:00
2022-09-24 14:16:48 +00:00
from flask import flash
from flask_login import current_user
from sqlalchemy import select
2023-11-01 22:18:47 +00:00
from . import data_dir
2022-09-24 14:16:48 +00:00
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")
2023-11-02 17:47:46 +00:00
2023-11-01 22:18:47 +00:00
def backup(backup_prefix):
db_path = data_dir / "db/advlabdb.sqlite"
db_bk_dir = db_path.parent / "backups"
db_bk_dir.mkdir(exist_ok=True)
now = datetime.now().strftime("%d_%m_%Y_%H_%M_%S")
dest = db_bk_dir / f"{backup_prefix}_{now}.sqlite"
2023-11-02 19:04:09 +00:00
status = subprocess.run(["sqlite3", db_path, f".backup {dest}"], check=False).returncode
2023-11-01 22:18:47 +00:00
if status == 0:
flash(f"Created a database backup at the path {dest}", "success")
else:
flash("Failed to create a database backup! Make sure that `sqlite3` is installed on your system!", "danger")
2023-11-02 17:47:46 +00:00
2023-11-01 22:18:47 +00:00
def manual_backup():
backup("manual")
2022-09-24 14:16:48 +00:00
2023-11-02 17:47:46 +00:00
2022-09-24 14:16:48 +00:00
def deactivate_assistants():
user_ids_to_deactivate = db.session.scalars(
select(Assistant.user_id)
.join(User)
2023-11-02 17:09:10 +00:00
.where(User.active is True)
2022-09-24 14:16:48 +00:00
.except_(
2023-11-02 17:09:10 +00:00
select(Assistant.user_id).join(Assistant.semester_experiments).join(Semester).where(Semester.done is False)
2022-09-24 14:16:48 +00:00
)
)
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")