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

49 statements  

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

1import pathlib 

2 

3from django.conf import settings 

4from django.core.cache import DEFAULT_CACHE_ALIAS, caches 

5from django.core.cache.backends.filebased import FileBasedCache 

6 

7from . import Error, Tags, Warning, register 

8 

9E001 = Error( 

10 "You must define a '%s' cache in your CACHES setting." % DEFAULT_CACHE_ALIAS, 

11 id="caches.E001", 

12) 

13 

14 

15@register(Tags.caches) 

16def check_default_cache_is_configured(app_configs, **kwargs): 

17 if DEFAULT_CACHE_ALIAS not in settings.CACHES: 17 ↛ 18line 17 didn't jump to line 18, because the condition on line 17 was never true

18 return [E001] 

19 return [] 

20 

21 

22@register(Tags.caches, deploy=True) 

23def check_cache_location_not_exposed(app_configs, **kwargs): 

24 errors = [] 

25 for name in ("MEDIA_ROOT", "STATIC_ROOT", "STATICFILES_DIRS"): 

26 setting = getattr(settings, name, None) 

27 if not setting: 

28 continue 

29 if name == "STATICFILES_DIRS": 

30 paths = set() 

31 for staticfiles_dir in setting: 

32 if isinstance(staticfiles_dir, (list, tuple)): 

33 _, staticfiles_dir = staticfiles_dir 

34 paths.add(pathlib.Path(staticfiles_dir).resolve()) 

35 else: 

36 paths = {pathlib.Path(setting).resolve()} 

37 for alias in settings.CACHES: 

38 cache = caches[alias] 

39 if not isinstance(cache, FileBasedCache): 

40 continue 

41 cache_path = pathlib.Path(cache._dir).resolve() 

42 if any(path == cache_path for path in paths): 

43 relation = "matches" 

44 elif any(path in cache_path.parents for path in paths): 

45 relation = "is inside" 

46 elif any(cache_path in path.parents for path in paths): 

47 relation = "contains" 

48 else: 

49 continue 

50 errors.append( 

51 Warning( 

52 f"Your '{alias}' cache configuration might expose your cache " 

53 f"or lead to corruption of your data because its LOCATION " 

54 f"{relation} {name}.", 

55 id="caches.W002", 

56 ) 

57 ) 

58 return errors 

59 

60 

61@register(Tags.caches) 

62def check_file_based_cache_is_absolute(app_configs, **kwargs): 

63 errors = [] 

64 for alias, config in settings.CACHES.items(): 

65 cache = caches[alias] 

66 if not isinstance(cache, FileBasedCache): 66 ↛ 68line 66 didn't jump to line 68, because the condition on line 66 was never false

67 continue 

68 if not pathlib.Path(config["LOCATION"]).is_absolute(): 

69 errors.append( 

70 Warning( 

71 f"Your '{alias}' cache LOCATION path is relative. Use an " 

72 f"absolute path instead.", 

73 id="caches.W003", 

74 ) 

75 ) 

76 return errors