Group users by checkout information created date

This commit is contained in:
Elia Bieri 2024-09-17 14:51:13 +02:00
parent 294c33ddc8
commit a29171f32f
2 changed files with 24 additions and 12 deletions

View File

@ -258,6 +258,11 @@ def create_default_users(default_password="test", set_avatar=False):
)
_create_student_user(
id=TEST_STUDENT3_VV_USER_ID,
email="student-vv3@eiger-versicherungen.ch",
first_name="Vladi",
last_name="Volodemir",
)
_create_student_user(
email="patrizia.huggel@eiger-versicherungen.ch",
first_name="Patrizia",
last_name="Huggel",
@ -424,7 +429,7 @@ def create_default_users(default_password="test", set_avatar=False):
)
_create_user(
_id=TEST_AUSBILDUNGSVERANTWORTLICHER1_USER_ID,
email="test-lernbegleiter1@example.com",
email="test-ausbildungsverantwortlicher1@example.com",
first_name="Bruno",
last_name="Banani-Ausbildungsverantwortlicher",
password=default_password,

View File

@ -1,6 +1,8 @@
from collections import defaultdict
from itertools import groupby
import graphene
from django.db.models.functions import ExtractYear
from graphene import Enum
from vbv_lernwelt.course.graphql.types import (
@ -306,21 +308,26 @@ class TrainingResponsibleStatisticsType(graphene.ObjectType):
@staticmethod
def resolve_participants_per_year(root, info):
user = info.context.user
course_session_users = CourseSessionUser.objects.filter(
course_session_users = (
CourseSessionUser.objects.filter(
agentparticipantrelation__agent=user,
agentparticipantrelation__participant__course_session=root.course_session_id,
)
grouped_course_session_users = groupby(
sorted(course_session_users, key=lambda x: x.created_at.year),
key=lambda x: x.created_at.year,
.annotate(
checkout_year=ExtractYear("user__checkout_information__created_at")
)
.select_related("user")
)
grouped_course_session_users = defaultdict(list)
for csu in course_session_users:
checkout_year = csu.checkout_year
if checkout_year:
grouped_course_session_users[checkout_year].append(csu)
return [
ParticipantsForYear(
_id=f"{root.course_session_id} {year}",
year=year,
participants=list(c),
participants=participants,
)
for year, c in grouped_course_session_users
for year, participants in grouped_course_session_users.items()
]