-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[api] Added API endpoints for notifications #3
Added API endpoint for listing and marking notifications as read.
- Loading branch information
Showing
16 changed files
with
560 additions
and
7 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
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,44 @@ | ||
from django.urls import reverse | ||
from openwisp_notifications.swapper import load_model | ||
from openwisp_notifications.utils import _get_absolute_url | ||
from rest_framework import serializers | ||
|
||
Notification = load_model('Notification') | ||
|
||
|
||
class ContentTypeField(serializers.Field): | ||
def to_representation(self, obj): | ||
return obj.model | ||
|
||
|
||
class NotificationSerializer(serializers.ModelSerializer): | ||
notification_url = serializers.SerializerMethodField() | ||
actor_content_type = ContentTypeField(read_only=True) | ||
target_content_type = ContentTypeField(read_only=True) | ||
action_object_content_type = ContentTypeField(read_only=True) | ||
|
||
class Meta: | ||
model = Notification | ||
exclude = ['description', 'deleted', 'public'] | ||
extra_fields = ['message', 'email_subject'] | ||
|
||
def get_field_names(self, declared_fields, info): | ||
model_fields = super().get_field_names(declared_fields, info) | ||
|
||
if getattr(self.Meta, 'extra_fields', None): | ||
return model_fields + self.Meta.extra_fields | ||
else: | ||
return model_fields | ||
|
||
def get_notification_url(self, obj): | ||
url = reverse( | ||
f'admin:{Notification._meta.app_label}_{Notification._meta.model_name}_change', | ||
args=(obj.id,), | ||
) | ||
return _get_absolute_url(url) | ||
|
||
|
||
class NotificationListSerializer(NotificationSerializer): | ||
class Meta(NotificationSerializer.Meta): | ||
fields = ['id', 'message', 'unread', 'notification_url', 'email_subject'] | ||
exclude = None |
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,14 @@ | ||
from django.urls import path | ||
from openwisp_notifications.api import views | ||
|
||
app_name = 'openwisp_notifications' | ||
|
||
|
||
def get_api_urls(api_views=None): | ||
if not api_views: | ||
api_views = views | ||
return [ | ||
path('', views.notifications_list, name='notifications_list'), | ||
path('read/', views.notifications_read_all, name='notifications_read_all'), | ||
path('<uuid:pk>/', views.notification_detail, 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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from django_filters.rest_framework import DjangoFilterBackend | ||
from openwisp_notifications.api.serializers import ( | ||
NotificationListSerializer, | ||
NotificationSerializer, | ||
) | ||
from openwisp_notifications.swapper import load_model | ||
from rest_framework import status | ||
from rest_framework.authentication import SessionAuthentication | ||
from rest_framework.generics import GenericAPIView, RetrieveDestroyAPIView | ||
from rest_framework.mixins import ListModelMixin | ||
from rest_framework.pagination import PageNumberPagination | ||
from rest_framework.permissions import DjangoModelPermissions | ||
from rest_framework.response import Response | ||
|
||
from openwisp_users.api.authentication import BearerAuthentication | ||
|
||
Notification = load_model('Notification') | ||
|
||
|
||
class NotificationPaginator(PageNumberPagination): | ||
page_size = 10 | ||
page_size_query_param = 'page_size' | ||
max_page_size = 100 | ||
|
||
|
||
class BaseNotificationView(GenericAPIView): | ||
model = Notification | ||
authentication_classes = [BearerAuthentication, SessionAuthentication] | ||
permission_classes = [DjangoModelPermissions] | ||
queryset = Notification.objects.all() | ||
|
||
def get_queryset(self): | ||
return self.queryset.filter(recipient=self.request.user) | ||
|
||
|
||
class NotificationListView(BaseNotificationView, ListModelMixin): | ||
serializer_class = NotificationListSerializer | ||
pagination_class = NotificationPaginator | ||
filter_backends = [DjangoFilterBackend] | ||
filterset_fields = ['unread'] | ||
|
||
def get(self, request, *args, **kwargs): | ||
return self.list(request, *args, **kwargs) | ||
|
||
|
||
class NotificationDetailView(BaseNotificationView, RetrieveDestroyAPIView): | ||
serializer_class = NotificationSerializer | ||
lookup_field = 'pk' | ||
|
||
def patch(self, request, pk): | ||
return self._mark_notification_read(pk) | ||
|
||
def _mark_notification_read(self, notification_id): | ||
notification = self.get_object() | ||
notification.mark_as_read() | ||
return Response(status=status.HTTP_200_OK,) | ||
|
||
|
||
class NotificationReadAllView(BaseNotificationView): | ||
def post(self, request, *args, **kwargs): | ||
queryset = self.get_queryset() | ||
queryset.update(unread=False) | ||
return Response(status=status.HTTP_200_OK) | ||
|
||
|
||
notifications_list = NotificationListView.as_view() | ||
notification_detail = NotificationDetailView.as_view() | ||
notifications_read_all = NotificationReadAllView.as_view() |
Oops, something went wrong.