Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/django/core/checks/registry.py: 81%

59 statements  

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

1from itertools import chain 

2 

3from django.utils.inspect import func_accepts_kwargs 

4from django.utils.itercompat import is_iterable 

5 

6 

7class Tags: 

8 """ 

9 Built-in tags for internal checks. 

10 """ 

11 

12 admin = "admin" 

13 async_support = "async_support" 

14 caches = "caches" 

15 compatibility = "compatibility" 

16 database = "database" 

17 files = "files" 

18 models = "models" 

19 security = "security" 

20 signals = "signals" 

21 sites = "sites" 

22 staticfiles = "staticfiles" 

23 templates = "templates" 

24 translation = "translation" 

25 urls = "urls" 

26 

27 

28class CheckRegistry: 

29 def __init__(self): 

30 self.registered_checks = set() 

31 self.deployment_checks = set() 

32 

33 def register(self, check=None, *tags, **kwargs): 

34 """ 

35 Can be used as a function or a decorator. Register given function 

36 `f` labeled with given `tags`. The function should receive **kwargs 

37 and return list of Errors and Warnings. 

38 

39 Example:: 

40 

41 registry = CheckRegistry() 

42 @registry.register('mytag', 'anothertag') 

43 def my_check(app_configs, **kwargs): 

44 # ... perform checks and collect `errors` ... 

45 return errors 

46 # or 

47 registry.register(my_check, 'mytag', 'anothertag') 

48 """ 

49 

50 def inner(check): 

51 if not func_accepts_kwargs(check): 51 ↛ 52line 51 didn't jump to line 52, because the condition on line 51 was never true

52 raise TypeError( 

53 "Check functions must accept keyword arguments (**kwargs)." 

54 ) 

55 check.tags = tags 

56 checks = ( 

57 self.deployment_checks 

58 if kwargs.get("deploy") 

59 else self.registered_checks 

60 ) 

61 checks.add(check) 

62 return check 

63 

64 if callable(check): 

65 return inner(check) 

66 else: 

67 if check: 67 ↛ 69line 67 didn't jump to line 69, because the condition on line 67 was never false

68 tags += (check,) 

69 return inner 

70 

71 def run_checks( 

72 self, 

73 app_configs=None, 

74 tags=None, 

75 include_deployment_checks=False, 

76 databases=None, 

77 ): 

78 """ 

79 Run all registered checks and return list of Errors and Warnings. 

80 """ 

81 errors = [] 

82 checks = self.get_checks(include_deployment_checks) 

83 

84 if tags is not None: 84 ↛ 85line 84 didn't jump to line 85, because the condition on line 84 was never true

85 checks = [check for check in checks if not set(check.tags).isdisjoint(tags)] 

86 

87 for check in checks: 

88 new_errors = check(app_configs=app_configs, databases=databases) 

89 if not is_iterable(new_errors): 89 ↛ 90line 89 didn't jump to line 90, because the condition on line 89 was never true

90 raise TypeError( 

91 "The function %r did not return a list. All functions " 

92 "registered with the checks registry must return a list." % check, 

93 ) 

94 errors.extend(new_errors) 

95 return errors 

96 

97 def tag_exists(self, tag, include_deployment_checks=False): 

98 return tag in self.tags_available(include_deployment_checks) 

99 

100 def tags_available(self, deployment_checks=False): 

101 return set( 

102 chain.from_iterable( 

103 check.tags for check in self.get_checks(deployment_checks) 

104 ) 

105 ) 

106 

107 def get_checks(self, include_deployment_checks=False): 

108 checks = list(self.registered_checks) 

109 if include_deployment_checks: 109 ↛ 110line 109 didn't jump to line 110, because the condition on line 109 was never true

110 checks.extend(self.deployment_checks) 

111 return checks 

112 

113 

114registry = CheckRegistry() 

115register = registry.register 

116run_checks = registry.run_checks 

117tag_exists = registry.tag_exists