centralized student permission in one function

This commit is contained in:
2025-12-01 14:39:09 +01:00
parent a31798d0b0
commit f06f269568
4 changed files with 26 additions and 21 deletions

View File

@@ -15,6 +15,7 @@ from .training_adm import TrainingInLIne
from .hourbuilding_adm import HourBuildingInLine
from ..custom.colortag import course_color
from ..custom.student_permissions import has_edit_permission
from ..actions.exportweek import export_selected
from datetime import date
@@ -88,12 +89,7 @@ class WeekPreferenceAdmin(nested_admin.NestedPolymorphicModelAdmin):
# If user is a student deny edit permission for week past the current one
def has_change_permission(self, request: HttpRequest, obj: WeekPreference | None = None) -> bool:
if hasattr(request.user, "student") and obj:
student: Student = request.user.student
current_week = date.today().isocalendar().week
if current_week > obj.week or not student.active:
return False
return True
return has_edit_permission(request=request, obj=obj)
# If user is a student deny edit permission for week past the current one
def has_add_permission(self, request: HttpRequest, obj: WeekPreference | None = None) -> bool:
@@ -106,10 +102,8 @@ class WeekPreferenceAdmin(nested_admin.NestedPolymorphicModelAdmin):
def changeform_view(self, request: HttpRequest, object_id: int | None = None, form_url: str = "", extra_context=None):
extra_context = extra_context or {}
if hasattr(request.user, "student") and object_id:
student: Student = request.user.student
current_week = date.today().isocalendar().week
weekpref = WeekPreference.objects.get(id=object_id)
if current_week > weekpref.week or not student.active:
if not has_edit_permission(request=request, obj=weekpref):
extra_context["show_save"] = False
extra_context["show_save_and_continue"] = False
extra_context["show_save_and_add_another"] = False