Verified Commit df893f6f authored by Jonathan Weth's avatar Jonathan Weth

Show also groups which have child groups with lessons in my groups and select form

In order to do that, refactor the queries to model extensions
parent dad12d3e
Pipeline #3827 passed with stages
in 3 minutes and 52 seconds
......@@ -79,11 +79,7 @@ class SelectForm(forms.Form):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["group"].queryset = (
self.fields["group"].queryset = Group.get_groups_with_lessons()
PersonalNoteFormSet = forms.modelformset_factory(
from datetime import date
from typing import Dict, 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
import reversion
from calendarweek import CalendarWeek
......@@ -204,3 +205,28 @@ def get_extra_marks(
stats[extra_mark] = qs
return stats
def get_groups_with_lessons(cls: Group):
"""Get all groups which have related lessons or child groups with related lessons."""
group_pks = (
.values_list("pk", flat=True)
groups = cls.objects.filter(
Q(child_groups__pk__in=group_pks) | Q(pk__in=group_pks)
return groups
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)
......@@ -463,11 +463,7 @@ def full_register_group(request: HttpRequest, id_: int) -> HttpResponse:
def my_students(request: HttpRequest) -> HttpResponse:
context = {}
relevant_groups = (
.filter(lessons_count__gt=0, owners=request.user.person)
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)
......@@ -475,12 +471,7 @@ def my_students(request: HttpRequest) -> HttpResponse:
def my_groups(request: HttpRequest) -> HttpResponse:
context = {}
groups = (
.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)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment