1
0
Fork 0
mirror of https://codeberg.org/Mo8it/AdvLabDB.git synced 2024-12-20 23:41:20 +00:00

Move Semester functions

This commit is contained in:
Mo 2022-05-29 21:47:23 +02:00
parent 1141d1dfb9
commit 6f22253a49
4 changed files with 20 additions and 26 deletions

View file

@ -39,11 +39,7 @@ from .customClasses import (
) )
from .database_import import importFromFile from .database_import import importFromFile
from .exceptions import DataBaseException, ModelViewException from .exceptions import DataBaseException, ModelViewException
from .model_dependent_funs import ( from .model_dependent_funs import initActiveSemesterMenuLinks
initActiveSemesterMenuLinks,
lastSemester,
sortedSemestersStartingWithNewest,
)
from .model_independent_funs import randomPassword from .model_independent_funs import randomPassword
from .models import ( from .models import (
MAX_MARK, MAX_MARK,
@ -77,7 +73,7 @@ class SemesterRowFilter(FilterEqual):
if not has_request_context(): if not has_request_context():
return tuple() return tuple()
semesters = sortedSemestersStartingWithNewest() semesters = Semester.sortedSemestersStartingWithNewest()
return tuple((semester.id, semester.repr()) for semester in semesters) return tuple((semester.id, semester.repr()) for semester in semesters)
@ -148,7 +144,7 @@ class UserView(SecureAdminModelView):
"Active Semester", "Active Semester",
query_factory=semesterQueryFactory, query_factory=semesterQueryFactory,
validators=[DataRequired()], validators=[DataRequired()],
default=lastSemester, default=Semester.lastSemester,
description="Not fixed and users (including assistants) can change it.", description="Not fixed and users (including assistants) can change it.",
) )
@ -1519,7 +1515,7 @@ class AnalysisView(SecureAdminBaseView):
for part in parts for part in parts
] ]
semesters = sortedSemestersStartingWithNewest() semesters = Semester.sortedSemestersStartingWithNewest()
semesterLabels = [semester.repr() for semester in semesters] semesterLabels = [semester.repr() for semester in semesters]
meanFinalPartMarks = np.flip( meanFinalPartMarks = np.flip(
[ [

View file

@ -9,25 +9,10 @@ from . import app, db
from .models import Semester from .models import Semester
def sortedSemestersStartingWithNewest(limit=0):
# Inserting an older semester is not allowed!
# Therefore, the id is enough.
stmt = select(Semester).order_by(Semester.id.desc())
if limit > 0:
stmt = stmt.limit(limit)
return db.session.execute(stmt).scalars()
def lastSemester():
return sortedSemestersStartingWithNewest(limit=1).first()
def initActiveSemesterMenuLinks(space): def initActiveSemesterMenuLinks(space):
with app.app_context(): with app.app_context():
try: try:
semesters = sortedSemestersStartingWithNewest() semesters = Semester.sortedSemestersStartingWithNewest()
for semester in semesters: for semester in semesters:
space.add_link( space.add_link(
MenuLink( MenuLink(
@ -55,7 +40,7 @@ def get_count(table):
def active_semester_repr(): def active_semester_repr():
active_semester = current_user.active_semester active_semester = current_user.active_semester
repr = active_semester.repr() repr = active_semester.repr()
if active_semester != lastSemester(): if active_semester != Semester.lastSemester():
flash(f"You are in the old semester {repr}!", "warning") flash(f"You are in the old semester {repr}!", "warning")
return repr return repr

View file

@ -479,6 +479,19 @@ class Semester(db.Model):
for part in oldSemester.parts: for part in oldSemester.parts:
db.session.add(Part(program=part.program, number=part.number, semester=self)) db.session.add(Part(program=part.program, number=part.number, semester=self))
def sortedSemestersStartingWithNewest(limit=0):
# Inserting an older semester is not allowed!
# Therefore, the id is enough.
stmt = select(Semester).order_by(Semester.id.desc())
if limit > 0:
stmt = stmt.limit(limit)
return db.session.execute(stmt).scalars()
def lastSemester():
return Semester.sortedSemestersStartingWithNewest(limit=1).first()
def repr(self): def repr(self):
return f"{self.label}{self.year}" return f"{self.label}{self.year}"

View file

@ -2,7 +2,7 @@ from flask import flash, redirect, render_template, request, url_for
from flask_security import auth_required, current_user, roles_accepted, roles_required from flask_security import auth_required, current_user, roles_accepted, roles_required
from . import app, db from . import app, db
from .model_dependent_funs import active_semester_repr, lastSemester from .model_dependent_funs import active_semester_repr
from .models import Semester from .models import Semester