Skip to content

Commit

Permalink
Merge pull request #159 from Andrew-Chen-Wang/update-duplicate-reg
Browse files Browse the repository at this point in the history
Add UPDATE_ON_DUPLICATE_REG_ID
  • Loading branch information
xtrinch authored Jul 7, 2021
2 parents cab776d + 9545353 commit b28a8d0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
16 changes: 16 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ Edit your settings.py file:
# are deleted upon receiving error response from FCM
# default: False
"DELETE_INACTIVE_DEVICES": True/False,
"UPDATE_ON_DUPLICATE_REG_ID": True/False,
# Transform create of an existing Device (based on registration id) into
# a update. See below section Update of device with duplicate registration ID
# for more details.
}
Native Django migrations are in use. ``manage.py migrate`` will install and migrate all models.
Expand Down Expand Up @@ -202,6 +206,18 @@ http://www.django-rest-framework.org/tutorial/6-viewsets-and-routers#binding-vie
# ...
)
Update of device with duplicate registration ID
-----------------------------------------------

The DRF viewset enforce the uniqueness of the registration ID. In same use case it
may cause an issue: If an already registered mobile device changes its user, then
it will fail to register because the registration ID already exist.

When option ``UPDATE_ON_DUPLICATE_REG_ID`` is set to True, then any creation of
device with an already existing registration ID will be transformed into an update.

The ``UPDATE_ON_DUPLICATE_REG_ID`` only works with DRF.

Python 3 support
----------------
``fcm-django`` is fully compatible with Python 2.7 & 3.4 & 3.5 & 3.6 & 3.7
Expand Down
26 changes: 25 additions & 1 deletion fcm_django/api/rest_framework.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from __future__ import absolute_import
from rest_framework import permissions

from rest_framework import permissions, status
from rest_framework.serializers import ModelSerializer, ValidationError, \
Serializer, CurrentUserDefault
from rest_framework.mixins import CreateModelMixin
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet, GenericViewSet
from fcm_django.models import FCMDevice
from django import VERSION as DJ_VERSION
Expand Down Expand Up @@ -106,6 +108,28 @@ def has_object_permission(self, request, view, obj):
class DeviceViewSetMixin(object):
lookup_field = "registration_id"

def create(self, request, *args, **kwargs):
serializer = None
is_update = False
if SETTINGS.get('UPDATE_ON_DUPLICATE_REG_ID') and 'registration_id' in request.data:
instance = self.queryset.model.objects.filter(
registration_id=request.data['registration_id']
).first()
if instance:
serializer = self.get_serializer(instance, data=request.data)
is_update = True
if not serializer:
serializer = self.get_serializer(data=request.data)

serializer.is_valid(raise_exception=True)
if is_update:
self.perform_update(serializer)
return Response(serializer.data)
else:
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)

def perform_create(self, serializer):
if is_user_authenticated(self.request.user):
if (SETTINGS["ONE_DEVICE_PER_USER"] and
Expand Down
1 change: 1 addition & 0 deletions fcm_django/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
FCM_DJANGO_SETTINGS.setdefault("FCM_SERVER_KEY", None)
FCM_DJANGO_SETTINGS.setdefault("ONE_DEVICE_PER_USER", False)
FCM_DJANGO_SETTINGS.setdefault("DELETE_INACTIVE_DEVICES", False)
FCM_DJANGO_SETTINGS.setdefault("UPDATE_ON_DUPLICATE_REG_ID", False)

# User model
FCM_DJANGO_SETTINGS.setdefault("USER_MODEL", settings.AUTH_USER_MODEL)
Expand Down

0 comments on commit b28a8d0

Please sign in to comment.