Skip to content

Commit

Permalink
chore: moved notification sender
Browse files Browse the repository at this point in the history
  • Loading branch information
Topvennie committed May 21, 2024
1 parent 0efb577 commit ff67b4b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
11 changes: 0 additions & 11 deletions backend/api/serializers/group_serializer.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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


Expand Down
17 changes: 17 additions & 0 deletions backend/api/views/group_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"""
Expand Down

0 comments on commit ff67b4b

Please sign in to comment.