From ff67b4bfbcefbf0eb7654bbc965c3b5a3f5eef8d Mon Sep 17 00:00:00 2001 From: Topvennie Date: Tue, 21 May 2024 15:28:39 +0200 Subject: [PATCH] chore: moved notification sender --- backend/api/serializers/group_serializer.py | 11 ----------- backend/api/views/group_view.py | 17 +++++++++++++++++ 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/backend/api/serializers/group_serializer.py b/backend/api/serializers/group_serializer.py index b6f95d61..b262889c 100644 --- a/backend/api/serializers/group_serializer.py +++ b/backend/api/serializers/group_serializer.py @@ -1,7 +1,5 @@ -from api.models.assistant import Assistant from api.models.group import Group from api.models.student import Student -from api.models.teacher import Teacher from api.permissions.role_permissions import (is_assistant, is_student, is_teacher) from api.serializers.project_serializer import ProjectSerializer @@ -57,15 +55,6 @@ def validate(self, attrs): if "score" in attrs and attrs["score"] > group.project.max_score: raise ValidationError(gettext("group.errors.score_exceeds_max")) - if "score" in attrs and (group.score is None or attrs["score"] != group.score): - # Score is updated -> send notification - notification_create.send( - sender=Group, - type=NotificationType.SCORE_UPDATED, - queryset=list(group.students.all()), - arguments={"score": attrs["score"]}, - ) - return attrs diff --git a/backend/api/views/group_view.py b/backend/api/views/group_view.py index 7c118bcd..cf300cfd 100644 --- a/backend/api/views/group_view.py +++ b/backend/api/views/group_view.py @@ -9,6 +9,7 @@ from api.serializers.submission_serializer import SubmissionSerializer from django.utils.translation import gettext from drf_yasg.utils import swagger_auto_schema +from notifications.signals import NotificationType, notification_create from rest_framework.decorators import action from rest_framework.mixins import (CreateModelMixin, DestroyModelMixin, RetrieveModelMixin, UpdateModelMixin) @@ -28,6 +29,22 @@ class GroupViewSet(CreateModelMixin, serializer_class = GroupSerializer permission_classes = [IsAdminUser | GroupPermission] + def update(self, request, *args, **kwargs): + old_group = self.get_object() + response = super().update(request, *args, **kwargs) + if response.status_code == 200: + new_group = self.get_object() + if "score" in request.data and old_group.score != new_group.score: + # Partial updates end up in the update function as well + notification_create.send( + sender=Group, + type=NotificationType.SCORE_UPDATED, + queryset=list(new_group.students.all()), + arguments={"score": str(new_group.score)}, + ) + + return response + @action(detail=True, methods=["get"], permission_classes=[IsAdminUser | GroupStudentPermission]) def students(self, request, **_): """Returns a list of students for the given group"""