Skip to content

Fields.sqlalchemy

respo.fields.sqlalchemy

MutableRespoClient

Bases: Mutable, client.RespoClient

SQLAlchemy field that represent RespoClient instance, based on Mutable.

Wrapper around RespoClient instance that triggers changed() on add_role and remove_role. Overwrittes ORM that use fancy mechanisms that won't detect mutable objects changes (and won't be commited to database).

https://docs.sqlalchemy.org/en/14/orm/extensions/mutable.html

Source code in respo/fields/sqlalchemy.py
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
55
56
57
58
59
60
61
62
63
64
class MutableRespoClient(Mutable, client.RespoClient):
    """SQLAlchemy field that represent RespoClient instance, based on Mutable.

    Wrapper around RespoClient instance that triggers changed() on
    add_role and remove_role. Overwrittes ORM that use fancy mechanisms
    that won't detect mutable objects changes (and won't be commited to database).

    https://docs.sqlalchemy.org/en/14/orm/extensions/mutable.html
    """

    @classmethod
    def coerce(cls, key, value):
        """Transforms Python object to MutableRespoClient Field."""

        if isinstance(value, cls):
            return value
        elif isinstance(value, client.RespoClient):
            return cls(str(value))
        raise ValueError("Field must be instance of RespoClient or MutableRespoClient.")

    def add_role(
        self,
        role_name: str,
        respo_model: Optional[core.RespoModel] = None,
        validate_input: bool = settings.config.RESPO_CHECK_FORCE,
    ) -> bool:
        res = super().add_role(role_name, respo_model, validate_input)
        self.changed()
        return res

    def remove_role(
        self,
        role_name: str,
        respo_model: Optional[core.RespoModel] = None,
        validate_input: bool = settings.config.RESPO_CHECK_FORCE,
    ) -> bool:
        res = super().remove_role(role_name, respo_model, validate_input)
        self.changed()
        return res

coerce(key, value) classmethod

Transforms Python object to MutableRespoClient Field.

Source code in respo/fields/sqlalchemy.py
36
37
38
39
40
41
42
43
44
@classmethod
def coerce(cls, key, value):
    """Transforms Python object to MutableRespoClient Field."""

    if isinstance(value, cls):
        return value
    elif isinstance(value, client.RespoClient):
        return cls(str(value))
    raise ValueError("Field must be instance of RespoClient or MutableRespoClient.")

TEXTRespoField

Bases: TypeDecorator

Platform-independent Custom Type to store Respo model based on TEXT type

Source code in respo/fields/sqlalchemy.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class TEXTRespoField(TypeDecorator):
    """Platform-independent Custom Type to store Respo model based on TEXT type"""

    impl = TEXT
    cache_ok = True

    def process_bind_param(self, value: "MutableRespoClient", dialect) -> str:
        return str(value)

    def process_result_value(
        self, value: Optional[str], dialect
    ) -> "MutableRespoClient":
        if value is None:  # pragma: no cover
            value = ""
        return MutableRespoClient(roles=value)