2023-05-12 22:03:44 +01:00
|
|
|
"""This module declareds the pydantic schema representation for devices."""
|
|
|
|
|
|
|
|
from datetime import datetime
|
2023-05-13 01:54:04 +01:00
|
|
|
from abc import ABC
|
2023-05-12 22:03:44 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2023-05-13 01:54:04 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2023-05-12 22:03:44 +01:00
|
|
|
class Device(BaseModel):
|
2023-05-13 01:54:04 +01:00
|
|
|
"""Device schema used for Device display."""
|
|
|
|
|
2023-05-12 22:03:44 +01:00
|
|
|
id: int
|
|
|
|
model: DeviceModel
|
2023-05-13 01:54:04 +01:00
|
|
|
added: datetime
|
|
|
|
last_seen: datetime | None
|
2023-05-12 22:03:44 +01:00
|
|
|
|
|
|
|
@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):
|
2023-05-13 01:54:04 +01:00
|
|
|
if last_seen:
|
|
|
|
if last_seen >= datetime.now(last_seen.tzinfo):
|
|
|
|
raise ValueError("Date last seen cannot be in the future.")
|
2023-05-12 22:03:44 +01:00
|
|
|
return last_seen
|
|
|
|
|
|
|
|
class Config:
|
|
|
|
orm_mode = True
|