From 53447e272625aa0acef5198b4d47ce29ca8062eb Mon Sep 17 00:00:00 2001 From: Topvennie Date: Wed, 6 Mar 2024 14:20:53 +0100 Subject: [PATCH] chore: change user hyperlink field --- backend/notifications/serializers.py | 16 +++------------- backend/notifications/signals.py | 14 ++++++++------ backend/notifications/views.py | 2 +- 3 files changed, 12 insertions(+), 20 deletions(-) diff --git a/backend/notifications/serializers.py b/backend/notifications/serializers.py index 52482ca9..4a24cd59 100644 --- a/backend/notifications/serializers.py +++ b/backend/notifications/serializers.py @@ -13,21 +13,11 @@ class Meta: fields = "__all__" -# Hyper linked user field that returns a hyper link but expects an id -class UserHyperLinkedRelatedField(serializers.HyperlinkedRelatedField): - view_name = "user-detail" - queryset = User.objects.all() - - def to_internal_value(self, data): - try: - return self.queryset.get(pk=data) - except User.DoesNotExist: - self.fail("no_match") - - class NotificationSerializer(serializers.ModelSerializer): # Hyper linked user field - user = UserHyperLinkedRelatedField() + user = serializers.HyperlinkedRelatedField( + view_name="user-detail", queryset=User.objects.all() + ) # Translate template and arguments into a message message = serializers.SerializerMethodField() diff --git a/backend/notifications/signals.py b/backend/notifications/signals.py index 52b0a84e..2ded382f 100644 --- a/backend/notifications/signals.py +++ b/backend/notifications/signals.py @@ -1,14 +1,12 @@ from __future__ import annotations -import io from enum import Enum from typing import Dict from authentication.models import User from django.dispatch import Signal, receiver -from notifications.models import Notification, NotificationTemplate +from django.urls import reverse from notifications.serializers import NotificationSerializer -from rest_framework.parsers import JSONParser notification_create = Signal() @@ -18,7 +16,11 @@ def notification_creation( type: NotificationType, user: User, arguments: Dict[str, str], **kwargs ) -> bool: serializer = NotificationSerializer( - data={"template_id": type.value, "user": user.id, "arguments": arguments} + data={ + "template_id": type.value, + "user": reverse("user-detail", kwargs={"pk": user.id}), + "arguments": arguments, + } ) if not serializer.is_valid(): @@ -30,5 +32,5 @@ def notification_creation( class NotificationType(Enum): - SCORE_ADDED = 1 # Arguments: {"score": int} - SCORE_UPDATED = 2 # Arguments: {"score": int} + SCORE_ADDED = 1 # Arguments: {"score": int} + SCORE_UPDATED = 2 # Arguments: {"score": int} diff --git a/backend/notifications/views.py b/backend/notifications/views.py index c02df3c0..5f4dd772 100644 --- a/backend/notifications/views.py +++ b/backend/notifications/views.py @@ -16,7 +16,7 @@ class NotificationPermission(BasePermission): # The user can only access their own notifications # An admin can access all notifications def has_permission(self, request: Request, view: NotificationView) -> bool: - return view.kwargs.get("user_id") == request.user.id + return view.kwargs.get("user_id") == request.user.id or request.user.is_staff class NotificationView(APIView):