Coverage for src / mesh / views / viewmodel / model_proxy.py: 100%
13 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-05-04 12:41 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-05-04 12:41 +0000
1from abc import ABC
2from typing import TYPE_CHECKING, Any, Generic, TypeVar
4from django.db.models import Model
6if TYPE_CHECKING:
7 from mesh.models.roles.base_role import Role
9T = TypeVar("T", bound=Model)
12class RoleModelProxy(ABC, Generic[T]):
13 """
14 Defines a proxy for a model class when some properties require to be adapted according to the user role rights.
15 To be used only for template display.
17 The logic is to overload a model's attribute in the associated interface.
18 If the attribute is not found in the interface, it falls back to the instance value.
20 Additionally, we also provide a shortcut to calling rights function with
21 the current instance as an argument.
22 """
24 model: type[T]
25 _instance: T
27 def __init__(self, instance: T, role: "Role") -> None:
28 self._instance = instance
29 self.role = role
31 def __getattr__(self, name: str) -> Any:
32 """
33 Fetches the attribute from the underlying instance.
34 This gets called only if the attribute is not found normally.
35 https://docs.python.org/3/reference/datamodel.html#object.__getattr__
37 If it doesn't exist, we authorize shortcuts to right function check.
38 """
40 attr = getattr(self._instance, name)
41 return attr