"""This module declareds the pydantic schema representation for devices.""" from datetime import datetime from abc import ABC from pydantic import BaseModel, validator class DeviceModel(BaseModel): id: int name: str picture_code: str @validator('name') def assert_name_is_valid(cls, name): if not len(name): raise ValueError("Name must not be empty.") return name @validator('picture_code') def assert_picture_is_valid(cls, picture): if not len(picture): raise ValueError("Picture must not be empty.") return picture class Config: orm_mode = True class DeviceCreate(BaseModel): """Device schema used for Device creation.""" owner_id: int model_id: int @validator('model_id') def assert_model_id_is_valid(cls, model_id): if not 1 <= model_id <= 3: raise ValueError("Model id is invalid.") return model_id class DeviceUpdate(BaseModel): """Device schema used for Device updates.""" last_seen: datetime class Device(BaseModel): """Device schema used for Device display.""" id: int model: DeviceModel added: datetime last_seen: datetime | None @validator('added') def assert_added_is_valid(cls, added): if added >= datetime.now(added.tzinfo): raise ValueError("Date added cannot be in the future.") return added @validator('last_seen') def assert_last_seen_is_valid(cls, last_seen): if last_seen: if last_seen >= datetime.now(last_seen.tzinfo): raise ValueError("Date last seen cannot be in the future.") return last_seen class Config: orm_mode = True