def token_retrieve(request): token = None if 'X_TOKEN' in request.META: token = request.META['X_TOKEN'] elif 'HTTP_X_TOKEN' in request.META: token = request.META['HTTP_X_TOKEN'] if token is not None: if len(token) == 20: token = UUID(hexlify(b85decode(token.encode())).decode()) if len(token) == 25: hasher = Hasher(salt=settings.SECRET_KEY) token = UUID(hasher.decode(token)) try: token = Token.objects.get(id=token, is_active=True, is_anonymous=False) request.user = token.owner if token.due is not None and token.due < timezone.now(): token.is_active = False token.save() token = None except Exception: token = None return token
def post(self, request): if isinstance(request.data, QueryDict): request_data = dict(request.data.iterlists()) else: request_data = request.data uuid = request_data.pop('uuid', None) site_id = request_data.pop('site', None) if uuid is None or site_id is None: return Response({'ok': False, 'Description': 'uuid and site should not be None'}) uuid = UUID(bytes=base64.urlsafe_b64decode(uuid.encode('ascii') + '=='.encode('ascii'))) data = {'site': site_id, 'uid': uuid.hex, 'data': request_data} serializer = SensorReportSerializer(data=data) if serializer.is_valid(raise_exception=True): serializer.save() return Response({'ok': True}) return Response({'ok': False})