Commit dad563e6 authored by Dominik George's avatar Dominik George 🍳

Merge branch '110-show-also-groups-with-child-groups-with-lessons-in-my-groups' into 'master'

Resolve "Show also groups with child groups with lessons in my groups"

Closes #110

See merge request !93
parents dad12d3e df893f6f
Pipeline #3834 passed with stages
in 2 minutes and 54 seconds
......@@ -79,11 +79,7 @@ class SelectForm(forms.Form):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["group"].queryset = (
Group.objects.for_current_school_term_or_all()
.annotate(lessons_count=Count("lessons"))
.filter(lessons_count__gt=0)
)
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
@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)
......@@ -463,11 +463,7 @@ def full_register_group(request: HttpRequest, id_: int) -> HttpResponse:
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)
)
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 = (
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)
......
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