mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-11-27 09:04:54 +00:00
notifications API
This commit is contained in:
@ -142,7 +142,9 @@ SUCCESS_CODE = {
|
|||||||
"3033": "Valid Referral code",
|
"3033": "Valid Referral code",
|
||||||
"3034": "Invite guardian successfully",
|
"3034": "Invite guardian successfully",
|
||||||
"3035": "Task started successfully",
|
"3035": "Task started successfully",
|
||||||
"3036": "Task reassign successfully"
|
"3036": "Task reassign successfully",
|
||||||
|
# notification read
|
||||||
|
"3037": "Notification read successfully",
|
||||||
}
|
}
|
||||||
"""status code error"""
|
"""status code error"""
|
||||||
STATUS_CODE_ERROR = {
|
STATUS_CODE_ERROR = {
|
||||||
|
|||||||
@ -2,7 +2,8 @@
|
|||||||
"""Django import"""
|
"""Django import"""
|
||||||
from django.urls import path, include
|
from django.urls import path, include
|
||||||
from .views import (SignupViewset, UpdateGuardianProfile, AllTaskListAPIView, CreateTaskAPIView, TaskListAPIView,
|
from .views import (SignupViewset, UpdateGuardianProfile, AllTaskListAPIView, CreateTaskAPIView, TaskListAPIView,
|
||||||
SearchTaskListAPIView, TopJuniorListAPIView, ApproveJuniorAPIView, ApproveTaskAPIView)
|
SearchTaskListAPIView, TopJuniorListAPIView, ApproveJuniorAPIView, ApproveTaskAPIView,
|
||||||
|
GuardianListAPIView)
|
||||||
"""Third party import"""
|
"""Third party import"""
|
||||||
from rest_framework import routers
|
from rest_framework import routers
|
||||||
|
|
||||||
@ -36,6 +37,8 @@ router.register('filter-task', SearchTaskListAPIView, basename='filter-task')
|
|||||||
router.register('approve-junior', ApproveJuniorAPIView, basename='approve-junior')
|
router.register('approve-junior', ApproveJuniorAPIView, basename='approve-junior')
|
||||||
# Approve junior API"""
|
# Approve junior API"""
|
||||||
router.register('approve-task', ApproveTaskAPIView, basename='approve-task')
|
router.register('approve-task', ApproveTaskAPIView, basename='approve-task')
|
||||||
|
# guardian list API"""
|
||||||
|
router.register('guardian-list', GuardianListAPIView, basename='guardian-list')
|
||||||
# Define Url pattern"""
|
# Define Url pattern"""
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('api/v1/', include(router.urls)),
|
path('api/v1/', include(router.urls)),
|
||||||
|
|||||||
@ -328,3 +328,33 @@ class ApproveTaskAPIView(viewsets.ViewSet):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
return custom_error_response(str(e), response_status=status.HTTP_400_BAD_REQUEST)
|
return custom_error_response(str(e), response_status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
class GuardianListAPIView(viewsets.ModelViewSet):
|
||||||
|
"""Junior list of assosicated guardian"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
# serializer_class = JuniorDetailListSerializer
|
||||||
|
# queryset = Junior.objects.all()
|
||||||
|
# permission_classes = [IsAuthenticated]
|
||||||
|
# filter_backends = (SearchFilter,)
|
||||||
|
# search_fields = ['auth__first_name', 'auth__last_name']
|
||||||
|
# http_method_names = ('get',)
|
||||||
|
#
|
||||||
|
# def get_queryset(self):
|
||||||
|
# queryset = self.filter_queryset(self.queryset)
|
||||||
|
# return queryset
|
||||||
|
#
|
||||||
|
# def list(self, request, *args, **kwargs):
|
||||||
|
# """ junior list"""
|
||||||
|
# try:
|
||||||
|
# update_positions_based_on_points()
|
||||||
|
# guardian_data = Guardian.objects.filter(user__email=request.user).last()
|
||||||
|
# # fetch junior object
|
||||||
|
# if guardian_data:
|
||||||
|
# queryset = self.get_queryset()
|
||||||
|
# queryset = queryset.filter(guardian_code__icontains=str(guardian_data.guardian_code))
|
||||||
|
# # use JuniorDetailListSerializer serializer
|
||||||
|
# serializer = JuniorDetailListSerializer(queryset, many=True)
|
||||||
|
# return custom_response(None, serializer.data, response_status=status.HTTP_200_OK)
|
||||||
|
# return custom_error_response(ERROR_CODE['2045'], response_status=status.HTTP_200_OK)
|
||||||
|
# except Exception as e:
|
||||||
|
# return custom_error_response(str(e), response_status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|||||||
@ -6,7 +6,7 @@ from rest_framework import serializers
|
|||||||
|
|
||||||
# local imports
|
# local imports
|
||||||
from notifications.utils import register_fcm_token
|
from notifications.utils import register_fcm_token
|
||||||
|
from notifications.models import Notification
|
||||||
|
|
||||||
class RegisterDevice(serializers.Serializer):
|
class RegisterDevice(serializers.Serializer):
|
||||||
"""
|
"""
|
||||||
@ -26,3 +26,22 @@ class RegisterDevice(serializers.Serializer):
|
|||||||
device_type = validated_data['type']
|
device_type = validated_data['type']
|
||||||
return register_fcm_token(self.context['user_id'], registration_id,
|
return register_fcm_token(self.context['user_id'], registration_id,
|
||||||
validated_data['device_id'], device_type)
|
validated_data['device_id'], device_type)
|
||||||
|
|
||||||
|
class NotificationListSerailizer(serializers.ModelSerializer):
|
||||||
|
"""List of notification"""
|
||||||
|
|
||||||
|
class Meta(object):
|
||||||
|
"""meta info"""
|
||||||
|
model = Notification
|
||||||
|
fields = ['id', 'data', 'is_read']
|
||||||
|
|
||||||
|
class ReadNotificationSerializer(serializers.ModelSerializer):
|
||||||
|
"""User task Serializer"""
|
||||||
|
class Meta(object):
|
||||||
|
"""Meta class"""
|
||||||
|
model = Notification
|
||||||
|
fields = ('id',)
|
||||||
|
def update(self, instance, validated_data):
|
||||||
|
instance.is_read = True
|
||||||
|
instance.save()
|
||||||
|
return instance
|
||||||
|
|||||||
@ -6,7 +6,7 @@ from django.urls import path, include
|
|||||||
from rest_framework import routers
|
from rest_framework import routers
|
||||||
|
|
||||||
# local imports
|
# local imports
|
||||||
from notifications.views import NotificationViewSet
|
from notifications.views import NotificationViewSet, ReadNotification
|
||||||
|
|
||||||
# initiate router
|
# initiate router
|
||||||
router = routers.SimpleRouter()
|
router = routers.SimpleRouter()
|
||||||
@ -15,4 +15,5 @@ router.register('notifications', NotificationViewSet, basename='notifications')
|
|||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('api/v1/', include(router.urls)),
|
path('api/v1/', include(router.urls)),
|
||||||
|
path('api/v1/read-notification/', ReadNotification.as_view()),
|
||||||
]
|
]
|
||||||
|
|||||||
@ -3,18 +3,17 @@ notifications views file
|
|||||||
"""
|
"""
|
||||||
# django imports
|
# django imports
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
from rest_framework import viewsets
|
|
||||||
from rest_framework.decorators import action
|
from rest_framework.decorators import action
|
||||||
from rest_framework.permissions import IsAuthenticated
|
from rest_framework.permissions import IsAuthenticated
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
|
from rest_framework import viewsets, status, views
|
||||||
# local imports
|
# local imports
|
||||||
from account.utils import custom_response
|
from account.utils import custom_response, custom_error_response
|
||||||
from base.messages import SUCCESS_CODE
|
from base.messages import SUCCESS_CODE, ERROR_CODE
|
||||||
from notifications.constants import TEST_NOTIFICATION
|
from notifications.constants import TEST_NOTIFICATION
|
||||||
from notifications.serializers import RegisterDevice
|
from notifications.serializers import RegisterDevice, NotificationListSerailizer, ReadNotificationSerializer
|
||||||
from notifications.utils import send_notification
|
from notifications.utils import send_notification
|
||||||
|
from notifications.models import Notification
|
||||||
|
|
||||||
class NotificationViewSet(viewsets.GenericViewSet):
|
class NotificationViewSet(viewsets.GenericViewSet):
|
||||||
""" used to do the notification actions """
|
""" used to do the notification actions """
|
||||||
@ -40,3 +39,38 @@ class NotificationViewSet(viewsets.GenericViewSet):
|
|||||||
"""
|
"""
|
||||||
send_notification.delay(TEST_NOTIFICATION, None, request.auth.payload['user_id'], {})
|
send_notification.delay(TEST_NOTIFICATION, None, request.auth.payload['user_id'], {})
|
||||||
return custom_response(SUCCESS_CODE["3000"])
|
return custom_response(SUCCESS_CODE["3000"])
|
||||||
|
|
||||||
|
@action(methods=['get'], detail=False, url_path='list', url_name='list',
|
||||||
|
serializer_class=NotificationListSerailizer)
|
||||||
|
def notification_list(self, request):
|
||||||
|
"""
|
||||||
|
notification list
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
queryset = Notification.objects.filter(notification_to=request.user)
|
||||||
|
serializer = NotificationListSerailizer(queryset, many=True)
|
||||||
|
return custom_response(None, serializer.data, response_status=status.HTTP_200_OK)
|
||||||
|
except Exception as e:
|
||||||
|
return custom_error_response(str(e), response_status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
|
||||||
|
class ReadNotification(views.APIView):
|
||||||
|
"""Update notification API"""
|
||||||
|
serializer_class = ReadNotificationSerializer
|
||||||
|
model = Notification
|
||||||
|
permission_classes = [IsAuthenticated]
|
||||||
|
|
||||||
|
def put(self, request, format=None):
|
||||||
|
try:
|
||||||
|
notification_id = str(self.request.data.get('notification_id'))
|
||||||
|
notification_queryset = Notification.objects.filter(id=notification_id, notification_to=self.request.user).last()
|
||||||
|
if notification_queryset:
|
||||||
|
# use ReadNotificationSerializer serializer
|
||||||
|
serializer = ReadNotificationSerializer(notification_queryset, data=request.data, partial=True)
|
||||||
|
if serializer.is_valid():
|
||||||
|
# save serializer
|
||||||
|
serializer.save()
|
||||||
|
return custom_response(SUCCESS_CODE['3037'], response_status=status.HTTP_200_OK)
|
||||||
|
return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
except Exception as e:
|
||||||
|
return custom_error_response(str(e), response_status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|||||||
Reference in New Issue
Block a user