def dispatch(self, request, *args, **kwargs): if not AuthServiceApi.verify_token(request): return self.handle_no_permission() self.user = AuthServiceApi.get_me(request.COOKIES.get('user_token')) if not self.test_func(): return self.handle_no_permission() return super().dispatch(request, *args, **kwargs)
def get(self, request, device, pk, format=None): me = AuthServiceApi.get_me(self.request.headers.get('Authorization')) if not me['is_staff']: read_permission = AuthServiceApi.has_read_permission( HUB_ID, device, me['pk']) if not read_permission['has_read_perm']: return Response(status=status.HTTP_403_FORBIDDEN) try: device_obj = Device.objects.get(pk=device) unit = DeviceUnit.objects.get(device=device_obj, pk=pk) except Device.DoesNotExist: raise Http404 except DeviceUnit.DoesNotExist: raise Http404 if unit.type_of_unit == UnitType.HUMIDITY_UNIT: objs = HumidityUnitValue.objects.filter( device_unit=unit).order_by('-timestamp')[:50] serializer = HumidityUnitValueSerializer(objs, many=True) elif unit.type_of_unit == UnitType.TEMPERATURE_UNIT: objs = TemperatureUnitValue.objects.filter( device_unit=unit).order_by('-timestamp')[:50] serializer = TemperatureUnitValueSerializer(objs, many=True) elif unit.type_of_unit == UnitType.SWITCH_UNIT: objs = SwitchUnitValue.objects.filter( device_unit=unit).order_by('-timestamp')[:50] serializer = SwitchUnitValueSerializer(objs, many=True) else: raise Exception("Bad Unit Type") return Response(serializer.data, status=status.HTTP_200_OK)
def delete(self, request, pk, format=None): me = AuthServiceApi.get_me(self.request.headers.get('Authorization')) if not me['is_staff']: return Response(status=status.HTTP_403_FORBIDDEN) connected_unit = ConnectedUnit.objects.get(pk=pk) connected_unit.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def get(self, request, from_unit, pk=None, format=None): me = AuthServiceApi.get_me(self.request.headers.get('Authorization')) if not me['is_staff']: return Response(status=status.HTTP_403_FORBIDDEN) if pk is None: return self.get_many(request, from_unit, format) else: return self.get_single(request, from_unit, pk, format)
def get(self, request, *args, **kwargs): hub = AuthServiceApi.get_hub(self.kwargs.get('hub')) self.device = InternalApi.get_device( self.request.COOKIES.get('user_token'), hub, self.kwargs.get('pk')) self.units = InternalApi.get_units( self.request.COOKIES.get('user_token'), hub, self.kwargs.get('pk')) self.user = AuthServiceApi.get_me( self.request.COOKIES.get('user_token')) return super(DeviceUnitsView, self).get(request, *args, **kwargs)
def get_all(self, request, format=None): me = AuthServiceApi.get_me(request.headers.get('Authorization')) if me['is_staff']: devices = Device.objects.all() else: devices_pks = AuthServiceApi.get_registered_devices_with_read_perm( HUB_ID, me['pk'])['read_permission_devices'] devices = Device.objects.filter(pk__in=devices_pks) serializer = DeviceSerializer(devices, many=True) return Response(serializer.data)
def post(self, request, format=None): me = AuthServiceApi.get_me(self.request.headers.get('Authorization')) if not me['is_staff']: return Response(status=status.HTTP_403_FORBIDDEN) serializer = DeviceSerializer(data=request.data) if serializer.is_valid(): device = serializer.save() AuthServiceApi.register_device(HUB_ID, device) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def delete(self, request, pk, format=None): me = AuthServiceApi.get_me(self.request.headers.get('Authorization')) if not me['is_staff']: return Response(status=status.HTTP_403_FORBIDDEN) try: device = Device.objects.get(pk=pk) except Device.DoesNotExist: raise Http404 AuthServiceApi.unregister_device(HUB_ID, device) device.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def post(self, request, device, format=None): me = AuthServiceApi.get_me(self.request.headers.get('Authorization')) if not me['is_staff']: return Response(status=status.HTTP_403_FORBIDDEN) serializer = DeviceUnitSerializer(data=request.data) if serializer.is_valid(): device = Device.objects.get(pk=device) DeviceUnit.objects.create(device=device, **serializer.validated_data) return Response(serializer.validated_data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get_one(self, request, pk, format=None): me = AuthServiceApi.get_me(self.request.headers.get('Authorization')) if not me['is_staff']: read_permission = AuthServiceApi.has_read_permission( HUB_ID, pk, me['pk']) if not read_permission['has_read_perm']: raise Http404 try: devices = Device.objects.get(pk=pk) except Device.DoesNotExist: raise Http404 serializer = DeviceSerializer(devices) return Response(serializer.data)
def put(self, request, pk, format=None): me = AuthServiceApi.get_me(self.request.headers.get('Authorization')) if not me['is_staff']: return Response(status=status.HTTP_403_FORBIDDEN) try: device = Device.objects.get(pk=pk) except Device.DoesNotExist: raise Http404 serializer = DeviceSerializer(device, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, device, pk, format=None): me = AuthServiceApi.get_me(self.request.headers.get('Authorization')) if not me['is_staff']: return Response(status=status.HTTP_403_FORBIDDEN) serializer = DeviceUnitSerializer(data=request.data) if serializer.is_valid(): unit = DeviceUnit.objects.get(pk=pk) unit.name = serializer.validated_data['name'] unit.direction = serializer.validated_data['direction'] unit.type_of_unit = serializer.validated_data['type_of_unit'] unit.save() return Response(serializer.validated_data, status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get(self, request, device, pk=None, format=None): me = AuthServiceApi.get_me(self.request.headers.get('Authorization')) device_obj = Device.objects.get(pk=device) if pk is None: if me['is_staff']: objects = DeviceUnit.objects.filter(device=device_obj) else: read_permission = AuthServiceApi.has_read_permission( HUB_ID, device, me['pk']) if not read_permission['has_read_perm']: return Response(status=status.HTTP_403_FORBIDDEN) objects = DeviceUnit.objects.filter(device=device_obj) else: read_permission = AuthServiceApi.has_read_permission( HUB_ID, device, me['pk']) if me['is_staff']: objects = DeviceUnit.objects.get(device=device_obj, pk=pk) else: if not read_permission['has_read_perm']: return Response(status=status.HTTP_403_FORBIDDEN) objects = DeviceUnit.objects.get(device=device_obj, pk=pk) serializer = DeviceUnitSerializer(objects, many=pk is None) return Response(serializer.data)