build 4.6 Demo
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
"""Read projections used by the demo API."""
|
||||
|
||||
from .models import Device, DeviceType, File, Manufacturer
|
||||
|
||||
|
||||
def get_all_manufacturers() -> list[dict]:
|
||||
return [item.to_json() for item in Manufacturer.objects.all()]
|
||||
|
||||
|
||||
def get_all_device_types() -> list[dict]:
|
||||
return [item.to_json() for item in DeviceType.objects.all()]
|
||||
|
||||
|
||||
def get_all_devices() -> list[dict]:
|
||||
devices = Device.objects.select_related(
|
||||
"product", "product__manufacturer", "product__device_type"
|
||||
)
|
||||
return [
|
||||
{
|
||||
"id": device.id,
|
||||
"manufacturer_name": device.product.manufacturer.name,
|
||||
"device_type": device.product.device_type.device_type,
|
||||
"device_name": device.product.product_name,
|
||||
"serial": device.serial,
|
||||
"comment": device.comment,
|
||||
"created": device.created.isoformat(),
|
||||
}
|
||||
for device in devices
|
||||
]
|
||||
|
||||
|
||||
def get_device_by_id(device_id: int) -> dict:
|
||||
device = Device.objects.select_related(
|
||||
"product", "product__manufacturer", "product__device_type"
|
||||
).get(id=device_id)
|
||||
return device.to_json()
|
||||
|
||||
|
||||
def get_files_by_device(device_id: int) -> list[dict]:
|
||||
files = File.objects.filter(device_id=device_id).select_related("firmware")
|
||||
return [
|
||||
{
|
||||
"id": file.id,
|
||||
"filename": file.filename,
|
||||
"ip": file.ip_address,
|
||||
"state": file.status,
|
||||
"upload": file.upload_time.isoformat(),
|
||||
"firmware_version": file.firmware.version,
|
||||
}
|
||||
for file in files
|
||||
]
|
||||
Reference in New Issue
Block a user