Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/django/contrib/admin/decorators.py: 69%

41 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

1def action(function=None, *, permissions=None, description=None): 

2 """ 

3 Conveniently add attributes to an action function:: 

4 

5 @admin.action( 

6 permissions=['publish'], 

7 description='Mark selected stories as published', 

8 ) 

9 def make_published(self, request, queryset): 

10 queryset.update(status='p') 

11 

12 This is equivalent to setting some attributes (with the original, longer 

13 names) on the function directly:: 

14 

15 def make_published(self, request, queryset): 

16 queryset.update(status='p') 

17 make_published.allowed_permissions = ['publish'] 

18 make_published.short_description = 'Mark selected stories as published' 

19 """ 

20 

21 def decorator(func): 

22 if permissions is not None: 

23 func.allowed_permissions = permissions 

24 if description is not None: 24 ↛ 26line 24 didn't jump to line 26, because the condition on line 24 was never false

25 func.short_description = description 

26 return func 

27 

28 if function is None: 28 ↛ 31line 28 didn't jump to line 31, because the condition on line 28 was never false

29 return decorator 

30 else: 

31 return decorator(function) 

32 

33 

34def display( 

35 function=None, *, boolean=None, ordering=None, description=None, empty_value=None 

36): 

37 """ 

38 Conveniently add attributes to a display function:: 

39 

40 @admin.display( 

41 boolean=True, 

42 ordering='-publish_date', 

43 description='Is Published?', 

44 ) 

45 def is_published(self, obj): 

46 return obj.publish_date is not None 

47 

48 This is equivalent to setting some attributes (with the original, longer 

49 names) on the function directly:: 

50 

51 def is_published(self, obj): 

52 return obj.publish_date is not None 

53 is_published.boolean = True 

54 is_published.admin_order_field = '-publish_date' 

55 is_published.short_description = 'Is Published?' 

56 """ 

57 

58 def decorator(func): 

59 if boolean is not None and empty_value is not None: 59 ↛ 60line 59 didn't jump to line 60, because the condition on line 59 was never true

60 raise ValueError( 

61 "The boolean and empty_value arguments to the @display " 

62 "decorator are mutually exclusive." 

63 ) 

64 if boolean is not None: 64 ↛ 65line 64 didn't jump to line 65, because the condition on line 64 was never true

65 func.boolean = boolean 

66 if ordering is not None: 66 ↛ 67line 66 didn't jump to line 67, because the condition on line 66 was never true

67 func.admin_order_field = ordering 

68 if description is not None: 68 ↛ 70line 68 didn't jump to line 70, because the condition on line 68 was never false

69 func.short_description = description 

70 if empty_value is not None: 70 ↛ 71line 70 didn't jump to line 71, because the condition on line 70 was never true

71 func.empty_value_display = empty_value 

72 return func 

73 

74 if function is None: 74 ↛ 77line 74 didn't jump to line 77, because the condition on line 74 was never false

75 return decorator 

76 else: 

77 return decorator(function) 

78 

79 

80def register(*models, site=None): 

81 """ 

82 Register the given model(s) classes and wrapped ModelAdmin class with 

83 admin site: 

84 

85 @register(Author) 

86 class AuthorAdmin(admin.ModelAdmin): 

87 pass 

88 

89 The `site` kwarg is an admin site to use instead of the default admin site. 

90 """ 

91 from django.contrib.admin import ModelAdmin 

92 from django.contrib.admin.sites import AdminSite 

93 from django.contrib.admin.sites import site as default_site 

94 

95 def _model_admin_wrapper(admin_class): 

96 if not models: 96 ↛ 97line 96 didn't jump to line 97, because the condition on line 96 was never true

97 raise ValueError("At least one model must be passed to register.") 

98 

99 admin_site = site or default_site 

100 

101 if not isinstance(admin_site, AdminSite): 101 ↛ 102line 101 didn't jump to line 102, because the condition on line 101 was never true

102 raise ValueError("site must subclass AdminSite") 

103 

104 if not issubclass(admin_class, ModelAdmin): 104 ↛ 105line 104 didn't jump to line 105, because the condition on line 104 was never true

105 raise ValueError("Wrapped class must subclass ModelAdmin.") 

106 

107 admin_site.register(models, admin_class=admin_class) 

108 

109 return admin_class 

110 

111 return _model_admin_wrapper