Skip to content

pixano.app.models.sources

SourceModel(created_at=None, updated_at=None, **data)

Bases: BaseSchemaModel[Source]

Model for the Source schema.

Source code in pixano/app/models/base_schema.py
def __init__(self, /, created_at: datetime | None = None, updated_at: datetime | None = None, **data: Any):
    """Create a new model by parsing and validating input data from keyword arguments.

    Raises [`ValidationError`][pydantic_core.ValidationError] if the input data cannot be
    validated to form a valid model.

    `self` is explicitly positional-only to allow `self` as a field name.

    Args:
        created_at: The creation date of the object.
        updated_at: The last modification date of the object.
        data: The data of the object validated by Pydantic.
    """
    created_at, updated_at = validate_and_init_create_at_and_update_at(created_at, updated_at)
    data.update({"created_at": created_at, "updated_at": updated_at})
    super().__init__(**data)

from_row(row, table_info) classmethod

Create a SourceModel from a Source.

Parameters:

Name Type Description Default
row Source

The row to create the model from.

required
table_info TableInfo

The table info of the row.

required

Returns:

Type Description
Self

The created model.

Source code in pixano/app/models/sources.py
@classmethod
def from_row(cls, row: Source, table_info: TableInfo) -> Self:
    """Create a SourceModel from a Source.

    Args:
        row: The row to create the model from.
        table_info: The table info of the row.

    Returns:
        The created model.
    """
    source_model = BaseSchemaModel.from_row(row, table_info)
    source_model.data["metadata"] = json.loads(source_model.data["metadata"])
    return cls.model_construct(**source_model.__dict__)  # Avoid validation and casting

to_row(dataset)

Create a Source from the model.

Source code in pixano/app/models/sources.py
def to_row(self, dataset: Dataset) -> Source:
    """Create a [Source][pixano.features.Source] from the model."""
    schema_dict = self.model_dump()
    row = Source.model_validate(
        {
            "id": schema_dict["id"],
            "created_at": schema_dict["created_at"],
            "updated_at": schema_dict["updated_at"],
            **schema_dict["data"],
        }
    )
    row.dataset = dataset
    row.table_name = self.table_info.name
    row.metadata = json.dumps(self.data["metadata"])
    return row