From de91814c6abf5699c896a2b108bc94dbee8427fd Mon Sep 17 00:00:00 2001 From: Daniel Egger Date: Wed, 3 Apr 2024 12:24:12 +0200 Subject: [PATCH] Add learning mentor relations to persons view --- server/config/urls.py | 6 +- .../dashboard/tests/test_views.py | 6 +- server/vbv_lernwelt/dashboard/views.py | 100 ++++++++++++++++-- 3 files changed, 95 insertions(+), 17 deletions(-) diff --git a/server/config/urls.py b/server/config/urls.py index 9848a2c1..b18dc421 100644 --- a/server/config/urls.py +++ b/server/config/urls.py @@ -10,9 +10,6 @@ from django.views import defaults as default_views from django.views.decorators.csrf import csrf_exempt from django_ratelimit.exceptions import Ratelimited from graphene_django.views import GraphQLView -from wagtail import urls as wagtail_urls -from wagtail.admin import urls as wagtailadmin_urls -from wagtail.documents import urls as media_library_urls from vbv_lernwelt.api.directory import list_entities from vbv_lernwelt.api.user import get_profile, me_user_view, post_avatar @@ -61,6 +58,9 @@ from vbv_lernwelt.importer.views import ( ) from vbv_lernwelt.media_files.views import user_image from vbv_lernwelt.notify.views import email_notification_settings +from wagtail import urls as wagtail_urls +from wagtail.admin import urls as wagtailadmin_urls +from wagtail.documents import urls as media_library_urls class SignedIntConverter(IntConverter): diff --git a/server/vbv_lernwelt/dashboard/tests/test_views.py b/server/vbv_lernwelt/dashboard/tests/test_views.py index 4f9bc491..4f0a5c99 100644 --- a/server/vbv_lernwelt/dashboard/tests/test_views.py +++ b/server/vbv_lernwelt/dashboard/tests/test_views.py @@ -1,12 +1,12 @@ from django.test import TestCase from vbv_lernwelt.course.creators.test_utils import ( + add_course_session_group_supervisor, + add_course_session_user, create_course, create_course_session, - create_user, - add_course_session_user, create_course_session_group, - add_course_session_group_supervisor, + create_user, ) from vbv_lernwelt.course.models import CourseSessionUser from vbv_lernwelt.dashboard.views import get_course_sessions_with_roles_for_user diff --git a/server/vbv_lernwelt/dashboard/views.py b/server/vbv_lernwelt/dashboard/views.py index defa34bc..7c5342de 100644 --- a/server/vbv_lernwelt/dashboard/views.py +++ b/server/vbv_lernwelt/dashboard/views.py @@ -7,7 +7,6 @@ from rest_framework.response import Response from vbv_lernwelt.core.models import User from vbv_lernwelt.course.models import CourseSession, CourseSessionUser -from vbv_lernwelt.course.serializers import CourseSessionSerializer from vbv_lernwelt.course.views import logger from vbv_lernwelt.course_session_group.models import CourseSessionGroup from vbv_lernwelt.learning_mentor.models import LearningMentor @@ -73,22 +72,101 @@ def get_dashboard_persons(request): try: course_sessions = get_course_sessions_with_roles_for_user(request.user) - persons = [] + result_persons = {} for cs in course_sessions: - if {"SUPERVISOR", "EXPERT", "MEMBER"} & cs.roles: - persons.extend( - CourseSessionUser.objects.filter(course_session=cs.id).values( - "user" - ) + if { + "SUPERVISOR", + "EXPERT", + "MEMBER", + } & cs.roles and cs.course.configuration.is_uk: + course_session_users = CourseSessionUser.objects.filter( + course_session=cs.id ) + my_role = ( + "SUPERVISOR" + if "SUPERVISOR" in cs.roles + else ("EXPERT" if "EXPERT" in cs.roles else "MEMBER") + ) + for csu in course_session_users: + result_persons[csu.user.id] = { + "user_id": csu.user.id, + "first_name": csu.user.first_name, + "last_name": csu.user.last_name, + "email": csu.user.email, + "course_sessions": [ + { + "id": cs.id, + "session_title": cs.title, + "course_id": cs.course.id, + "course_title": cs.course.title, + "user_role": csu.role, + "my_role": my_role, + } + ], + } - all_to_serialize = course_sessions + # add persons where request.user is mentor + for cs in course_sessions: + if "LEARNING_MENTOR" in cs.roles: + lm = LearningMentor.objects.filter( + mentor=request.user, course_session=cs.id + ).first() + + for participant in lm.participants.all(): + course_session_entry = { + "id": cs.id, + "course_id": cs.course.id, + "session_title": cs.title, + "course_title": cs.course.title, + "user_role": "LEARNING_MENTEE", + "my_role": "LEARNING_MENTOR", + } + + if participant.user.id not in result_persons: + result_persons[participant.user.id] = { + "user_id": participant.user.id, + "first_name": participant.user.first_name, + "last_name": participant.user.last_name, + "email": participant.user.email, + "course_sessions": [course_session_entry], + } + else: + # user is already in result_persons + result_persons[participant.user.id]["course_sessions"].append( + course_session_entry + ) + + # add persons where request.user is mentee + mentor_relation_qs = LearningMentor.objects.filter( + participants__user=request.user + ).prefetch_related("mentor", "course_session") + for mentor_relation in mentor_relation_qs: + course_session_entry = { + "id": mentor_relation.course_session.id, + "session_title": mentor_relation.course_session.title, + "course_id": mentor_relation.course_session.course.id, + "course_title": mentor_relation.course_session.course.title, + "user_role": "LEARNING_MENTOR", + "my_role": "LEARNING_MENTEE", + } + + if mentor_relation.mentor.id not in result_persons: + result_persons[mentor_relation.mentor.id] = { + "user_id": mentor_relation.mentor.id, + "first_name": mentor_relation.mentor.first_name, + "last_name": mentor_relation.mentor.last_name, + "email": mentor_relation.mentor.email, + "course_sessions": [course_session_entry], + } + else: + # user is already in result_persons + result_persons[mentor_relation.mentor.id]["course_sessions"].append( + course_session_entry + ) return Response( status=200, - data=CourseSessionSerializer( - all_to_serialize, many=True, context={"user": request.user} - ).data, + data=list(result_persons.values()), ) except PermissionDenied as e: raise e