Coverage for apis_core/history/tables.py: 83%

36 statements  

« prev     ^ index     » next       coverage.py v7.5.3, created at 2025-09-03 06:15 +0000

1import django_tables2 as tables 

2from django.apps import apps 

3 

4from apis_core.generic.tables import ActionColumn, CustomTemplateColumn, ViewColumn 

5 

6 

7class DescriptionColumnHistory(CustomTemplateColumn): 

8 """ 

9 A column showing a model description 

10 """ 

11 

12 template_name = "history/columns/description.html" 

13 orderable = False 

14 

15 

16class OriginalIDColumn(CustomTemplateColumn): 

17 """ 

18 A column showing the original id of a model instance 

19 """ 

20 

21 template_name = "history/columns/original_id.html" 

22 orderable = False 

23 verbose_name = "Original ID" 

24 

25 

26class ResetColumn(ActionColumn): 

27 """ 

28 A column showing a reset button 

29 """ 

30 

31 template_name = "history/columns/reset.html" 

32 permission = "change" 

33 

34 

35class APISHistoryTableBaseTable(tables.Table): 

36 history_id = tables.Column(verbose_name="ID") 

37 original_id = OriginalIDColumn() 

38 desc = DescriptionColumnHistory() 

39 view = ViewColumn() 

40 

41 class Meta: 

42 fields = ["history_id", "desc", "most_recent", "view"] 

43 

44 

45class HistoryGenericTable(tables.Table): 

46 model = tables.Column(empty_values=()) 

47 fields_changed = tables.Column(empty_values=()) 

48 instance = tables.Column(linkify=lambda record: record.get_absolute_url()) 

49 fields_changed = tables.TemplateColumn( 

50 template_name="history/columns/fields_changed.html" 

51 ) 

52 reset = ResetColumn() 

53 

54 class Meta: 

55 fields = [ 

56 "model", 

57 "instance", 

58 "fields_changed", 

59 "history_type", 

60 "history_date", 

61 "history_user", 

62 ] 

63 

64 def render_model(self, record): 

65 return record.instance.__class__.__name__ 

66 

67 def __init__(self, *args, **kwargs): 

68 print(kwargs) 

69 if apps.is_installed("apis_core.collections"): 

70 from apis_core.collections.columns import CollectionsColumn 

71 

72 kwargs["extra_columns"] = [("collections", CollectionsColumn())] 

73 super().__init__(*args, **kwargs)