Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/django/template/backends/base.py: 54%

31 statements  

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

1from django.core.exceptions import ImproperlyConfigured, SuspiciousFileOperation 

2from django.template.utils import get_app_template_dirs 

3from django.utils._os import safe_join 

4from django.utils.functional import cached_property 

5 

6 

7class BaseEngine: 

8 

9 # Core methods: engines have to provide their own implementation 

10 # (except for from_string which is optional). 

11 

12 def __init__(self, params): 

13 """ 

14 Initialize the template engine. 

15 

16 `params` is a dict of configuration settings. 

17 """ 

18 params = params.copy() 

19 self.name = params.pop("NAME") 

20 self.dirs = list(params.pop("DIRS")) 

21 self.app_dirs = params.pop("APP_DIRS") 

22 if params: 22 ↛ 23line 22 didn't jump to line 23, because the condition on line 22 was never true

23 raise ImproperlyConfigured( 

24 "Unknown parameters: {}".format(", ".join(params)) 

25 ) 

26 

27 @property 

28 def app_dirname(self): 

29 raise ImproperlyConfigured( 

30 "{} doesn't support loading templates from installed " 

31 "applications.".format(self.__class__.__name__) 

32 ) 

33 

34 def from_string(self, template_code): 

35 """ 

36 Create and return a template for the given source code. 

37 

38 This method is optional. 

39 """ 

40 raise NotImplementedError( 

41 "subclasses of BaseEngine should provide a from_string() method" 

42 ) 

43 

44 def get_template(self, template_name): 

45 """ 

46 Load and return a template for the given name. 

47 

48 Raise TemplateDoesNotExist if no such template exists. 

49 """ 

50 raise NotImplementedError( 

51 "subclasses of BaseEngine must provide a get_template() method" 

52 ) 

53 

54 # Utility methods: they are provided to minimize code duplication and 

55 # security issues in third-party backends. 

56 

57 @cached_property 

58 def template_dirs(self): 

59 """ 

60 Return a list of directories to search for templates. 

61 """ 

62 # Immutable return value because it's cached and shared by callers. 

63 template_dirs = tuple(self.dirs) 

64 if self.app_dirs: 

65 template_dirs += get_app_template_dirs(self.app_dirname) 

66 return template_dirs 

67 

68 def iter_template_filenames(self, template_name): 

69 """ 

70 Iterate over candidate files for template_name. 

71 

72 Ignore files that don't lie inside configured template dirs to avoid 

73 directory traversal attacks. 

74 """ 

75 for template_dir in self.template_dirs: 

76 try: 

77 yield safe_join(template_dir, template_name) 

78 except SuspiciousFileOperation: 

79 # The joined path was located outside of this template_dir 

80 # (it might be inside another one, so this isn't fatal). 

81 pass