-
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.
Merge pull request #129 from UpstreamDataInc/dev_from_orm
Use `model_validate` built into pydantic to parse DB models
- Loading branch information
Showing
16 changed files
with
108 additions
and
111 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
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) |
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,37 +1,42 @@ | ||
from __future__ import annotations | ||
|
||
import asyncio | ||
from urllib.parse import unquote, urlparse | ||
from urllib.request import url2pathname | ||
|
||
from pydantic import BaseModel | ||
|
||
from goosebit.db.models import Hardware, Software | ||
from anyio import Path | ||
from pydantic import BaseModel, ConfigDict, Field, computed_field | ||
|
||
|
||
class HardwareSchema(BaseModel): | ||
model_config = ConfigDict(from_attributes=True) | ||
|
||
id: int | ||
model: str | ||
revision: str | ||
|
||
@classmethod | ||
async def convert(cls, hardware: Hardware): | ||
return cls(id=hardware.id, model=hardware.model, revision=hardware.revision) | ||
|
||
|
||
class SoftwareSchema(BaseModel): | ||
model_config = ConfigDict(from_attributes=True) | ||
|
||
id: int | ||
name: str | ||
uri: str = Field(exclude=True) | ||
size: int | ||
hash: str | ||
version: str | ||
compatibility: list[HardwareSchema] | ||
|
||
@classmethod | ||
async def convert(cls, software: Software): | ||
return cls( | ||
id=software.id, | ||
name=software.path_user, | ||
size=software.size, | ||
hash=software.hash, | ||
version=software.version, | ||
compatibility=await asyncio.gather(*[HardwareSchema.convert(h) for h in software.compatibility]), | ||
) | ||
@property | ||
def path(self) -> Path: | ||
return Path(url2pathname(unquote(urlparse(self.uri).path))) | ||
|
||
@property | ||
def local(self) -> bool: | ||
return urlparse(self.uri).scheme == "file" | ||
|
||
@computed_field # type: ignore[misc] | ||
@property | ||
def name(self) -> str: | ||
if self.local: | ||
return self.path.name | ||
else: | ||
return self.uri |
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
Oops, something went wrong.