1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
from typing import Type
from psqlextra.models import PostgresViewModel
from .model import PostgresModelState
class PostgresViewModelState(PostgresModelState):
"""Represents the state of a :see:PostgresViewModel in the migrations."""
def __init__(self, *args, view_options={}, **kwargs):
"""Initializes a new instance of :see:PostgresViewModelState.
Arguments:
view_options:
Dictionary of options for views.
See: PostgresViewModelMeta for a list.
"""
super().__init__(*args, **kwargs)
self.view_options = dict(view_options)
@classmethod
def _pre_new( # type: ignore[override]
cls,
model: Type[PostgresViewModel],
model_state: "PostgresViewModelState",
) -> "PostgresViewModelState":
"""Called when a new model state is created from the specified
model."""
model_state.view_options = dict(model._view_meta.original_attrs)
return model_state
def _pre_clone( # type: ignore[override]
self, model_state: "PostgresViewModelState"
) -> "PostgresViewModelState":
"""Called when this model state is cloned."""
model_state.view_options = dict(self.view_options)
return model_state
def _pre_render(self, name: str, bases, attributes):
"""Called when this model state is rendered into a model."""
view_meta = type("ViewMeta", (), dict(self.view_options))
return name, bases, {**attributes, "ViewMeta": view_meta}
@classmethod
def _get_base_model_class(self) -> Type[PostgresViewModel]:
"""Gets the class to use as a base class for rendered models."""
return PostgresViewModel
|