Source code for apps.api.model_helper

from apps.core.models.device import Device
from apps.core.models.device_type import DeviceType
from apps.core.models.area import Area
from apps.core.models.reservation import Reservation
from apps.core.models.sensor import Sensor
from apps.core.models.physical_unit import PhysicalUnit
from apps.api.exceptions import ApiException
from rest_framework import status


[docs]def get_all_device_types(): device_types = DeviceType.objects.filter(deleted_at__isnull=True) return device_types
[docs]def get_device_type(device_type_id): try: device_type = DeviceType.objects.get(pk=device_type_id, deleted_at__isnull=True) except DeviceType.DoesNotExist: raise ApiException( "Device type does not exists.", status.HTTP_404_NOT_FOUND ) return device_type
[docs]def check_device_existence(device_type_id): devices = Device.objects.filter(type_id=device_type_id, deleted_at__isnull=True) if devices: raise ApiException("Can not delete device_type with existing devices", status.HTTP_403_FORBIDDEN) return True
[docs]def get_deleted_device_type_by_name(device_type_name): try: device_type = DeviceType.objects.get(type_name=device_type_name, deleted_at__isnull=False) except DeviceType.DoesNotExist: return False device_type.deleted_at = None return device_type
[docs]def get_all_devices(): devices = Device.objects.filter(deleted_at__isnull=True) return devices
[docs]def get_device(device_id): try: device = Device.objects.get(pk=device_id, deleted_at__isnull=True) except Device.DoesNotExist: raise ApiException("Device does not exists.", status.HTTP_404_NOT_FOUND) return device
[docs]def get_area(area_id): try: area = Area.objects.get(pk=area_id, deleted_at__isnull=True) except Area.DoesNotExist: raise ApiException("Area does not exists.", status.HTTP_404_NOT_FOUND) return area
[docs]def get_deleted_area_by_name(area_name): try: area = Area.objects.get(office_name=area_name, deleted_at__isnull=False) except Area.DoesNotExist: return False area.deleted_at = None return area
[docs]def get_reservation(reservation_id): try: reservation = Reservation.objects.get(pk=reservation_id, deleted_at__isnull=True) except Reservation.DoesNotExist: raise ApiException( "Reservation does not exists.", status.HTTP_404_NOT_FOUND ) return reservation
[docs]def get_sensor(sensor_id): try: sensor = Sensor.objects.get(pk=sensor_id, deleted_at__isnull=True) except Sensor.DoesNotExist: raise ApiException( "Reservation does not exists.", status.HTTP_404_NOT_FOUND ) return sensor
[docs]def get_devices_by_area(area_id): devices = Device.objects.filter(area=area_id, deleted_at__isnull=True) return devices
[docs]def get_all_sensors(): sensors = Sensor.objects.filter(deleted_at__isnull=True) return sensors
[docs]def get_all_physical_units(): units = PhysicalUnit.objects.all() return units