-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use
model_validate
built into pydantic to parse DB models
`pydantic` models have a deprecated `from_orm` method, which is designed to parse DB models into the schema. This was superseded by `model_validate`, which does the same thing when the schema config `from_attributes` value is true. Migrating to using this simplifies the method of parsing DB models, and will eventually allow the schemas to be used internally instead of the DB models.
- Loading branch information
1 parent
18abbb3
commit a740559
Showing
17 changed files
with
139 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,9 @@ | ||
from __future__ import annotations | ||
|
||
import asyncio | ||
|
||
from pydantic import BaseModel | ||
|
||
from goosebit.db.models import Device | ||
from goosebit.schema.devices import DeviceSchema | ||
|
||
|
||
class DevicesResponse(BaseModel): | ||
devices: list[DeviceSchema] | ||
|
||
@classmethod | ||
async def convert(cls, devices: list[Device]): | ||
return cls(devices=await asyncio.gather(*[DeviceSchema.convert(d) for d in devices])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,14 @@ | ||
import asyncio | ||
from __future__ import annotations | ||
|
||
from pydantic import BaseModel | ||
|
||
from goosebit.api.responses import StatusResponse | ||
from goosebit.db.models import Rollout | ||
from goosebit.schema.rollouts import RolloutSchema | ||
|
||
|
||
class RolloutsPutResponse(StatusResponse): | ||
id: int | ||
id: int | None = None | ||
|
||
|
||
class RolloutsResponse(BaseModel): | ||
rollouts: list[RolloutSchema] | ||
|
||
@classmethod | ||
async def convert(cls, devices: list[Rollout]): | ||
return cls(rollouts=await asyncio.gather(*[RolloutSchema.convert(d) for d in devices])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,9 @@ | ||
from __future__ import annotations | ||
|
||
import asyncio | ||
|
||
from pydantic import BaseModel | ||
|
||
from goosebit.db.models import Software | ||
from goosebit.schema.software import SoftwareSchema | ||
|
||
|
||
class SoftwareResponse(BaseModel): | ||
software: list[SoftwareSchema] | ||
|
||
@classmethod | ||
async def convert(cls, software: list[Software]): | ||
return cls(software=await asyncio.gather(*[SoftwareSchema.convert(f) for f in software])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,34 @@ | ||
from __future__ import annotations | ||
|
||
from pydantic import BaseModel | ||
from datetime import datetime | ||
|
||
from goosebit.db.models import Rollout | ||
from pydantic import BaseModel, ConfigDict, Field, computed_field, field_serializer | ||
|
||
from goosebit.schema.software import SoftwareSchema | ||
|
||
|
||
class RolloutSchema(BaseModel): | ||
model_config = ConfigDict(from_attributes=True) | ||
|
||
id: int | ||
created_at: int | ||
created_at: datetime | ||
name: str | None | ||
feed: str | ||
sw_file: str | ||
sw_version: str | ||
software: SoftwareSchema = Field(exclude=True) | ||
paused: bool | ||
success_count: int | ||
failure_count: int | ||
|
||
@classmethod | ||
async def convert(cls, rollout: Rollout): | ||
return cls( | ||
id=rollout.id, | ||
created_at=int(rollout.created_at.timestamp() * 1000), | ||
name=rollout.name, | ||
feed=rollout.feed, | ||
sw_file=rollout.software.path.name, | ||
sw_version=rollout.software.version, | ||
paused=rollout.paused, | ||
success_count=rollout.success_count, | ||
failure_count=rollout.failure_count, | ||
) | ||
@computed_field # type: ignore[misc] | ||
@property | ||
def sw_version(self) -> str: | ||
return self.software.version | ||
|
||
@computed_field # type: ignore[misc] | ||
@property | ||
def sw_file(self) -> str: | ||
return self.software.path.name | ||
|
||
@field_serializer("created_at") | ||
def serialize_created_at(self, created_at: datetime, _info): | ||
return int(created_at.timestamp() * 1000) |
Oops, something went wrong.