diff --git a/aleksis/apps/alsijil/forms.py b/aleksis/apps/alsijil/forms.py index c66564465a1b39299f24f39392a87d7c7baed6c9..33f43b5153e7ac36a93093ecbadb2da1def157c6 100644 --- a/aleksis/apps/alsijil/forms.py +++ b/aleksis/apps/alsijil/forms.py @@ -86,15 +86,7 @@ class SelectForm(forms.Form): person = self.request.user.person - group_pks = ( - Group.objects.for_current_school_term_or_all() - .annotate(lessons_count=Count("lessons")) - .filter(lessons_count__gt=0) - .values_list("pk", flat=True) - ) - group_qs = Group.objects.filter( - Q(child_groups__pk__in=group_pks) | Q(pk__in=group_pks) - ).distinct() + group_qs = Group.get_groups_with_lessons() if not check_global_permission(self.request.user, "alsijil.view_week"): group_qs = ( diff --git a/aleksis/apps/alsijil/model_extensions.py b/aleksis/apps/alsijil/model_extensions.py index 1c7fa661c80f14a706b5b11b4ebfa9856ee12fab..015731b9a42e915517fc47fb7a7a462df1c4eee9 100644 --- a/aleksis/apps/alsijil/model_extensions.py +++ b/aleksis/apps/alsijil/model_extensions.py @@ -1,7 +1,8 @@ from datetime import date from typing import Dict, Iterator, Optional, Union -from django.db.models import Exists, OuterRef, QuerySet +from django.db.models import Exists, OuterRef, Q, QuerySet +from django.db.models.aggregates import Count from django.utils.translation import gettext as _ import reversion @@ -242,3 +243,28 @@ def get_extra_marks( stats[extra_mark] = qs return stats + + +@Group.class_method +def get_groups_with_lessons(cls: Group): + """Get all groups which have related lessons or child groups with related lessons.""" + group_pks = ( + cls.objects.for_current_school_term_or_all() + .annotate(lessons_count=Count("lessons")) + .filter(lessons_count__gt=0) + .values_list("pk", flat=True) + ) + groups = cls.objects.filter( + Q(child_groups__pk__in=group_pks) | Q(pk__in=group_pks) + ).distinct() + + return groups + + +@Person.method +def get_owner_groups_with_lessons(self: Person): + """Get all groups the person is an owner of and which have related lessons. + + Groups which have child groups with related lessons are also included. + """ + return Group.get_groups_with_lessons().filter(owners=self) diff --git a/aleksis/apps/alsijil/templates/alsijil/absences/register.html b/aleksis/apps/alsijil/templates/alsijil/absences/register.html index b14890e21438a6e452c4fa3e6c595f99d5b856dd..4074c947fb5016b912bdfaf6d793bdf1a1b263b1 100644 --- a/aleksis/apps/alsijil/templates/alsijil/absences/register.html +++ b/aleksis/apps/alsijil/templates/alsijil/absences/register.html @@ -17,7 +17,7 @@ $(document).ready(function () { $("#id_date_start").change(function () { $("#id_date_end").val($("#id_date_start").val()); - $("#id_date_end").change(); + initDatePicker("#id_date_end"); }); }); </script> diff --git a/aleksis/apps/alsijil/views.py b/aleksis/apps/alsijil/views.py index adfb6586072fe285e28aebee1b9d9925cc4ea672..4f34328162c1cfc51f21bebb2ffe91b39c2fd9db 100644 --- a/aleksis/apps/alsijil/views.py +++ b/aleksis/apps/alsijil/views.py @@ -533,12 +533,8 @@ def full_register_group(request: HttpRequest, id_: int) -> HttpResponse: @permission_required("alsijil.view_my_students") def my_students(request: HttpRequest) -> HttpResponse: context = {} - relevant_groups = ( - Group.objects.for_current_school_term_or_all() - .annotate(lessons_count=Count("lessons")) - .filter(lessons_count__gt=0, owners=request.user.person) - ) - persons = Person.objects.filter(member_of__in=relevant_groups).distinct() + relevant_groups = request.user.person.get_owner_groups_with_lessons() + persons = Person.objects.filter(member_of__in=relevant_groups) context["persons"] = persons return render(request, "alsijil/class_register/persons.html", context) @@ -546,12 +542,7 @@ def my_students(request: HttpRequest) -> HttpResponse: @permission_required("alsijil.view_my_groups",) def my_groups(request: HttpRequest) -> HttpResponse: context = {} - groups = ( - Group.objects.for_current_school_term_or_all() - .annotate(lessons_count=Count("lessons")) - .filter(lessons_count__gt=0, owners=request.user.person) - ) - context["groups"] = groups + context["groups"] = request.user.person.get_owner_groups_with_lessons() return render(request, "alsijil/class_register/groups.html", context)