2022-03-28 20:58:40 +00:00
|
|
|
"""
|
2022-03-28 21:54:39 +00:00
|
|
|
Python representation of `device` table.
|
2022-03-28 20:58:40 +00:00
|
|
|
"""
|
|
|
|
|
2022-03-28 00:43:28 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from datetime import datetime
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
|
|
|
from sqlalchemy.exc import IntegrityError
|
|
|
|
from sqlmodel import Field, Relationship, SQLModel, UniqueConstraint
|
|
|
|
|
|
|
|
from .connection import Connection
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from .user import User
|
|
|
|
|
|
|
|
|
|
|
|
class DeviceBase(SQLModel):
|
2022-03-28 20:58:40 +00:00
|
|
|
"""
|
|
|
|
Common to all representations of devices
|
|
|
|
"""
|
|
|
|
|
2022-03-28 00:43:28 +00:00
|
|
|
name: str
|
|
|
|
type: str
|
|
|
|
expiry: datetime | None
|
|
|
|
|
|
|
|
|
|
|
|
class DeviceCreate(DeviceBase):
|
2022-03-28 20:58:40 +00:00
|
|
|
"""
|
|
|
|
Representation of a newly created device
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class DeviceRead(DeviceBase):
|
|
|
|
"""
|
|
|
|
Representation of a device read via the API
|
|
|
|
"""
|
|
|
|
|
2022-03-28 00:43:28 +00:00
|
|
|
owner_name: str | None
|
|
|
|
|
|
|
|
|
2022-03-28 00:55:18 +00:00
|
|
|
class Device(DeviceBase, table=True):
|
2022-03-28 20:58:40 +00:00
|
|
|
"""
|
2022-03-28 21:54:39 +00:00
|
|
|
Representation of `device` table
|
2022-03-28 20:58:40 +00:00
|
|
|
"""
|
|
|
|
|
2022-03-28 00:43:28 +00:00
|
|
|
__table_args__ = (UniqueConstraint(
|
|
|
|
"owner_name",
|
|
|
|
"name",
|
|
|
|
),)
|
|
|
|
|
|
|
|
id: int | None = Field(primary_key=True)
|
2022-03-28 21:54:39 +00:00
|
|
|
owner_name: str | None = Field(foreign_key="user.name")
|
2022-03-28 00:43:28 +00:00
|
|
|
|
2022-03-28 00:59:53 +00:00
|
|
|
# no idea, but "User" (in quotes) doesn't work here
|
|
|
|
# might be a future problem?
|
2022-03-28 00:43:28 +00:00
|
|
|
owner: User = Relationship(
|
|
|
|
back_populates="devices",
|
|
|
|
)
|
|
|
|
|
|
|
|
@classmethod
|
2022-03-29 00:01:12 +00:00
|
|
|
def create(
|
|
|
|
cls,
|
|
|
|
*,
|
|
|
|
owner: User,
|
|
|
|
device: DeviceCreate,
|
|
|
|
) -> Device | None:
|
2022-03-28 00:43:28 +00:00
|
|
|
"""
|
|
|
|
Create a new device in the database.
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
with Connection.session as db:
|
2022-03-29 00:01:12 +00:00
|
|
|
new_device = cls.from_orm(device)
|
|
|
|
new_device.owner = owner
|
2022-03-28 00:43:28 +00:00
|
|
|
|
2022-03-29 00:01:12 +00:00
|
|
|
db.add(new_device)
|
2022-03-28 00:43:28 +00:00
|
|
|
db.commit()
|
2022-03-29 00:01:12 +00:00
|
|
|
db.refresh(new_device)
|
2022-03-28 00:43:28 +00:00
|
|
|
|
2022-03-29 00:01:12 +00:00
|
|
|
return new_device
|
2022-03-28 00:43:28 +00:00
|
|
|
|
|
|
|
except IntegrityError:
|
|
|
|
# device already existed
|
|
|
|
return None
|
|
|
|
|
2022-03-29 00:01:12 +00:00
|
|
|
@classmethod
|
|
|
|
def create_kwargs(
|
|
|
|
cls,
|
|
|
|
*,
|
|
|
|
owner: User,
|
|
|
|
**kwargs
|
|
|
|
) -> Device | None:
|
|
|
|
"""
|
|
|
|
Create a new device in the database. Keywords version.
|
|
|
|
"""
|
|
|
|
|
|
|
|
return cls.create(
|
|
|
|
owner=owner,
|
|
|
|
device=DeviceCreate(**kwargs),
|
|
|
|
)
|
|
|
|
|
2022-03-28 00:43:28 +00:00
|
|
|
def update(self) -> None:
|
|
|
|
"""
|
|
|
|
Update this device in the database.
|
|
|
|
"""
|
|
|
|
|
|
|
|
with Connection.session as db:
|
|
|
|
db.add(self)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(self)
|
|
|
|
|
|
|
|
def delete(self) -> bool:
|
|
|
|
"""
|
|
|
|
Delete this device from the database.
|
|
|
|
"""
|
|
|
|
|
|
|
|
with Connection.session as db:
|
|
|
|
db.delete(self)
|
|
|
|
db.commit()
|