Skip to content

Commit

Permalink
unique together with registration_id and user_id when using fcmdevice…
Browse files Browse the repository at this point in the history
…authorizedviewset
  • Loading branch information
xtrinch committed Aug 23, 2016
1 parent 6cc5feb commit 89f9d9f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 25 deletions.
2 changes: 1 addition & 1 deletion fcm_django/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
__author__ = "xTrinch"
__email__ = "mojca.rojko@gmail.com"
__version__ = "0.1.5"
__version__ = "0.1.6"


class NotificationError(Exception):
Expand Down
40 changes: 17 additions & 23 deletions fcm_django/api/rest_framework.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
from __future__ import absolute_import

from rest_framework import permissions
from rest_framework.serializers import ModelSerializer, ValidationError
from rest_framework.serializers import ModelSerializer, ValidationError, Serializer, CurrentUserDefault
from rest_framework.viewsets import ModelViewSet
from rest_framework.fields import IntegerField

from fcm_django.models import FCMDevice
from fcm_django.fields import hex_re
from fcm_django.fields import UNSIGNED_64BIT_INT_MAX_VALUE


# Fields

Expand All @@ -22,20 +16,7 @@ class Meta:
# See https://github.com/tomchristie/django-rest-framework/issues/1101
extra_kwargs = {"active": {"default": True}}


class FCMDeviceSerializer(ModelSerializer):

class Meta(DeviceSerializerMixin.Meta):
model = FCMDevice

extra_kwargs = {"id": {"read_only": False, "required": False}}

#def validate_device_id(self, value):
# # device ids are 64 bit unsigned values
# if value > UNSIGNED_64BIT_INT_MAX_VALUE:
# raise ValidationError("Device ID is out of range")
# return value

class UniqueRegistrationSerializerMixin(Serializer):
def validate(self, attrs):
devices = None
primary_key = None
Expand All @@ -54,16 +35,29 @@ def validate(self, attrs):
elif self.context["request"].method == "POST":
request_method = "create"

Device = self.Meta.model
if request_method == "update":
devices = FCMDevice.objects.filter(registration_id=attrs["registration_id"]) \
devices = Device.objects.filter(registration_id=attrs["registration_id"]) \
.exclude(id=primary_key)
elif request_method == "create":
devices = FCMDevice.objects.filter(registration_id=attrs["registration_id"])
# if request authenticated, unique together with registration_id and user
user = self.context['request'].user
if user is not None:
devices = Device.objects.filter(registration_id=attrs["registration_id"], user=user)
else:
devices = Device.objects.filter(registration_id=attrs["registration_id"])

if devices:
raise ValidationError({'registration_id': 'This field must be unique.'})
return attrs

class FCMDeviceSerializer(ModelSerializer, UniqueRegistrationSerializerMixin):

class Meta(DeviceSerializerMixin.Meta):
model = FCMDevice

extra_kwargs = {"id": {"read_only": False, "required": False}}


# Permissions
class IsOwner(permissions.BasePermission):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from distutils.core import setup
import setuptools

VERSION = '0.1.5'
VERSION = '0.1.6'

CLASSIFIERS = [
"Development Status :: 5 - Production/Stable",
Expand Down

0 comments on commit 89f9d9f

Please sign in to comment.