Skip to content
Snippets Groups Projects
Verified Commit df893f6f authored by Jonathan Weth's avatar Jonathan Weth :keyboard:
Browse files

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
No related branches found
No related tags found
1 merge request!93Resolve "Show also groups with child groups with lessons in my groups"
Pipeline #3827 passed
...@@ -79,11 +79,7 @@ class SelectForm(forms.Form): ...@@ -79,11 +79,7 @@ class SelectForm(forms.Form):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.fields["group"].queryset = ( self.fields["group"].queryset = Group.get_groups_with_lessons()
Group.objects.for_current_school_term_or_all()
.annotate(lessons_count=Count("lessons"))
.filter(lessons_count__gt=0)
)
PersonalNoteFormSet = forms.modelformset_factory( PersonalNoteFormSet = forms.modelformset_factory(
......
from datetime import date from datetime import date
from typing import Dict, Optional, Union 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 import reversion
from calendarweek import CalendarWeek from calendarweek import CalendarWeek
...@@ -204,3 +205,28 @@ def get_extra_marks( ...@@ -204,3 +205,28 @@ def get_extra_marks(
stats[extra_mark] = qs stats[extra_mark] = qs
return stats 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: ...@@ -463,11 +463,7 @@ def full_register_group(request: HttpRequest, id_: int) -> HttpResponse:
def my_students(request: HttpRequest) -> HttpResponse: def my_students(request: HttpRequest) -> HttpResponse:
context = {} context = {}
relevant_groups = ( relevant_groups = request.user.person.get_owner_groups_with_lessons()
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) persons = Person.objects.filter(member_of__in=relevant_groups)
context["persons"] = persons context["persons"] = persons
return render(request, "alsijil/class_register/persons.html", context) return render(request, "alsijil/class_register/persons.html", context)
...@@ -475,12 +471,7 @@ def my_students(request: HttpRequest) -> HttpResponse: ...@@ -475,12 +471,7 @@ def my_students(request: HttpRequest) -> HttpResponse:
def my_groups(request: HttpRequest) -> HttpResponse: def my_groups(request: HttpRequest) -> HttpResponse:
context = {} context = {}
groups = ( context["groups"] = request.user.person.get_owner_groups_with_lessons()
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
return render(request, "alsijil/class_register/groups.html", context) return render(request, "alsijil/class_register/groups.html", context)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment