-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from SELab-2/notifications
Notifications
- Loading branch information
Showing
9 changed files
with
143 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from __future__ import annotations | ||
|
||
from enum import Enum | ||
from typing import Dict | ||
|
||
from authentication.models import User | ||
from django.dispatch import Signal, receiver | ||
from django.urls import reverse | ||
from notifications.serializers import NotificationSerializer | ||
|
||
notification_create = Signal() | ||
|
||
|
||
@receiver(notification_create) | ||
def notification_creation( | ||
type: NotificationType, user: User, arguments: Dict[str, str], **kwargs | ||
) -> bool: | ||
serializer = NotificationSerializer( | ||
data={ | ||
"template_id": type.value, | ||
"user": reverse("user-detail", kwargs={"pk": user.id}), | ||
"arguments": arguments, | ||
} | ||
) | ||
|
||
if not serializer.is_valid(): | ||
return False | ||
|
||
serializer.save() | ||
|
||
return True | ||
|
||
|
||
class NotificationType(Enum): | ||
SCORE_ADDED = 1 # Arguments: {"score": int} | ||
SCORE_UPDATED = 2 # Arguments: {"score": int} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
from notifications.views import NotificationViewSet | ||
from rest_framework.routers import DefaultRouter | ||
from django.urls import path | ||
from notifications.views import NotificationView | ||
|
||
router = DefaultRouter() | ||
|
||
router.register(r"", NotificationViewSet, basename="notification") | ||
|
||
urlpatterns = router.urls | ||
urlpatterns = [ | ||
path("<str:user_id>/", NotificationView.as_view(), name="notification-detail"), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,38 @@ | ||
from __future__ import annotations | ||
|
||
from typing import List | ||
|
||
from notifications.models import Notification | ||
from notifications.serializers import NotificationSerializer | ||
from rest_framework.viewsets import ModelViewSet | ||
from rest_framework.permissions import BasePermission, IsAuthenticated | ||
from rest_framework.request import Request | ||
from rest_framework.response import Response | ||
from rest_framework.status import HTTP_200_OK | ||
from rest_framework.views import APIView | ||
|
||
|
||
# TODO: Give admin access to everything | ||
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 or request.user.is_staff | ||
|
||
|
||
class NotificationView(APIView): | ||
permission_classes: List[BasePermission] = [IsAuthenticated, NotificationPermission] | ||
|
||
def get(self, request: Request, user_id: str) -> Response: | ||
notifications = Notification.objects.filter(user=user_id) | ||
serializer = NotificationSerializer( | ||
notifications, many=True, context={"request": request} | ||
) | ||
|
||
return Response(serializer.data) | ||
|
||
# Mark all notifications as read for the user | ||
def post(self, request: Request, user_id: str) -> Response: | ||
notifications = Notification.objects.filter(user=user_id) | ||
notifications.update(is_read=True) | ||
|
||
class NotificationViewSet(ModelViewSet): | ||
queryset = Notification.objects.all() | ||
serializer_class = NotificationSerializer | ||
return Response(status=HTTP_200_OK) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters