From 368ef7d16ef0e7c545e4a2e99345f1e9f80f4539 Mon Sep 17 00:00:00 2001 From: Livio Bieri Date: Thu, 14 Dec 2023 11:31:10 +0100 Subject: [PATCH] fix: don't crash dashboard as mentor --- .../course/creators/test_utils.py | 9 +++++ .../vbv_lernwelt/dashboard/graphql/queries.py | 27 ++++++++++++- .../dashboard/tests/graphql/test_dashboard.py | 39 +++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) diff --git a/server/vbv_lernwelt/course/creators/test_utils.py b/server/vbv_lernwelt/course/creators/test_utils.py index 44e97bff..7fc1fdbe 100644 --- a/server/vbv_lernwelt/course/creators/test_utils.py +++ b/server/vbv_lernwelt/course/creators/test_utils.py @@ -40,6 +40,7 @@ from vbv_lernwelt.course_session.models import ( ) from vbv_lernwelt.course_session_group.models import CourseSessionGroup from vbv_lernwelt.duedate.models import DueDate +from vbv_lernwelt.learning_mentor.models import LearningMentor from vbv_lernwelt.learnpath.models import ( Circle, LearningContentAssignment, @@ -94,6 +95,14 @@ def create_course_session( ) +def add_learning_mentor( + course: Course, mentor: User, mentee: CourseSessionUser +) -> LearningMentor: + learning_mentor = LearningMentor.objects.create(course=course, mentor=mentor) + learning_mentor.participants.add(mentee) + return learning_mentor + + def add_course_session_user( course_session: CourseSession, user: User, role: CourseSessionUser.Role ) -> CourseSessionUser: diff --git a/server/vbv_lernwelt/dashboard/graphql/queries.py b/server/vbv_lernwelt/dashboard/graphql/queries.py index 7579fd56..5c62f242 100644 --- a/server/vbv_lernwelt/dashboard/graphql/queries.py +++ b/server/vbv_lernwelt/dashboard/graphql/queries.py @@ -23,6 +23,7 @@ from vbv_lernwelt.iam.permissions import ( can_view_course_session_group_statistics, can_view_course_session_progress, ) +from vbv_lernwelt.learning_mentor.models import LearningMentor class DashboardQuery(graphene.ObjectType): @@ -86,7 +87,13 @@ class DashboardQuery(graphene.ObjectType): user=user, exclude_course_ids=statistics_dashboard_course_ids ) - return statistic_dashboards + course_session_dashboards + learning_mentor_dashboards = get_learning_mentor_dashboards(user=user) + + return ( + statistic_dashboards + + course_session_dashboards + + learning_mentor_dashboards + ) def resolve_course_progress(root, info, course_id: str): # noqa """ @@ -174,6 +181,24 @@ def get_user_statistics_dashboards(user: User) -> Tuple[List[Dict[str, str]], Se return dashboards, course_index +def get_learning_mentor_dashboards(user: User) -> List[Dict[str, str]]: + learning_mentor = LearningMentor.objects.filter(mentor=user) + dashboards = [] + + for mentor in learning_mentor: + course = mentor.course + dashboards.append( + { + "id": str(course.id), + "name": course.title, + "slug": course.slug, + "dashboard_type": DashboardType.SIMPLE_DASHBOARD, + } + ) + + return dashboards + + def get_user_course_session_dashboards( user: User, exclude_course_ids: Set[int] ) -> List[Dict[str, str]]: diff --git a/server/vbv_lernwelt/dashboard/tests/graphql/test_dashboard.py b/server/vbv_lernwelt/dashboard/tests/graphql/test_dashboard.py index 5a8e0e40..43e64138 100644 --- a/server/vbv_lernwelt/dashboard/tests/graphql/test_dashboard.py +++ b/server/vbv_lernwelt/dashboard/tests/graphql/test_dashboard.py @@ -4,6 +4,7 @@ from vbv_lernwelt.assignment.models import AssignmentType from vbv_lernwelt.course.creators.test_utils import ( add_course_session_group_supervisor, add_course_session_user, + add_learning_mentor, create_assignment, create_assignment_completion, create_circle, @@ -213,6 +214,44 @@ class DashboardTestCase(GraphQLTestCase): self.assertEqual(course_3_config["slug"], course_3.slug) self.assertEqual(course_3_config["dashboard_type"], "SIMPLE_DASHBOARD") + def test_dashboard_config_mentor(self): + # GIVEN + course_1, _ = create_course("Test Course 1") + cs_1 = create_course_session(course=course_1, title="Test Course 1 Session") + + mentor = create_user("learning mentor") + csu = add_course_session_user( + course_session=cs_1, + user=create_user("csu"), + role=CourseSessionUser.Role.MEMBER, + ) + + add_learning_mentor(course=course_1, mentor=mentor, mentee=csu) + + self.client.force_login(mentor) + + # WHEN + query = """query { + dashboard_config { + id + name + slug + dashboard_type + } + } + """ + + response = self.query(query) + + # THEN + self.assertResponseNoErrors(response) + + self.assertEqual(len(response.json()["data"]["dashboard_config"]), 1) + self.assertEqual( + response.json()["data"]["dashboard_config"][0]["dashboard_type"], + "SIMPLE_DASHBOARD", + ) + def test_course_statistics_deny_not_allowed_user(self): # GIVEN disallowed_user = create_user("1337_hacker_schorsch")