-
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 branch 'development' of github.com:SELab-2/UGent-7 into prod_env
- Loading branch information
Showing
17 changed files
with
192 additions
and
56 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 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,10 @@ | ||
from rest_framework.permissions import BasePermission | ||
from rest_framework.request import Request | ||
from rest_framework.viewsets import ViewSet | ||
|
||
|
||
class NotificationPermission(BasePermission): | ||
# The user can only access their own notifications | ||
# An admin can access all notifications | ||
def has_permission(self, request: Request, view: ViewSet) -> bool: | ||
return view.kwargs.get("pk") == request.user.id or request.user.is_staff |
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
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 |
---|---|---|
@@ -1,9 +1,37 @@ | ||
from rest_framework.viewsets import GenericViewSet | ||
from rest_framework.mixins import ListModelMixin, RetrieveModelMixin | ||
from api.permissions.notification_permissions import NotificationPermission | ||
from authentication.models import User | ||
from authentication.serializers import UserSerializer | ||
from notifications.models import Notification | ||
from notifications.serializers import NotificationSerializer | ||
from rest_framework.decorators import action | ||
from rest_framework.mixins import ListModelMixin, RetrieveModelMixin | ||
from rest_framework.request import Request | ||
from rest_framework.response import Response | ||
from rest_framework.status import HTTP_200_OK | ||
from rest_framework.viewsets import GenericViewSet | ||
|
||
|
||
class UserViewSet(ListModelMixin, RetrieveModelMixin, GenericViewSet): | ||
queryset = User.objects.all() | ||
serializer_class = UserSerializer | ||
|
||
@action(detail=True, methods=["get"], permission_classes=[NotificationPermission]) | ||
def notifications(self, request: Request, pk: str): | ||
notifications = Notification.objects.filter(user=pk) | ||
serializer = NotificationSerializer( | ||
notifications, many=True, context={"request": request} | ||
) | ||
|
||
return Response(serializer.data) | ||
|
||
@action( | ||
detail=True, | ||
methods=["post"], | ||
permission_classes=[NotificationPermission], | ||
url_path="notifications/read", | ||
) | ||
def read(self, request: Request, pk: str): | ||
notifications = Notification.objects.filter(user=pk) | ||
notifications.update(is_read=True) | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1 @@ | ||
from django.urls import path | ||
from notifications.views import NotificationView | ||
|
||
urlpatterns = [ | ||
path("tmp/<str:user_id>/", NotificationView.as_view(), name="notification-detail"), | ||
] | ||
urlpatterns = [] |
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,37 +0,0 @@ | ||
from __future__ import annotations | ||
|
||
from typing import List | ||
|
||
from notifications.models import Notification | ||
from notifications.serializers import NotificationSerializer | ||
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 | ||
|
||
|
||
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) | ||
|
||
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
Binary file not shown.
Oops, something went wrong.