| 59 | |
| 60 | |
| 61 | class ResponseBaseModel(ABC): |
| 62 | def __init__(self, container: Container): |
| 63 | self.container = container |
| 64 | |
| 65 | @property |
| 66 | @abstractmethod |
| 67 | def model_name(self) -> str: |
| 68 | pass |
| 69 | |
| 70 | @property |
| 71 | @abstractmethod |
| 72 | def relationships(self) -> dict[str, RelationshipConfig]: |
| 73 | pass |
| 74 | |
| 75 | def get_resource_type(self) -> str: |
| 76 | return self.model_name |
| 77 | |
| 78 | def get_service(self, service_name: str) -> Any: |
| 79 | return getattr(self.container, service_name)() |
| 80 | |
| 81 | async def process_includes(self, data: Any, include_params: dict[str, bool]) -> dict[str, list[JsonApiResource]]: |
| 82 | included: dict[str, list[JsonApiResource]] = {} |
| 83 | if not include_params: |
| 84 | return included |
| 85 | |
| 86 | for param_name, is_include in include_params.items(): |
| 87 | if not is_include: |
| 88 | continue |
| 89 | |
| 90 | relationship_name = self._param_to_relationship_name(param_name) |
| 91 | if relationship_name not in self.relationships: |
| 92 | continue |
| 93 | |
| 94 | relationship_config = self.relationships[relationship_name] |
| 95 | if isinstance(data, (list, Sequence)): |
| 96 | included[relationship_name] = await self._process_relationship_for_list(data, relationship_config) |
| 97 | else: |
| 98 | included[relationship_name] = await self._process_relationship_for_item(data, relationship_config) |
| 99 | |
| 100 | return included |
| 101 | |
| 102 | @staticmethod |
| 103 | def _param_to_relationship_name(param_name: str) -> str: |
| 104 | if param_name.startswith("with"): |
| 105 | param_name = param_name.replace("with", "").strip("_") |
| 106 | return param_name |
| 107 | |
| 108 | async def _process_relationship_for_list( |
| 109 | self, data_list: list[Any] | Sequence[Any], config: RelationshipConfig |
| 110 | ) -> list[JsonApiResource]: |
| 111 | if not data_list: |
| 112 | return [] |
| 113 | |
| 114 | ids = [] |
| 115 | for item in data_list: |
| 116 | if hasattr(item, config.local_key): |
| 117 | ids.append(getattr(item, config.local_key)) |
| 118 |
nothing calls this directly
no outgoing calls
no test coverage detected