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

Apply link formatters

This commit is contained in:
Mo 2022-06-30 04:08:21 +02:00
parent 50c03b9df2
commit 8c439ab530

View file

@ -9,7 +9,6 @@ from flask_admin.contrib.sqla.fields import QuerySelectField, QuerySelectMultipl
from flask_admin.contrib.sqla.filters import BooleanEqualFilter, FilterEqual
from flask_admin.helpers import get_form_data
from flask_admin.menu import MenuLink
from flask_admin.model.template import EndpointLinkRowAction
from flask_security import admin_change_password, current_user, hash_password
from flask_wtf import FlaskForm
from flask_wtf.file import FileAllowed, FileField, FileRequired
@ -30,12 +29,27 @@ from wtforms.validators import URL, DataRequired, Email, NumberRange, Optional
from wtforms.widgets import NumberInput
from . import adminSpace, app, assistantSpace, db, user_datastore
from .advlabdb_independent_funs import flashRandomPassword
from .customClasses import (
CustomIdEndpointLinkRowAction,
SecureAdminBaseView,
SecureAdminModelView,
from .admin_link_formatters import (
admin_formatter,
appointment_date_formatter,
appointment_formatter,
assistant_formatter,
experiment_formatter,
experiment_mark_formatter,
group_experiment_formatter,
group_formatter,
part_formatter,
part_student_formatter,
part_with_semester_formatter,
program_formatter,
semester_experiment_formatter,
semester_experiment_with_semester_formatter,
semester_formatter,
student_formatter,
user_formatter,
)
from .advlabdb_independent_funs import email_formatter, flashRandomPassword
from .customClasses import SecureAdminBaseView, SecureAdminModelView
from .database_import import importFromFile
from .exceptions import DataBaseException, ModelViewException
from .model_dependent_funs import (
@ -143,8 +157,6 @@ class UserView(SecureAdminModelView):
generate_new_password = generate_new_password_field()
can_view_details = True
column_list = [
"first_name",
"last_name",
@ -178,6 +190,11 @@ class UserView(SecureAdminModelView):
"active",
]
column_formatters = {
"email": email_formatter,
"active_semester": semester_formatter,
}
_skip_session_addition_on_model_creation = True
def customCreateModel(self, form):
@ -286,7 +303,6 @@ class SemesterView(SecureAdminModelView):
can_edit = False
can_delete = False
can_view_details = True
column_display_all_relations = True
column_list = [
@ -303,6 +319,13 @@ class SemesterView(SecureAdminModelView):
("label", True),
]
column_formatters = {
"parts": part_formatter,
"semester_experiments": semester_experiment_formatter,
"active_users": user_formatter,
"groups": group_formatter,
}
def customCreateModel(self, form):
return Semester.initFromOldSemester(
label=form.label.data,
@ -337,7 +360,6 @@ def programQueryFactory():
class PartView(SecureAdminModelView):
can_view_details = True
column_display_all_relations = True
column_sortable_list = []
@ -345,9 +367,6 @@ class PartView(SecureAdminModelView):
"program",
"number",
]
column_details_exclude_list = [
"semester",
]
form_columns = column_list
form_extra_fields = {
@ -363,10 +382,11 @@ class PartView(SecureAdminModelView):
"number": {"widget": NumberInput(min=1)},
}
column_searchable_list = [
"program.label",
"number",
]
column_formatters = {
"program": program_formatter,
"semester": semester_formatter,
"part_students": part_student_formatter,
}
def query_modifier(self, query):
return query.where(Part.semester == current_user.active_semester)
@ -376,7 +396,6 @@ class PartView(SecureAdminModelView):
class StudentView(SecureAdminModelView):
can_view_details = True
column_display_all_relations = True
column_list = [
@ -401,6 +420,12 @@ class StudentView(SecureAdminModelView):
"contact_email",
]
column_formatters = {
"uni_email": email_formatter,
"contact_email": email_formatter,
"part_students": part_student_formatter,
}
form_excluded_columns = [
"part_students",
]
@ -411,15 +436,6 @@ class StudentView(SecureAdminModelView):
"contact_email": {"validators": [Email()]},
}
column_extra_row_actions = [
EndpointLinkRowAction(
icon_class="fa fa-history",
endpoint="admin_experiment_mark.index_view",
id_arg="flt1_0",
title="Experiments history",
)
]
def partQueryFactory():
return Part.query.where(Part.semester == current_user.active_semester)
@ -443,18 +459,6 @@ class PartStudentView(SecureAdminModelView):
def apply(self, query, value, alias=None):
return query.where(PartStudent.part_id == int(value))
class StudentEndpointLinkRowAction(CustomIdEndpointLinkRowAction):
def customId(self, row):
return row.student_id
class GroupEndpointLinkRowAction(CustomIdEndpointLinkRowAction):
def customId(self, row):
return row.group_id
class PartEndpointLinkRowAction(CustomIdEndpointLinkRowAction):
def customId(self, row):
return row.part_id
class CreateForm(FlaskForm):
def studentQueryFactory():
return Student.query
@ -496,23 +500,12 @@ class PartStudentView(SecureAdminModelView):
)
refreshFiltersCache = True
column_extra_row_actions = [
StudentEndpointLinkRowAction(
icon_class="fa fa-user",
endpoint="admin_student.details_view",
title="Student",
),
GroupEndpointLinkRowAction(
icon_class="fa fa-users",
endpoint="admin_group.details_view",
title="Group",
),
PartEndpointLinkRowAction(
icon_class="fa fa-puzzle-piece",
endpoint="admin_part.details_view",
title="Part",
),
]
column_formatters = {
"student": student_formatter,
"group": group_formatter,
"part": part_formatter,
"experiment_marks": experiment_mark_formatter,
}
def query_modifier(self, query):
return query.join(Part).where(Part.semester == current_user.active_semester)
@ -565,8 +558,6 @@ class GroupView(SecureAdminModelView):
return CustomForm
can_view_details = True
column_display_all_relations = True
column_exclude_list = [
@ -579,6 +570,12 @@ class GroupView(SecureAdminModelView):
)
refreshFiltersCache = True
column_formatters = {
"program": program_formatter,
"part_students": part_student_formatter,
"group_experiments": group_experiment_formatter,
}
def query_modifier(self, query):
return query.where(Group.semester == current_user.active_semester)
@ -599,7 +596,6 @@ class ExperimentView(SecureAdminModelView):
def apply(self, query, value, alias=None):
return query.where(Experiment.program_id == int(value))
can_view_details = True
column_display_all_relations = True
column_filters = (
@ -650,6 +646,11 @@ class ExperimentView(SecureAdminModelView):
)
}
column_formatters = {
"program": program_formatter,
"semester_experiments": semester_experiment_with_semester_formatter,
}
def assistantQueryFactory():
return Assistant.query.join(User).where(User.active == True)
@ -671,10 +672,6 @@ class SemesterExperimentView(SecureAdminModelView):
def apply(self, query, value, alias=None):
return query.join(Experiment).where(Experiment.program_id == int(value))
class ExperimentEndpointLinkRowAction(CustomIdEndpointLinkRowAction):
def customId(self, row):
return row.experiment_id
class CreateForm(FlaskForm):
def experimentQueryFactory():
return Experiment.query.where(Experiment.active == True)
@ -699,16 +696,12 @@ class SemesterExperimentView(SecureAdminModelView):
class EditForm(CreateForm):
experiment = None
can_view_details = True
column_display_all_relations = True
column_list = [
"experiment",
"assistants",
]
column_details_exclude_list = [
"semester",
]
column_filters = (ProgramFilter(SemesterExperiment, "Program"),)
refreshFiltersCache = True
@ -718,13 +711,12 @@ class SemesterExperimentView(SecureAdminModelView):
"experiment.title",
]
column_extra_row_actions = [
ExperimentEndpointLinkRowAction(
icon_class="fa fa-flask",
endpoint="admin_experiment.details_view",
title="Experiment",
),
]
column_formatters = {
"experiment": experiment_formatter,
"semester": semester_formatter,
"assistants": assistant_formatter,
"group_experiments": group_experiment_formatter,
}
def query_modifier(self, query):
return query.where(SemesterExperiment.semester == current_user.active_semester)
@ -771,31 +763,18 @@ def userHasRoleFilterFactory(column, role_name):
class AssistantView(SecureAdminModelView):
class UserEndpointLinkRowAction(CustomIdEndpointLinkRowAction):
def customId(self, row):
return row.user_id
def assistantUserQueryFactory():
return User.query.join(User.roles).where(Role.name == "assistant")
can_view_details = True
column_display_all_relations = True
column_list = [
"user",
"semester_experiments",
]
column_details_list = column_list + [
"user.phone_number",
"user.mobile_phone_number",
"user.building",
"user.room",
"appointments",
"experiment_marks",
]
column_searchable_list = [
"user.first_name",
"user.last_name",
"user.email",
]
column_filters = (
userHasRoleFilterFactory(Assistant, "assistant"),
@ -822,20 +801,15 @@ class AssistantView(SecureAdminModelView):
),
}
column_extra_row_actions = [
UserEndpointLinkRowAction(
icon_class="fa fa-user",
endpoint="admin_user.details_view",
title="User",
),
]
column_formatters = {
"user": user_formatter,
"semester_experiments": semester_experiment_with_semester_formatter,
"appointments": appointment_formatter,
"experiment_marks": experiment_mark_formatter,
}
class AdminView(SecureAdminModelView):
class UserEndpointLinkRowAction(CustomIdEndpointLinkRowAction):
def customId(self, row):
return row.user_id
can_export = False
can_set_page_size = False
can_create = False
@ -850,13 +824,9 @@ class AdminView(SecureAdminModelView):
"user.active",
)
column_extra_row_actions = [
UserEndpointLinkRowAction(
icon_class="fa fa-user",
endpoint="admin_user.details_view",
title="User",
),
]
column_formatters = {
"user": user_formatter,
}
class ExperimentRowFilter(FilterEqual):
@ -935,14 +905,6 @@ class GroupExperimentView(SecureAdminModelView):
.where(Experiment.program_id == programId, Experiment.number == experimentNumber)
)
class GroupEndpointLinkRowAction(CustomIdEndpointLinkRowAction):
def customId(self, row):
return row.group_id
class SemesterExperimentEndpointLinkRowAction(CustomIdEndpointLinkRowAction):
def customId(self, row):
return row.semester_experiment_id
class EditForm(FlaskForm):
note = group_experiment_note_field()
@ -967,8 +929,6 @@ class GroupExperimentView(SecureAdminModelView):
note = group_experiment_note_field()
can_view_details = True
column_display_all_relations = True
column_filters = (
@ -979,18 +939,12 @@ class GroupExperimentView(SecureAdminModelView):
)
refreshFiltersCache = True
column_extra_row_actions = [
GroupEndpointLinkRowAction(
icon_class="fa fa-users",
endpoint="admin_group.details_view",
title="Group",
),
SemesterExperimentEndpointLinkRowAction(
icon_class="fa fa-flask",
endpoint="admin_semester_experiment.details_view",
title="SemesterExperiment",
),
]
column_formatters = {
"semester_experiment": semester_experiment_formatter,
"group": group_formatter,
"appointments": appointment_date_formatter,
"experiment_marks": experiment_mark_formatter,
}
def query_modifier(self, query):
return query.join(Group).where(Group.semester == current_user.active_semester)
@ -1046,14 +1000,6 @@ class AppointmentView(SecureAdminModelView):
def apply(self, query, value, alias=None):
return query.where(Appointment.assistant_id == int(value))
class GroupExperimentEndpointLinkRowAction(CustomIdEndpointLinkRowAction):
def customId(self, row):
return row.group_experiment_id
class AssistantEndpointLinkRowAction(CustomIdEndpointLinkRowAction):
def customId(self, row):
return row.assistant_id
class CreateAndEditForm(FlaskForm):
group_experiment = QuerySelectField(
"Group Experiment",
@ -1081,18 +1027,10 @@ class AppointmentView(SecureAdminModelView):
"special",
]
column_extra_row_actions = [
GroupExperimentEndpointLinkRowAction(
icon_class="fa fa-flask",
endpoint="admin_group_experiment.details_view",
title="GroupExperiment",
),
AssistantEndpointLinkRowAction(
icon_class="fa fa-user-secret",
endpoint="admin_assistant.details_view",
title="Assistant",
),
]
column_formatters = {
"group_experiment": group_experiment_formatter,
"assistant": assistant_formatter,
}
def query_modifier(self, query):
return (
@ -1177,18 +1115,6 @@ class ExperimentMarkView(SecureAdminModelView):
query.join(GroupExperiment).join(SemesterExperiment).where(SemesterExperiment.semester_id == int(value))
)
class PartStudentEndpointLinkRowAction(CustomIdEndpointLinkRowAction):
def customId(self, row):
return row.part_student_id
class GroupExperimentEndpointLinkRowAction(CustomIdEndpointLinkRowAction):
def customId(self, row):
return row.group_experiment_id
class AssistantEndpointLinkRowAction(CustomIdEndpointLinkRowAction):
def customId(self, row):
return row.assistant_id
class CreateForm(FlaskForm):
part_student = QuerySelectField(
"Part Student",
@ -1234,23 +1160,12 @@ class ExperimentMarkView(SecureAdminModelView):
column_default_sort = [("oral_mark", False), ("protocol_mark", False)]
column_extra_row_actions = [
PartStudentEndpointLinkRowAction(
icon_class="fa fa-user",
endpoint="admin_part_student.details_view",
title="PartStudent",
),
GroupExperimentEndpointLinkRowAction(
icon_class="fa fa-flask",
endpoint="admin_group_experiment.details_view",
title="GroupExperiment",
),
AssistantEndpointLinkRowAction(
icon_class="fa fa-user-secret",
endpoint="admin_assistant.details_view",
title="Assistant",
),
]
column_formatters = {
"part_student": part_student_formatter,
"group_experiment": group_experiment_formatter,
"assistant": assistant_formatter,
"admin": admin_formatter,
}
# TODO: Find solution
"""
@ -1285,12 +1200,14 @@ class ExperimentMarkView(SecureAdminModelView):
class ProgramView(SecureAdminModelView):
can_export = False
can_set_page_size = False
can_view_details = True
column_display_all_relations = True
column_list = [
"label",
]
column_details_exclude_list = [
"groups",
]
column_sortable_list = []
form_excluded_columns = [
"parts",
@ -1298,6 +1215,11 @@ class ProgramView(SecureAdminModelView):
"groups",
]
column_formatters = {
"parts": part_with_semester_formatter,
"experiments": experiment_formatter,
}
class ImportView(SecureAdminBaseView):
class FileForm(FlaskForm):