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

Fixed active semester, added StudentModelView

This commit is contained in:
Mo 2021-04-26 21:31:15 +02:00
parent c86091fe39
commit 48fda40c2c
6 changed files with 43 additions and 26 deletions

File diff suppressed because one or more lines are too long

View file

@ -8,6 +8,10 @@ from advlabdb import admin, app, user_datastore, db
from advlabdb.models import * from advlabdb.models import *
from advlabdb.utils import randomPassword from advlabdb.utils import randomPassword
partsLabels = ["A/1", "A/2", "B/1", "B/2"]
class UserModelView(SecureModelView): class UserModelView(SecureModelView):
column_list = ["email", "active", "roles", "assistant"] column_list = ["email", "active", "roles", "assistant"]
column_searchable_list = ["email"] column_searchable_list = ["email"]
@ -40,10 +44,8 @@ class SemesterModelView(SecureModelView):
column_list = ["label", "parts"] column_list = ["label", "parts"]
form_columns = ["label", "create_parts"] form_columns = ["label", "create_parts"]
parts = ["A/1", "A/2", "B/1", "B/2"]
form_extra_fields = { form_extra_fields = {
"create_parts": BooleanField("Create parts:" + ", ".join(parts) + ".", default=True) "create_parts": BooleanField("Create parts:" + ", ".join(partsLabels) + ".", default=True)
} }
def after_model_change(self, form, model, is_created): def after_model_change(self, form, model, is_created):
@ -58,7 +60,16 @@ class PartModelView(SecureModelView):
column_details_list = ["label", "semester", "part_experiments", "part_students", "groups"] column_details_list = ["label", "semester", "part_experiments", "part_students", "groups"]
form_columns = ["label", "semester"] form_columns = ["label", "semester"]
admin.add_view(SecureModelView(Student, db.session))
class StudentModelView(SecureModelView):
can_view_details = True
column_list = ["student_number", "first_name", "last_name", "email", "part_students"]
column_details_list = column_list + ["bachelor_thesis", "bachelor_thesis_work_group"]
column_searchable_list = ["student_number", "email", "first_name", "last_name"]
form_columns = column_list
admin.add_view(StudentModelView(Student, db.session))
admin.add_view(SecureModelView(PartStudent, db.session)) admin.add_view(SecureModelView(PartStudent, db.session))
admin.add_view(SecureModelView(Group, db.session)) admin.add_view(SecureModelView(Group, db.session))
admin.add_view(SecureModelView(GroupExperiment, db.session)) admin.add_view(SecureModelView(GroupExperiment, db.session))

View file

