Coverage for src / lexigram / admin / ui / columns / column / formatting.py: 60%

10 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-08-10 14:02 +0800

1""" 

2Column formatting and masking methods. 

3""" 

4 

5from __future__ import annotations 

6 

7from collections.abc import Callable 

8from typing import Any, Self 

9 

10 

11class ColumnFormattingMixin: 

12 """Mixin class containing formatting and masking methods.""" 

13 

14 def format_state_using(self, callback: Callable[[Any], Any]) -> Self: 

15 """ 

16 Custom formatting function. 

17 

18 Allows you to transform the value before rendering. 

19 

20 Args: 

21 callback: Function that takes the value and returns formatted value 

22 

23 Returns: 

24 Self for method chaining 

25 

26 Example: 

27 >>> def uppercase(value): 

28 ... return str(value).upper() 

29 >>> TextColumn("name").format_state_using(uppercase) 

30 >>> 

31 >>> # Lambda example 

32 >>> TextColumn("price").format_state_using(lambda x: f"${x:.2f}") 

33 """ 

34 self._format_callback = callback 

35 return self 

36 

37 def mask(self, masker: Callable[[Any], str]) -> Self: 

38 """ 

39 Add a data masker to the column. 

40 

41 Maskers transform the value for display to protect sensitive information. 

42 

43 Args: 

44 masker: Function that takes the value and returns a masked version 

45 

46 Returns: 

47 Self for method chaining 

48 

49 Example: 

50 >>> from lexigram.admin.security.masking import DataMasker 

51 >>> TextColumn("email").mask(DataMasker.mask_email) 

52 """ 

53 self._masker = masker 

54 return self