Coverage for apis_core/generic/tables.py: 80%

49 statements  

« prev     ^ index     » next       coverage.py v7.6.8, created at 2024-12-20 09:24 +0000

1import django_tables2 as tables 

2 

3from apis_core.generic.helpers import permission_fullname 

4 

5 

6class CustomTemplateColumn(tables.TemplateColumn): 

7 """ 

8 A custom template column - the `tables.TemplateColumn` class does not allow 

9 to set attributes via class variables. Therefor we use this 

10 CustomTemplateColumn to set some arguments based on class attributes and 

11 override the attributes in child classes. 

12 """ 

13 

14 template_name = None 

15 orderable = None 

16 exclude_from_export = False 

17 verbose_name = None 

18 

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

20 super().__init__( 

21 template_name=self.template_name, 

22 orderable=self.orderable, 

23 exclude_from_export=self.exclude_from_export, 

24 verbose_name=self.verbose_name, 

25 *args, 

26 **kwargs, 

27 ) 

28 

29 

30class ActionColumn(CustomTemplateColumn): 

31 """ 

32 A custom template column with some additional attributes 

33 for actions. 

34 """ 

35 

36 orderable = False 

37 exclude_from_export = True 

38 verbose_name = "" 

39 attrs = {"td": {"style": "width:1%;"}} 

40 

41 def render(self, record, table, *args, **kwargs): 

42 if permission := getattr(self, "permission", False): 

43 if not table.request.user.has_perm(permission_fullname(permission, record)): 

44 return "" 

45 return super().render(record, table, *args, **kwargs) 

46 

47 

48class DeleteColumn(ActionColumn): 

49 """ 

50 A column showing a delete button 

51 """ 

52 

53 template_name = "columns/delete.html" 

54 permission = "delete" 

55 

56 

57class EditColumn(ActionColumn): 

58 """ 

59 A column showing an edit button 

60 """ 

61 

62 template_name = "columns/edit.html" 

63 permission = "change" 

64 

65 

66class ViewColumn(ActionColumn): 

67 """ 

68 A column showing a view button 

69 """ 

70 

71 template_name = "columns/view.html" 

72 permission = "view" 

73 

74 

75class DescriptionColumn(CustomTemplateColumn): 

76 """ 

77 A column showing a model description 

78 """ 

79 

80 template_name = "columns/description.html" 

81 orderable = False 

82 

83 

84class GenericTable(tables.Table): 

85 """ 

86 A generic table that contains an edit button column, a delete button column 

87 and a description column 

88 """ 

89 

90 edit = EditColumn() 

91 desc = DescriptionColumn() 

92 delete = DeleteColumn() 

93 view = ViewColumn() 

94 

95 class Meta: 

96 fields = ["id", "desc"] 

97 sequence = ("...", "view", "edit", "delete") 

98 

99 

100class MoreLessColumn(tables.TemplateColumn): 

101 """ 

102 Useful for displaying long fields. 

103 A preview is shown initially with a "Show more" link 

104 which is replaced with a "Show less" link when expanded. 

105 """ 

106 

107 template_name = "columns/more-less.html" 

108 

109 def __init__(self, preview, fulltext, *args, **kwargs): 

110 self.preview = preview 

111 self.fulltext = fulltext 

112 super().__init__(template_name=self.template_name, *args, **kwargs) 

113 

114 def render(self, record, **kwargs): 

115 self.extra_context["preview"] = self.preview(record) 

116 self.extra_context["fulltext"] = self.fulltext(record) 

117 return super().render(record, **kwargs)