diff --git a/base/messages.py b/base/messages.py index a13c1d6..950ffe1 100644 --- a/base/messages.py +++ b/base/messages.py @@ -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 diff --git a/notifications/__init__.py b/notifications/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/notifications/admin.py b/notifications/admin.py new file mode 100644 index 0000000..7bd27a7 --- /dev/null +++ b/notifications/admin.py @@ -0,0 +1,6 @@ +""" +notification admin file +""" +from django.contrib import admin + +# Register your models here. diff --git a/notifications/apps.py b/notifications/apps.py new file mode 100644 index 0000000..366a893 --- /dev/null +++ b/notifications/apps.py @@ -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' diff --git a/notifications/migrations/__init__.py b/notifications/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/notifications/models.py b/notifications/models.py new file mode 100644 index 0000000..90a6f02 --- /dev/null +++ b/notifications/models.py @@ -0,0 +1,6 @@ +""" +notification models file +""" +from django.db import models + +# Create your models here. diff --git a/notifications/serializers.py b/notifications/serializers.py new file mode 100644 index 0000000..053096f --- /dev/null +++ b/notifications/serializers.py @@ -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) diff --git a/notifications/tests.py b/notifications/tests.py new file mode 100644 index 0000000..fd087b9 --- /dev/null +++ b/notifications/tests.py @@ -0,0 +1,6 @@ +""" +notification test file +""" +from django.test import TestCase + +# Create your tests here. diff --git a/notifications/urls.py b/notifications/urls.py new file mode 100644 index 0000000..b184d02 --- /dev/null +++ b/notifications/urls.py @@ -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)), +] diff --git a/notifications/utils.py b/notifications/utils.py new file mode 100644 index 0000000..b3a0ede --- /dev/null +++ b/notifications/utils.py @@ -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 + diff --git a/notifications/views.py b/notifications/views.py new file mode 100644 index 0000000..8ac3786 --- /dev/null +++ b/notifications/views.py @@ -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"]) diff --git a/requirements.txt b/requirements.txt index 5dd7f76..6a141cb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 \ No newline at end of file diff --git a/web_admin/urls.py b/web_admin/urls.py index 95bc7d9..fa272f4 100644 --- a/web_admin/urls.py +++ b/web_admin/urls.py @@ -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)), diff --git a/zod_bank/settings.py b/zod_bank/settings.py index b107ffd..aea5896 100644 --- a/zod_bank/settings.py +++ b/zod_bank/settings.py @@ -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/""" diff --git a/zod_bank/urls.py b/zod_bank/urls.py index 009f1b3..a34ef93 100644 --- a/zod_bank/urls.py +++ b/zod_bank/urls.py @@ -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')), ]