Source code for api.notifications.views

from django.db.models import BooleanField, Exists, OuterRef, QuerySet
from rest_framework.generics import GenericAPIView, ListAPIView
from rest_framework.pagination import LimitOffsetPagination
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.status import HTTP_204_NO_CONTENT

from api.notifications.serializers import GetNotificationSerializer
from apps.notifications.models import Notification
from services.notifications.notification import NotificationService


[docs] class NotificationAPIView(ListAPIView): """Notification APIView""" queryset = Notification.objects.select_related("sender", "post").order_by("-created_at") serializer_class = GetNotificationSerializer permission_classes = [IsAuthenticated] pagination_class = LimitOffsetPagination service_class = NotificationService
[docs] def get_queryset(self) -> QuerySet[Notification]: queryset = super().get_queryset() is_following_subquery = self.request.user.following.filter(pk=OuterRef("sender_id")) # type: ignore queryset = queryset.annotate( is_current_user_following=Exists(is_following_subquery, output_field=BooleanField()) ).filter(receiver=self.request.user) return queryset
[docs] class ReadNotificationAPIView(GenericAPIView): queryset = Notification.objects.all() permission_classes = [IsAuthenticated] service_class = NotificationService
[docs] def post(self, request, *args, **kwargs): self.service_class.mark_as_read_notification(request.user) return Response(status=HTTP_204_NO_CONTENT)