Skip to content

Constructable

ConstructableFromORM(id, created_at, updated_at, *, _orm_check=False)

Bases: ABC

Abstract base class for models that can be constructed from ORM models. It serves as a base class for any class that relates directly to an ORM model.

Attributes:

Name Type Description
id int

The unique identifier for the model.

created_at datetime

The timestamp when the object was created.

updated_at datetime

The timestamp when the object was last updated.

Source code in svs_core/db/constructable.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def __init__(
    self,
    id: int,
    created_at: datetime,
    updated_at: datetime,
    *,
    _orm_check: bool = False,
) -> None:
    if not _orm_check:
        raise ValueError("This class can only be instantiated from ORM models.")
    self.id = id
    self.created_at = created_at
    self.updated_at = updated_at
    super().__init__()

from_orm(model, **kwargs) abstractmethod staticmethod

Create an instance of the class from an ORM model. Args: model (object): The ORM model instance to construct from. **kwargs: Additional keyword arguments for customization. Returns: ConstructableFromORM: An instance of the class constructed from the ORM model.

Source code in svs_core/db/constructable.py
35
36
37
38
39
40
41
42
43
44
45
46
47
@staticmethod
@abstractmethod
def from_orm(model: object, **kwargs: object) -> "ConstructableFromORM":
    """
    Create an instance of the class from an ORM model.
    Args:
        model (object): The ORM model instance to construct from.
        **kwargs: Additional keyword arguments for customization.
    Returns:
        ConstructableFromORM: An instance of the class constructed from the ORM model.
    """

    pass

from_orm_generic(model, **kwargs) classmethod

Create an instance of the class from an ORM model using a generic type. Args: cls (Type[T]): The class type to construct. model (object): The ORM model instance to construct from. **kwargs: Additional keyword arguments for customization. Returns: T: An instance of the class constructed from the ORM model.

Source code in svs_core/db/constructable.py
49
50
51
52
53
54
55
56
57
58
59
60
61
@classmethod
def from_orm_generic(cls: Type[T], model: object, **kwargs: object) -> T:
    """
    Create an instance of the class from an ORM model using a generic type.
    Args:
        cls (Type[T]): The class type to construct.
        model (object): The ORM model instance to construct from.
        **kwargs: Additional keyword arguments for customization.
    Returns:
        T: An instance of the class constructed from the ORM model.
    """

    return cast(T, cls.from_orm(model, **kwargs))