@ -134,7 +134,7 @@ class Part(db.Model):
class Semester(db.Model): class Semester(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
label = db.Column(db.String(100), nullable=False) # WS2122 for example label = db.Column(db.String(100), nullable=False, unique=True) # WS2122 for example
parts = db.relationship("Part", backref="semester", lazy=True) parts = db.relationship("Part", backref="semester", lazy=True)
def __repr__(self): def __repr__(self):
@ -154,6 +154,7 @@ class ExperimentMark(db.Model):
class User(db.Model, FsUserMixin): class User(db.Model, FsUserMixin):
assistant = db.relationship("Assistant", backref="user", lazy=True, uselist=False) assistant = db.relationship("Assistant", backref="user", lazy=True, uselist=False)
active_semester_id = db.Column(db.Integer, nullable=True)
def __repr__(self): def __repr__(self):
return f"<User {self.email}>" return f"<User {self.email}>"

View file

@ -1,6 +1,6 @@
import json import json
from advlabdb import app, user_datastore from advlabdb import app, user_datastore
from flask import render_template, request, url_for, flash, redirect, session from flask import render_template, request, url_for, flash, redirect
from flask_security import auth_required, roles_required, hash_password, current_user from flask_security import auth_required, roles_required, hash_password, current_user
from advlabdb.utils import * from advlabdb.utils import *
@ -18,19 +18,6 @@ def util_processor():
str(semester.id) + '">' + semester.label + '</a></li>') str(semester.id) + '">' + semester.label + '</a></li>')
return items return items
def activeSemesterLabel():
lastSemesterId = Semester.query.all()[-1].id
if "activeSemesterId" not in session:
session["activeSemesterId"] = lastSemesterId
elif session["activeSemesterId"] != lastSemesterId:
activeSemester = Semester.query.get(session['activeSemesterId'])
if activeSemester:
flash(f"You are in the old semester {activeSemester.label}!", "warning")
else:
flash(f"Semester changed!", "warning")
session["activeSemesterId"] = lastSemesterId
return Semester.query.get(session["activeSemesterId"]).label
def navbarItems(title): def navbarItems(title):
adminPages = ["Students", "Assistants", "Experiments", "Appointments", "Groups", "Users", "Semesters"] adminPages = ["Students", "Assistants", "Experiments", "Appointments", "Groups", "Users", "Semesters"]
assistantPages = ["Students", "Assistants", "Experiments", "Appointments", "Groups"] assistantPages = ["Students", "Assistants", "Experiments", "Appointments", "Groups"]
@ -61,7 +48,7 @@ def util_processor():
return items return items
return dict(semesterDropDownItems=semesterDropDownItems, return dict(semesterDropDownItems=semesterDropDownItems,
activeSemesterLabel=activeSemesterLabel, userActiveSemester=userActiveSemester,
navbarItems=navbarItems, navbarItems=navbarItems,
current_user=current_user, current_user=current_user,
) )
@ -76,7 +63,7 @@ def index():
@app.route("/students") @app.route("/students")
@roles_required("admin") @roles_required("admin")
def students(): def students():
semester = Semester.query.get(session["activeSemesterId"]) semester = Semester.query.get(current_user.active_semester_id)
parts = semester.parts parts = semester.parts
tables = [] tables = []
tablesLabels = [] tablesLabels = []
@ -128,7 +115,7 @@ def assistants():
@app.route("/experiments") @app.route("/experiments")
@roles_required("admin") @roles_required("admin")
def experiments(): def experiments():
semester = Semester.query.get(session["activeSemesterId"]) semester = Semester.query.get(current_user.active_semester_id)
parts = semester.parts parts = semester.parts
tables = [] tables = []
tablesLabels = [] tablesLabels = []
@ -159,7 +146,7 @@ def appointments():
@app.route("/groups") @app.route("/groups")
@roles_required("admin") @roles_required("admin")
def groups(): def groups():
semester = Semester.query.get(session["activeSemesterId"]) semester = Semester.query.get(current_user.active_semester_id)
parts = semester.parts parts = semester.parts
tables = [] tables = []
tablesLabels = [] tablesLabels = []
@ -219,5 +206,5 @@ def semesters():
@app.route("/set_semester", methods=["GET"]) @app.route("/set_semester", methods=["GET"])
@auth_required() @auth_required()
def set_semester(): def set_semester():
session["activeSemesterId"] = int(request.args.get("semester_id")) current_user.active_semester_id = int(request.args.get("semester_id"))
return redirect(request.referrer) return redirect(request.referrer)

View file

@ -35,7 +35,7 @@
{% if current_user.is_authenticated %} {% if current_user.is_authenticated %}
<li class="nav-item dropdown"> <li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" id="navbarDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-expanded="false"> <a class="nav-link dropdown-toggle" id="navbarDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Semester {{activeSemesterLabel()}} Semester {{userActiveSemester().label}}
</a> </a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink"> <ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
{% for item in semesterDropDownItems() %} {% for item in semesterDropDownItems() %}

View file

@ -1,5 +1,8 @@
from random import choice from random import choice
from string import digits, ascii_letters from string import digits, ascii_letters
from flask_security import current_user
from advlabdb.models import Semester
def makeTable(headerAndDataList, rows, tableId="table"): def makeTable(headerAndDataList, rows, tableId="table"):
def cellString(cell): def cellString(cell):
@ -54,6 +57,7 @@ data-export-types="['json', 'xml', 'csv', 'txt', 'sql', 'pdf']">
<br>''' <br>'''
return table return table
def appointmentDate(date): def appointmentDate(date):
return date.strftime("%a %d.%m.%Y") return date.strftime("%a %d.%m.%Y")
@ -64,3 +68,17 @@ def randomPassword():
def titleToTemplate(page): def titleToTemplate(page):
return page.lower().replace(" ", "_") return page.lower().replace(" ", "_")
def userActiveSemester():
lastSemesterId = Semester.query.all()[-1].id
if current_user.active_semester_id is None:
current_user.active_semester_id = lastSemesterId
elif current_user.active_semester_id != lastSemesterId:
activeSemester = Semester.query.get(current_user.active_semester_id)
if activeSemester:
flash(f"You are in the old semester {activeSemester.label}!", "warning")
else:
flash(f"Semester changed!", "warning")
current_user.active_semester_id = lastSemesterId
return Semester.query.get(current_user.active_semester_id)