Coverage for src/lexigram/admin/actions/standard/utils.py: 38%

13 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-08-24 23:39 +0800

1"""Shared helpers for the standard admin actions.""" 

2 

3from __future__ import annotations 

4 

5from typing import Any 

6 

7from lexigram.admin.actions.types import ActionContext 

8 

9 

10def _extract_id(record: Any) -> Any | None: 

11 """Extract the ``id`` field from a record dict or object. 

12 

13 Args: 

14 record: A record dict or object. 

15 

16 Returns: 

17 The record id, or ``None`` when absent. 

18 """ 

19 if isinstance(record, dict): 

20 return record.get("id") 

21 return getattr(record, "id", None) 

22 

23 

24def _resolve_data_source(ctx: ActionContext, injected: Any | None = None) -> Any | None: 

25 """Resolve the data source for an action execution. 

26 

27 Args: 

28 ctx: Action execution context. 

29 injected: Constructor-injected data source, if any. 

30 

31 Returns: 

32 The resolved data source, or ``None``. 

33 """ 

34 if injected is not None: 

35 return injected 

36 if ctx.data_source is not None: 

37 return ctx.data_source 

38 return ctx.metadata.get("data_source")