notification app, api for device registration and fcm token

This commit is contained in:
abutalib-kiwi
2023-07-18 14:06:04 +05:30
parent 04342133ef
commit ba4d5933de
15 changed files with 142 additions and 1 deletions

View File

@ -74,6 +74,7 @@ ERROR_CODE = {
}
"""Success message code"""
SUCCESS_CODE = {
"3000": "ok",
# Success code for password
"3001": "Sign up successfully",
# Success code for Thank you

View File

6
notifications/admin.py Normal file
View File

@ -0,0 +1,6 @@
"""
notification admin file
"""
from django.contrib import admin
# Register your models here.

13
notifications/apps.py Normal file
View File

@ -0,0 +1,13 @@
"""
notification app file
"""
# django imports
from django.apps import AppConfig
class NotificationsConfig(AppConfig):
"""
notification app config
"""
default_auto_field = 'django.db.models.BigAutoField'
name = 'notifications'

View File

6
notifications/models.py Normal file
View File

@ -0,0 +1,6 @@
"""
notification models file
"""
from django.db import models
# Create your models here.

View File

@ -0,0 +1,28 @@
"""
notification serializer file
"""
# third party imports
from rest_framework import serializers
# local imports
from notifications.utils import register_fcm_token
class RegisterDevice(serializers.Serializer):
"""
used to create and validate register device token
"""
registration_id = serializers.CharField(max_length=250)
device_id = serializers.CharField(max_length=250)
type = serializers.ChoiceField(choices=["ios", "web", "android"])
class Meta:
""" meta class """
fields = ('registration_id', 'type', 'device_id')
def create(self, validated_data):
""" override this method to create device token for users """
registration_id = validated_data['registration_id']
device_type = validated_data['type']
return register_fcm_token(self.context['user_id'], registration_id,
validated_data['device_id'], device_type)

6
notifications/tests.py Normal file
View File

@ -0,0 +1,6 @@
"""
notification test file
"""
from django.test import TestCase
# Create your tests here.

18
notifications/urls.py Normal file
View File

@ -0,0 +1,18 @@
"""
notifications urls file
"""
# django imports
from django.urls import path, include
from rest_framework import routers
# local imports
from notifications.views import NotificationViewSet
# initiate router
router = routers.SimpleRouter()
router.register('notifications', NotificationViewSet, basename='notifications')
urlpatterns = [
path('api/v1/', include(router.urls)),
]

17
notifications/utils.py Normal file
View File

@ -0,0 +1,17 @@
"""
notifications utils file
"""
# third party imports
from fcm_django.models import FCMDevice
# local imports
def register_fcm_token(user_id, registration_id, device_id, device_type):
""" used to register the fcm device token"""
device, _ = FCMDevice.objects.update_or_create(device_id=device_id,
defaults={'user_id': user_id, 'type': device_type,
'active': True,
'registration_id': registration_id})
return device

30
notifications/views.py Normal file
View File

@ -0,0 +1,30 @@
"""
notifications views file
"""
# django imports
from django.db.models import Q
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from account.utils import custom_response
from base.messages import SUCCESS_CODE
from notifications.serializers import RegisterDevice
class NotificationViewSet(viewsets.GenericViewSet):
""" used to do the notification actions """
serializer_class = None
permission_classes = [IsAuthenticated, ]
@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"])

View File

@ -79,3 +79,5 @@ uritemplate==4.1.1
urllib3==1.26.16
vine==5.0.0
wcwidth==0.2.6
fcm-django==2.0.0

View File

@ -11,7 +11,7 @@ from web_admin.views import ArticleViewSet
# initiate router
router = routers.SimpleRouter()
router.register('article', ArticleViewSet, basename='article')
router.register('articles', ArticleViewSet, basename='articles')
urlpatterns = [
path('api/v1/', include(router.urls)),

View File

@ -54,6 +54,7 @@ INSTALLED_APPS = [
'corsheaders',
'django.contrib.postgres',
'rest_framework',
'fcm_django',
# Add your custom apps here.
'django_ses',
'account',
@ -208,6 +209,18 @@ CORS_ALLOW_HEADERS = (
'x-requested-with',
)
CORS_EXPOSE_HEADERS = (
'Access-Control-Allow-Origin: *',
)
# fcm django settings
FCM_DJANGO_SETTINGS = {
"APP_VERBOSE_NAME": "ZOD_Bank",
"ONE_DEVICE_PER_USER": False,
"DELETE_INACTIVE_DEVICES": True,
"UPDATE_ON_DUPLICATE_REG_ID": True,
}
"""Static files (CSS, JavaScript, Images)
https://docs.djangoproject.com/en/3.0/howto/static-files/"""

View File

@ -30,5 +30,6 @@ urlpatterns = [
path('', include(('account.urls', 'account'), namespace='account')),
path('', include('guardian.urls')),
path('', include(('junior.urls', 'junior'), namespace='junior')),
path('', include(('notifications.urls', 'notifications'), namespace='notifications')),
path('', include(('web_admin.urls', 'web_admin'), namespace='web_admin')),
]