""" notifications views file """ # django imports from django.db.models import Q from rest_framework.decorators import action from rest_framework.permissions import IsAuthenticated, AllowAny from rest_framework.response import Response from rest_framework import viewsets, status, views # local imports from account.utils import custom_response, custom_error_response from base.messages import SUCCESS_CODE, ERROR_CODE from base.pagination import CustomPageNumberPagination from base.tasks import notify_task_expiry, notify_top_junior from notifications.constants import TEST_NOTIFICATION from notifications.serializers import RegisterDevice, NotificationListSerializer, ReadNotificationSerializer from notifications.utils import send_notification from notifications.models import Notification class NotificationViewSet(viewsets.GenericViewSet): """ used to do the notification actions """ serializer_class = NotificationListSerializer permission_classes = [IsAuthenticated, ] def list(self, request, *args, **kwargs) -> Response: """ to list user's notifications :param request: :return: """ queryset = Notification.objects.filter(notification_to_id=request.auth.payload['user_id'] ).select_related('notification_to').order_by('-updated_at', '-id') paginator = CustomPageNumberPagination() paginated_queryset = paginator.paginate_queryset(queryset, request) serializer = self.serializer_class(paginated_queryset, many=True) return paginator.get_paginated_response(serializer.data) @action(methods=['post'], detail=False, url_path='device', url_name='device', serializer_class=RegisterDevice) def fcm_registration(self, request): """ used to save the fcm token """ serializer = self.get_serializer_class()(data=request.data, context={'user_id': request.auth.payload['user_id']}) serializer.is_valid(raise_exception=True) serializer.save() return custom_response(SUCCESS_CODE["3000"]) @action(methods=['get'], detail=False, url_path='test', url_name='test') def send_test_notification(self, request): """ to test send notification, task expiry, top junior :return: """ notify_task_expiry() notify_top_junior() notification_type = request.query_params.get('type', TEST_NOTIFICATION) from_user_type = request.query_params.get('from_user_type') send_notification(int(notification_type), None, from_user_type, request.auth.payload['user_id'], {}) if notification_type and request.query_params.get('clear_all'): Notification.objects.filter(notification_type=notification_type).delete() return custom_response(SUCCESS_CODE["3000"]) @action(methods=['patch'], url_path='mark-as-read', url_name='mark-as-read', detail=False, serializer_class=ReadNotificationSerializer) def mark_as_read(self, request, *args, **kwargs): """ notification list """ if request.data.get('id'): Notification.objects.filter(id__in=request.data.get('id')).update(is_read=True) elif request.query_params.get('mark_all'): Notification.objects.filter(notification_to_id=request.auth.payload['user_id']).update(is_read=True) elif request.query_params.get('clear_all'): Notification.objects.filter(notification_to_id=request.auth.payload['user_id']).delete() return custom_response(SUCCESS_CODE['3039'], response_status=status.HTTP_200_OK)