Skip to content

Commit

Permalink
chore: change user hyperlink field
Browse files Browse the repository at this point in the history
  • Loading branch information
Topvennie committed Mar 6, 2024
1 parent d7ef235 commit 53447e2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 20 deletions.
16 changes: 3 additions & 13 deletions backend/notifications/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
14 changes: 8 additions & 6 deletions backend/notifications/signals.py
Original file line number Diff line number Diff line change
@@ -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()

Expand All @@ -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():
Expand All @@ -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}
2 changes: 1 addition & 1 deletion backend/notifications/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 53447e2

Please sign in to comment.