Files
goodtimes/manage_notifications/api/views.py
2024-05-08 20:45:36 +05:30

140 lines
5.0 KiB
Python

from rest_framework import status
from rest_framework.views import APIView
from django.conf import settings
from rest_framework import generics
from manage_notifications.api.serializers import (
IAmPrincipalNotificationSettingsSerializer,
InAppNotificationSerializer,
)
from manage_notifications.models import (
IAmPrincipalNotificationSettings,
InAppNotification,
NotificationCategoryChoices,
)
from goodtimes import constants
from rest_framework.pagination import LimitOffsetPagination
from goodtimes.utils import ApiResponse
from rest_framework.permissions import IsAuthenticated
from rest_framework_simplejwt.authentication import JWTAuthentication
class NotificationSettingToggle(APIView):
authentication_classes = [JWTAuthentication]
permission_classes = [IsAuthenticated]
def post(self, request, *args, **kwargs):
notification_category = request.data.get("notification_category")
enable = request.data.get("enable", True)
if (
not notification_category
or notification_category not in NotificationCategoryChoices.values
):
return ApiResponse.error(
errors="Invalid notification category",
message=constants.FAILURE,
status=status.HTTP_400_BAD_REQUEST,
)
# Assuming IAmPrincipal model has a user field that relates to Django's User model
# You might need to adjust the query depending on your user-principal relationship
notification_setting, created = (
IAmPrincipalNotificationSettings.objects.get_or_create(
principal=request.user,
notification_category=notification_category,
defaults={"is_enabled": enable},
)
)
if not created:
notification_setting.is_enabled = enable
notification_setting.save()
return ApiResponse.success(
data=f"{notification_category}: { enable}.",
message=constants.SUCCESS,
status=status.HTTP_200_OK,
)
class UserNotificationsAPIView(APIView):
authentication_classes = [JWTAuthentication]
permission_classes = [IsAuthenticated]
def get(self, request, *args, **kwargs):
# Assuming your IAmPrincipal model has a user field linking to the User model
principal = request.user
notifications = (
IAmPrincipalNotificationSettings.objects.filter(principal=principal)
.exclude(notification_category=NotificationCategoryChoices.GENERAL)
.exclude(notification_category=NotificationCategoryChoices.PROMOTIONS)
)
serializer = IAmPrincipalNotificationSettingsSerializer(
notifications, many=True
)
return ApiResponse.success(
data=serializer.data,
message=constants.SUCCESS,
status=status.HTTP_200_OK,
)
class InAppNotificationListAPIView(generics.ListAPIView):
serializer_class = InAppNotificationSerializer
pagination_class = LimitOffsetPagination # Add this line
permission_classes = [IsAuthenticated]
authentication_classes = [JWTAuthentication]
def get_queryset(self):
queryset = InAppNotification.objects.filter(
principal=self.request.user, deleted=False, active=True
)
return queryset
def list(self, request, *args, **kwargs):
queryset = self.filter_queryset(self.get_queryset())
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)
serializer = self.get_serializer(queryset, many=True)
return ApiResponse.success(
message="InAppNotifications retrieved successfully",
data=serializer.data,
status=status.HTTP_200_OK,
)
class DeletePlayerID(APIView):
authentication_classes = [JWTAuthentication]
permission_classes = [IsAuthenticated]
def post(self, request):
user = request.user
try:
if user.player_id:
print("request.user.player_id: ", user.player_id)
user.player_id = None # Set player_id to None
user.save() # Save the user instance to update the database
print("Deleted..>>>Player ID")
return ApiResponse.success(
message=constants.SUCCESS,
data="Player ID Deleted Successfully!",
status=status.HTTP_200_OK,
)
else:
return ApiResponse.error(
message=constants.FAILURE,
errors="User does not have a player ID",
status=status.HTTP_400_BAD_REQUEST,
)
except AttributeError as exception:
return ApiResponse.error(
message=constants.FAILURE,
errors=f"Error deleting player ID: {str(exception)}",
status=status.HTTP_400_BAD_REQUEST,
)