33from rest_framework .response import Response
44from rest_framework .serializers import ModelSerializer , Serializer , ValidationError
55from rest_framework .viewsets import ModelViewSet
6- from typing import Any
7- from push_notifications .fields import UNSIGNED_64BIT_INT_MAX_VALUE , hex_re
8- from push_notifications .models import APNSDevice , GCMDevice , WebPushDevice , WNSDevice
9- from push_notifications .settings import PUSH_NOTIFICATIONS_SETTINGS as SETTINGS
10- from django .db .models import QuerySet
6+
7+ from ..fields import UNSIGNED_64BIT_INT_MAX_VALUE , hex_re
8+ from ..models import APNSDevice , GCMDevice , WebPushDevice , WNSDevice
9+ from ..settings import PUSH_NOTIFICATIONS_SETTINGS as SETTINGS
1110
1211
1312# Fields
@@ -16,16 +15,16 @@ class HexIntegerField(IntegerField):
1615 Store an integer represented as a hex string of form "0x01".
1716 """
1817
19- def to_internal_value (self , data : str | int ) -> int :
18+ def to_internal_value (self , data ) :
2019 # validate hex string and convert it to the unsigned
2120 # integer representation for internal use
2221 try :
23- data = int (data , 16 ) if not isinstance (data , int ) else data
22+ data = int (data , 16 ) if type (data ) != int else data
2423 except ValueError :
2524 raise ValidationError ("Device ID is not a valid hex number" )
2625 return super ().to_internal_value (data )
2726
28- def to_representation (self , value : int ) -> int :
27+ def to_representation (self , value ) :
2928 return value
3029
3130
@@ -46,7 +45,7 @@ class APNSDeviceSerializer(ModelSerializer):
4645 class Meta (DeviceSerializerMixin .Meta ):
4746 model = APNSDevice
4847
49- def validate_registration_id (self , value : str ) -> str :
48+ def validate_registration_id (self , value ) :
5049
5150 # https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1622958-application
5251 # As of 02/2023 APNS tokens (registration_id) "are of variable length. Do not hard-code their size."
@@ -57,7 +56,7 @@ def validate_registration_id(self, value: str) -> str:
5756
5857
5958class UniqueRegistrationSerializerMixin (Serializer ):
60- def validate (self , attrs : dict [ str , Any ]) -> dict [ str , Any ] :
59+ def validate (self , attrs ) :
6160 devices = None
6261 primary_key = None
6362 request_method = None
@@ -104,7 +103,7 @@ class Meta(DeviceSerializerMixin.Meta):
104103 )
105104 extra_kwargs = {"id" : {"read_only" : False , "required" : False }}
106105
107- def validate_device_id (self , value : int ) -> int :
106+ def validate_device_id (self , value ) :
108107 # device ids are 64 bit unsigned values
109108 if value > UNSIGNED_64BIT_INT_MAX_VALUE :
110109 raise ValidationError ("Device ID is out of range" )
@@ -127,7 +126,7 @@ class Meta(DeviceSerializerMixin.Meta):
127126
128127# Permissions
129128class IsOwner (permissions .BasePermission ):
130- def has_object_permission (self , request : Any , view : Any , obj : Any ) -> bool :
129+ def has_object_permission (self , request , view , obj ) :
131130 # must be the owner to view the object
132131 return obj .user == request .user
133132
@@ -136,7 +135,7 @@ def has_object_permission(self, request: Any, view: Any, obj: Any) -> bool:
136135class DeviceViewSetMixin :
137136 lookup_field = "registration_id"
138137
139- def create (self , request : Any , * args : Any , ** kwargs : Any ) -> Response :
138+ def create (self , request , * args , ** kwargs ) :
140139 serializer = None
141140 is_update = False
142141 if SETTINGS .get ("UPDATE_ON_DUPLICATE_REG_ID" ) and self .lookup_field in request .data :
@@ -158,12 +157,12 @@ def create(self, request: Any, *args: Any, **kwargs: Any) -> Response:
158157 headers = self .get_success_headers (serializer .data )
159158 return Response (serializer .data , status = status .HTTP_201_CREATED , headers = headers )
160159
161- def perform_create (self , serializer : ModelSerializer ) -> None :
160+ def perform_create (self , serializer ) :
162161 if self .request .user .is_authenticated :
163162 serializer .save (user = self .request .user )
164163 return super ().perform_create (serializer )
165164
166- def perform_update (self , serializer : ModelSerializer ) -> None :
165+ def perform_update (self , serializer ) :
167166 if self .request .user .is_authenticated :
168167 serializer .save (user = self .request .user )
169168 return super ().perform_update (serializer )
@@ -172,7 +171,7 @@ def perform_update(self, serializer: ModelSerializer) -> None:
172171class AuthorizedMixin :
173172 permission_classes = (permissions .IsAuthenticated , IsOwner )
174173
175- def get_queryset (self ) -> QuerySet :
174+ def get_queryset (self ):
176175 # filter all devices to only those belonging to the current user
177176 return self .queryset .filter (user = self .request .user )
178177
0 commit comments