File: view.py

package info (click to toggle)
python-django-postgres-extra 2.0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,096 kB
  • sloc: python: 9,057; makefile: 17; sh: 7; sql: 1
file content (54 lines) | stat: -rw-r--r-- 1,734 bytes parent folder | download
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