feat: cockpit type
This commit is contained in:
parent
3e2cededc7
commit
e2c32b7fb6
|
|
@ -11,6 +11,7 @@ from django.views.decorators.csrf import csrf_exempt
|
||||||
from django_ratelimit.exceptions import Ratelimited
|
from django_ratelimit.exceptions import Ratelimited
|
||||||
from graphene_django.views import GraphQLView
|
from graphene_django.views import GraphQLView
|
||||||
|
|
||||||
|
from vbv_lernwelt.api.user import get_cockpit_type
|
||||||
from vbv_lernwelt.assignment.views import request_assignment_completion_status
|
from vbv_lernwelt.assignment.views import request_assignment_completion_status
|
||||||
from vbv_lernwelt.core.middleware.auth import django_view_authentication_exempt
|
from vbv_lernwelt.core.middleware.auth import django_view_authentication_exempt
|
||||||
from vbv_lernwelt.core.schema import schema
|
from vbv_lernwelt.core.schema import schema
|
||||||
|
|
@ -124,6 +125,9 @@ urlpatterns = [
|
||||||
path(r"api/course/completion/<signed_int:course_session_id>/<uuid:user_id>/",
|
path(r"api/course/completion/<signed_int:course_session_id>/<uuid:user_id>/",
|
||||||
request_course_completion_for_user,
|
request_course_completion_for_user,
|
||||||
name="request_course_completion_for_user"),
|
name="request_course_completion_for_user"),
|
||||||
|
path(r"api/course/<int:course_id>/cockpit/",
|
||||||
|
get_cockpit_type,
|
||||||
|
name="get_cockpit_type"),
|
||||||
|
|
||||||
path("api/mentor/<signed_int:course_session_id>/", include("vbv_lernwelt.learning_mentor.urls")),
|
path("api/mentor/<signed_int:course_session_id>/", include("vbv_lernwelt.learning_mentor.urls")),
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,59 @@
|
||||||
|
from django.urls import reverse
|
||||||
|
from rest_framework import status
|
||||||
|
from rest_framework.test import APITestCase
|
||||||
|
|
||||||
|
from vbv_lernwelt.course.creators.test_utils import (
|
||||||
|
create_course,
|
||||||
|
create_course_session,
|
||||||
|
create_user,
|
||||||
|
)
|
||||||
|
from vbv_lernwelt.course.models import CourseSessionUser
|
||||||
|
from vbv_lernwelt.learning_mentor.models import LearningMentor
|
||||||
|
|
||||||
|
|
||||||
|
class MeUserViewTest(APITestCase):
|
||||||
|
def setUp(self) -> None:
|
||||||
|
self.course, _ = create_course("Test Course")
|
||||||
|
self.user = create_user("tester")
|
||||||
|
self.url = reverse("get_cockpit_type", kwargs={"course_id": self.course.id})
|
||||||
|
|
||||||
|
def test_no_cockpit(self) -> None:
|
||||||
|
# GIVEN
|
||||||
|
self.client.force_login(self.user)
|
||||||
|
|
||||||
|
# WHEN
|
||||||
|
response = self.client.get(self.url)
|
||||||
|
|
||||||
|
# THEN
|
||||||
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
|
self.assertEquals(response.data["type"], None)
|
||||||
|
|
||||||
|
def test_mentor_cockpit(self) -> None:
|
||||||
|
# GIVEN
|
||||||
|
self.client.force_login(self.user)
|
||||||
|
LearningMentor.objects.create(mentor=self.user, course=self.course)
|
||||||
|
|
||||||
|
# WHEN
|
||||||
|
response = self.client.get(self.url)
|
||||||
|
|
||||||
|
# THEN
|
||||||
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
|
self.assertEquals(response.data["type"], "mentor")
|
||||||
|
|
||||||
|
def test_trainer_cockpit(self) -> None:
|
||||||
|
# GIVEN
|
||||||
|
self.client.force_login(self.user)
|
||||||
|
course_session = create_course_session(course=self.course, title="Test Session")
|
||||||
|
|
||||||
|
CourseSessionUser.objects.create(
|
||||||
|
user=self.user,
|
||||||
|
course_session=course_session,
|
||||||
|
role=CourseSessionUser.Role.EXPERT,
|
||||||
|
)
|
||||||
|
|
||||||
|
# WHEN
|
||||||
|
response = self.client.get(self.url)
|
||||||
|
|
||||||
|
# THEN
|
||||||
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
|
self.assertEquals(response.data["type"], "trainer")
|
||||||
|
|
@ -0,0 +1,27 @@
|
||||||
|
from django.shortcuts import get_object_or_404
|
||||||
|
from rest_framework.decorators import api_view, permission_classes
|
||||||
|
from rest_framework.generics import get_object_or_404
|
||||||
|
from rest_framework.permissions import IsAuthenticated
|
||||||
|
from rest_framework.response import Response
|
||||||
|
|
||||||
|
from vbv_lernwelt.course.models import Course, CourseSessionUser
|
||||||
|
from vbv_lernwelt.learning_mentor.models import LearningMentor
|
||||||
|
|
||||||
|
|
||||||
|
@api_view(["GET"])
|
||||||
|
@permission_classes([IsAuthenticated])
|
||||||
|
def get_cockpit_type(request, course_id: int):
|
||||||
|
course = get_object_or_404(Course, id=course_id)
|
||||||
|
|
||||||
|
cockpit_type = None
|
||||||
|
|
||||||
|
if LearningMentor.objects.filter(mentor=request.user, course=course).exists():
|
||||||
|
cockpit_type = "mentor"
|
||||||
|
elif CourseSessionUser.objects.filter(
|
||||||
|
user=request.user,
|
||||||
|
course_session__course=course,
|
||||||
|
role=CourseSessionUser.Role.EXPERT,
|
||||||
|
).exists():
|
||||||
|
cockpit_type = "trainer"
|
||||||
|
|
||||||
|
return Response({"type": cockpit_type})
|
||||||
|
|
@ -269,7 +269,6 @@ class CourseSessionUser(models.Model):
|
||||||
class Role(models.TextChoices):
|
class Role(models.TextChoices):
|
||||||
MEMBER = "MEMBER", _("Teilnehmer")
|
MEMBER = "MEMBER", _("Teilnehmer")
|
||||||
EXPERT = "EXPERT", _("Experte/Trainer")
|
EXPERT = "EXPERT", _("Experte/Trainer")
|
||||||
TUTOR = "TUTOR", _("Lernbegleitung")
|
|
||||||
|
|
||||||
role = models.CharField(choices=Role.choices, max_length=255, default=Role.MEMBER)
|
role = models.CharField(choices=Role.choices, max_length=255, default=Role.MEMBER)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -153,4 +153,20 @@ def remove_self_from_mentor(request, course_session_id: int, mentor_id: int):
|
||||||
@api_view(["POST"])
|
@api_view(["POST"])
|
||||||
@permission_classes([IsAuthenticated])
|
@permission_classes([IsAuthenticated])
|
||||||
def accept_invitation(request, course_session_id: int):
|
def accept_invitation(request, course_session_id: int):
|
||||||
|
course_session = get_object_or_404(CourseSession, id=course_session_id)
|
||||||
|
invitation = get_object_or_404(
|
||||||
|
MentorInvitation, id=request.data.get("invitation_id")
|
||||||
|
)
|
||||||
|
|
||||||
|
if invitation.participant.course_session != course_session:
|
||||||
|
return Response(
|
||||||
|
data={"message": "Invalid invitation"}, status=status.HTTP_400_BAD_REQUEST
|
||||||
|
)
|
||||||
|
|
||||||
|
mentor, _ = LearningMentor.objects.get_or_create(
|
||||||
|
mentor=request.user, course=course_session.course
|
||||||
|
)
|
||||||
|
|
||||||
|
mentor.participants.add(invitation.participant)
|
||||||
|
|
||||||
return Response({})
|
return Response({})
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue