Coverage for src/mesh/views/model_proxy/model_proxy.py: 78%
16 statements
« prev ^ index » next coverage.py v7.7.0, created at 2025-04-28 07:45 +0000
« prev ^ index » next coverage.py v7.7.0, created at 2025-04-28 07:45 +0000
1from __future__ import annotations
3from abc import ABC
4from typing import TYPE_CHECKING, Any
6from django.db.models import Model
8# Require for type hint with __future__.annotations
9if TYPE_CHECKING: 9 ↛ 10line 9 didn't jump to line 10 because the condition on line 9 was never true
10 from mesh.model.roles.role_handler import RoleHandler
13class RoleModelProxy(ABC):
14 """
15 Defines a proxy for a model class when some properties require to be adapted according to the user role rights.
16 To be used only for template display.
18 The logic is to overload a model's attribute in the associated interface.
19 If the attribute is not found in the interface, it falls back to the instance value.
21 Additionally, we also provide a shortcut to calling rights function with
22 the current instance as an argument.
23 """
25 model: type[Model]
26 _instance: Model
27 _role_handler: RoleHandler
29 def __init__(self, instance: Model, role_handler: RoleHandler) -> None:
30 self._instance = instance
31 self._role_handler = role_handler
33 def __getattr__(self, name: str) -> Any:
34 """
35 Fetches the attribute from the underlying instance.
36 This gets called only if the attribute is not found normally.
37 https://docs.python.org/3/reference/datamodel.html#object.__getattr__
39 If it doesn't exist, we authorize shortcuts to right function check.
40 """
42 attr = getattr(self._instance, name)
43 return attr