Files
goodtimes/manage_notifications/api/views.py

140 lines
5.0 KiB
Python
Raw Permalink Normal View History

2024-03-14 13:48:41 +05:30
from rest_framework import status
from rest_framework.views import APIView
from django.conf import settings
2024-05-07 22:26:13 +05:30
from rest_framework import generics
2024-03-14 13:48:41 +05:30
from manage_notifications.api.serializers import (
IAmPrincipalNotificationSettingsSerializer,
2024-05-07 22:26:13 +05:30
InAppNotificationSerializer,
2024-03-14 13:48:41 +05:30
)
from manage_notifications.models import (
IAmPrincipalNotificationSettings,
2024-05-07 22:26:13 +05:30
InAppNotification,
2024-03-14 13:48:41 +05:30
NotificationCategoryChoices,
)
from goodtimes import constants
2024-05-07 22:26:13 +05:30
from rest_framework.pagination import LimitOffsetPagination
2024-03-14 13:48:41 +05:30
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,
)
2024-05-07 22:26:13 +05:30
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,
)
2024-05-08 20:34:52 +05:30
class DeletePlayerID(APIView):
authentication_classes = [JWTAuthentication]
permission_classes = [IsAuthenticated]
def post(self, request):
2024-05-08 20:45:36 +05:30
user = request.user
2024-05-08 20:34:52 +05:30
try:
2024-05-08 20:45:36 +05:30
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
2024-05-08 20:40:57 +05:30
print("Deleted..>>>Player ID")
2024-05-08 20:34:52 +05:30
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,
)