Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select target firmware based on device state #148

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions goosebit/api/v1/devices/routes.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
from __future__ import annotations

import asyncio

from fastapi import APIRouter, Security
from fastapi.requests import Request

from goosebit.api.responses import StatusResponse
from goosebit.auth import validate_user_permissions
from goosebit.db.models import Device
from goosebit.updater.manager import delete_devices
from goosebit.schema.devices import DeviceSchema
from goosebit.schema.software import SoftwareSchema
from goosebit.updater.manager import delete_devices, get_update_manager

from . import device
from .requests import DevicesDeleteRequest
Expand All @@ -21,7 +25,18 @@
)
async def devices_get(_: Request) -> DevicesResponse:
devices = await Device.all().prefetch_related("assigned_software", "hardware")
return DevicesResponse(devices=devices)
response = DevicesResponse(devices=devices)

async def set_assigned_sw(d: DeviceSchema):
updater = await get_update_manager(d.uuid)
_, target = await updater.get_update()
if target is not None:
await target.fetch_related("compatibility")
d.assigned_software = SoftwareSchema.model_validate(target)
return d

response.devices = await asyncio.gather(*[set_assigned_sw(d) for d in response.devices])
return response


@router.delete(
Expand Down
16 changes: 15 additions & 1 deletion goosebit/ui/bff/devices/routes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import asyncio
from typing import Annotated

from fastapi import APIRouter, Depends, Security
Expand All @@ -10,6 +11,8 @@
from goosebit.api.v1.devices import routes
from goosebit.auth import validate_user_permissions
from goosebit.db.models import Device, Software, UpdateModeEnum, UpdateStateEnum
from goosebit.schema.devices import DeviceSchema
from goosebit.schema.software import SoftwareSchema
from goosebit.ui.bff.common.requests import DataTableRequest
from goosebit.ui.bff.common.util import parse_datatables_query
from goosebit.updater.manager import get_update_manager
Expand Down Expand Up @@ -37,7 +40,18 @@ def search_filter(search_value: str):

query = Device.all().prefetch_related("assigned_software", "hardware", "assigned_software__compatibility")

return await BFFDeviceResponse.convert(dt_query, query, search_filter)
response = await BFFDeviceResponse.convert(dt_query, query, search_filter)

async def set_assigned_sw(d: DeviceSchema):
updater = await get_update_manager(d.uuid)
_, target = await updater.get_update()
if target is not None:
await target.fetch_related("compatibility")
d.assigned_software = SoftwareSchema.model_validate(target)
return d

response.data = await asyncio.gather(*[set_assigned_sw(d) for d in response.data])
return response


@router.patch(
Expand Down