Esempio n. 1
0
    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)
Esempio n. 2
0
 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)
Esempio n. 3
0
    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)