|
| 1 | +from __future__ import absolute_import |
| 2 | + |
| 3 | +from rest_framework import permissions |
| 4 | +from rest_framework.serializers import ModelSerializer, ValidationError |
| 5 | +from rest_framework.viewsets import ModelViewSet |
| 6 | +from rest_framework.fields import IntegerField |
| 7 | + |
| 8 | +from push_notifications.models import APNSDevice, GCMDevice |
| 9 | +from push_notifications.fields import hex_re |
| 10 | + |
| 11 | + |
| 12 | +# Fields |
| 13 | + |
| 14 | +class HexIntegerField(IntegerField): |
| 15 | + """ |
| 16 | + Store an integer represented as a hex string of form "0x01". |
| 17 | + """ |
| 18 | + |
| 19 | + def to_internal_value(self, data): |
| 20 | + data = int(data, 16) |
| 21 | + return super(HexIntegerField, self).to_internal_value(data) |
| 22 | + |
| 23 | + def to_representation(self, value): |
| 24 | + return value |
| 25 | + |
| 26 | + |
| 27 | +# Serializers |
| 28 | +class DeviceSerializerMixin(ModelSerializer): |
| 29 | + class Meta: |
| 30 | + fields = ("name", "registration_id", "device_id", "active", "date_created") |
| 31 | + read_only_fields = ("date_created", ) |
| 32 | + |
| 33 | + |
| 34 | +class APNSDeviceSerializer(ModelSerializer): |
| 35 | + |
| 36 | + class Meta(DeviceSerializerMixin.Meta): |
| 37 | + model = APNSDevice |
| 38 | + |
| 39 | + def validate_registration_id(self, value): |
| 40 | + # iOS device tokens are 256-bit hexadecimal (64 characters) |
| 41 | + |
| 42 | + if hex_re.match(value) is None or len(value) != 64: |
| 43 | + raise ValidationError("Registration ID (device token) is invalid") |
| 44 | + |
| 45 | + return value |
| 46 | + |
| 47 | + |
| 48 | +class GCMDeviceSerializer(ModelSerializer): |
| 49 | + device_id = HexIntegerField( |
| 50 | + help_text="ANDROID_ID / TelephonyManager.getDeviceId() (e.g: 0x01)", |
| 51 | + style={'input_type': 'text'}, |
| 52 | + required=False |
| 53 | + ) |
| 54 | + |
| 55 | + class Meta(DeviceSerializerMixin.Meta): |
| 56 | + model = GCMDevice |
| 57 | + |
| 58 | + |
| 59 | +# Permissions |
| 60 | +class IsOwner(permissions.BasePermission): |
| 61 | + def has_object_permission(self, request, view, obj): |
| 62 | + # must be the owner to view the object |
| 63 | + return obj.user == request.user |
| 64 | + |
| 65 | + |
| 66 | +# Mixins |
| 67 | +class DeviceViewSetMixin(object): |
| 68 | + lookup_field = "registration_id" |
| 69 | + |
| 70 | + def perform_create(self, serializer): |
| 71 | + if self.request.user.is_authenticated(): |
| 72 | + serializer.save(user=self.request.user) |
| 73 | + |
| 74 | + |
| 75 | +class AuthorizedMixin(object): |
| 76 | + permission_classes = (permissions.IsAuthenticated, IsOwner) |
| 77 | + |
| 78 | + def get_queryset(self): |
| 79 | + # filter all devices to only those belonging to the current user |
| 80 | + return self.queryset.filter(user=self.request.user) |
| 81 | + |
| 82 | + |
| 83 | +# ViewSets |
| 84 | +class APNSDeviceViewSet(DeviceViewSetMixin, ModelViewSet): |
| 85 | + queryset = APNSDevice.objects.all() |
| 86 | + serializer_class = APNSDeviceSerializer |
| 87 | + |
| 88 | + |
| 89 | +class APNSDeviceAuthorizedViewSet(AuthorizedMixin, APNSDeviceViewSet): |
| 90 | + pass |
| 91 | + |
| 92 | + |
| 93 | +class GCMDeviceViewSet(DeviceViewSetMixin, ModelViewSet): |
| 94 | + queryset = GCMDevice.objects.all() |
| 95 | + serializer_class = GCMDeviceSerializer |
| 96 | + |
| 97 | + |
| 98 | +class GCMDeviceAuthorizedViewSet(AuthorizedMixin, GCMDeviceViewSet): |
| 99 | + pass |
0 commit comments