feat: praxis assignments API

This commit is contained in:
Reto Aebersold 2023-12-07 10:06:17 +01:00
parent 5d4e6983de
commit 56ecb72751
6 changed files with 100 additions and 32 deletions

View File

@ -5,9 +5,9 @@ from vbv_lernwelt.learning_mentor.models import LearningMentor
@admin.register(LearningMentor)
class LearningMentorAdmin(admin.ModelAdmin):
def student_count(self, obj):
return obj.students.count()
def participant_count(self, obj):
return obj.participants.count()
student_count.short_description = "Students"
participant_count.short_description = "Participants"
list_display = ["mentor", "course", "student_count"]
list_display = ["mentor", "course", "participant_count"]

View File

@ -1,4 +1,4 @@
# Generated by Django 3.2.20 on 2023-12-07 07:52
# Generated by Django 3.2.20 on 2023-12-07 08:39
import django.db.models.deletion
from django.conf import settings
@ -9,8 +9,8 @@ class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("course", "0005_course_enable_circle_documents"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
@ -40,10 +40,10 @@ class Migration(migrations.Migration):
),
),
(
"students",
"participants",
models.ManyToManyField(
blank=True,
related_name="students",
related_name="participants",
to="course.CourseSessionUser",
),
),

View File

@ -8,9 +8,9 @@ class LearningMentor(models.Model):
mentor = models.OneToOneField(User, on_delete=models.CASCADE)
course = models.ForeignKey("course.Course", on_delete=models.CASCADE)
students = models.ManyToManyField(
participants = models.ManyToManyField(
CourseSessionUser,
related_name="students",
related_name="participants",
blank=True,
)
@ -22,4 +22,4 @@ class LearningMentor(models.Model):
@property
def course_sessions(self):
return self.students.values_list("course_session", flat=True).distinct()
return self.participants.values_list("course_session", flat=True).distinct()

View File

@ -5,12 +5,12 @@ from django.dispatch import receiver
from .models import LearningMentor
@receiver(m2m_changed, sender=LearningMentor.students.through)
@receiver(m2m_changed, sender=LearningMentor.participants.through)
def validate_student(sender, instance, action, reverse, model, pk_set, **kwargs):
if action == "pre_add":
students = model.objects.filter(pk__in=pk_set)
for student in students:
if student.course_session.course != instance.course:
participants = model.objects.filter(pk__in=pk_set)
for participant in participants:
if participant.course_session.course != instance.course:
raise ValidationError(
"Student (CourseSessionUser) does not match the course for this mentor."
"Participant (CourseSessionUser) does not match the course for this mentor."
)

View File

@ -1,26 +1,89 @@
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
from vbv_lernwelt.course.creators.test_utils import (
add_course_session_user,
create_course,
create_course_session,
create_user,
)
from vbv_lernwelt.course.models import CourseSessionUser
from vbv_lernwelt.learning_mentor.models import LearningMentor
class LearningMentorAPITest(APITestCase):
# def setUp(self) -> None:
# create_default_users()
# create_test_course(with_sessions=True)
#
# self.mentor = User.objects.get(username="expert-vv.expert1@eiger-versicherungen.ch")
def test_api(self) -> None:
# GIVEN
def setUp(self) -> None:
course, course_page = create_course("Test Course")
course_session = create_course_session(course=course, title="Test VV")
self.course_session = create_course_session(course=course, title="Test VV")
self.mentor = create_user("mentor")
self.participant_1 = add_course_session_user(
self.course_session,
create_user("participant_1"),
role=CourseSessionUser.Role.MEMBER,
)
self.participant_2 = add_course_session_user(
self.course_session,
create_user("participant_2"),
role=CourseSessionUser.Role.MEMBER,
)
self.participant_3 = add_course_session_user(
self.course_session,
create_user("participant_3"),
role=CourseSessionUser.Role.MEMBER,
)
# self.client.force_login(self.mentor)
url = reverse("mentor_summary", kwargs={"course_session_id": course_session.id})
self.url = reverse(
"mentor_summary", kwargs={"course_session_id": self.course_session.id}
)
def test_api_no_mentor(self) -> None:
# GIVEN
self.client.force_login(self.mentor)
# WHEN
response = self.client.get(url)
response = self.client.get(self.url)
# THEN
# self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def test_api_no_participants(self) -> None:
# GIVEN
self.client.force_login(self.mentor)
LearningMentor.objects.create(
mentor=self.mentor, course=self.course_session.course
)
# WHEN
response = self.client.get(self.url)
# THEN
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["participants"], [])
self.assertEqual(response.data["praxis_assignments"], [])
def test_api_participants(self) -> None:
# GIVEN
participants = [self.participant_1, self.participant_2, self.participant_3]
self.client.force_login(self.mentor)
mentor = LearningMentor.objects.create(
mentor=self.mentor,
course=self.course_session.course,
)
mentor.participants.set(participants)
# WHEN
response = self.client.get(self.url)
# THEN
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(len(response.data["participants"]), len(participants))
participant_1 = [
p
for p in response.data["participants"]
if p["id"] == str(self.participant_1.user.id)
][0]
self.assertEqual(participant_1["email"], "participant_1@example.com")
self.assertEqual(participant_1["first_name"], "Test")
self.assertEqual(participant_1["last_name"], "Participant_1")

View File

@ -3,6 +3,7 @@ from enum import Enum
from typing import List
from rest_framework.decorators import api_view
from rest_framework.generics import get_object_or_404
from rest_framework.response import Response
from vbv_lernwelt.assignment.models import (
@ -144,11 +145,15 @@ def mentor_summary(request, course_session_id: int):
if request.method == "GET":
course_session = CourseSession.objects.get(id=course_session_id)
participants = LearningMentor.objects.filter(mentor=request.user)
mentor = get_object_or_404(
LearningMentor, mentor=request.user, course=course_session.course
)
participants = mentor.participants.filter(course_session=course_session)
return Response(
{
"participants": [UserSerializer(s).data for s in participants],
"participants": [UserSerializer(p.user).data for p in participants],
"praxis_assignments": get_praxis_assignments(
course_session, participants
),