Debian Codegraph @

django_jinja/base.py

raw symbols SCIP
1 import re
2 import os.path as path
3 from importlib import import_module
4
5 from django.template.context import BaseContext
6
7
8 def dict_from_context(context):
9 """
10 Converts context to native python dict.
11 """
12
13 if isinstance(context, BaseContext):
14 new_dict = {}
15 for i in reversed(list(context)):
16 new_dict.update(dict_from_context(i))
17 return new_dict
18
19 return dict(context)
20
21
22 def _iter_templatetags_modules_list():
23 """
24 Get list of modules that contains templatetags
25 submodule.
26 """
27 from django.apps import apps
28 all_modules = [x.name for x in apps.get_app_configs()]
29
30 for app_path in all_modules:
31 try:
32 mod = import_module(app_path + ".templatetags")
33 # Empty folders can lead to unexpected behavior with Python 3.
34 # We make sure to have the `__file__` attribute.
35 if getattr(mod, '__file__', None) is not None:
36 yield (app_path, path.dirname(mod.__file__))
37 except ImportError:
38 pass
39
40
41 def patch_django_for_autoescape():
42 """
43 Patch django modules for make them compatible with
44 jinja autoescape implementation.
45 """
46 from django.utils import safestring
47 from django.forms.boundfield import BoundField
48 from django.forms.utils import ErrorList
49 from django.forms.utils import ErrorDict
50
51 if hasattr(safestring, "SafeText"):
52 if not hasattr(safestring.SafeText, "__html__"):
53 safestring.SafeText.__html__ = lambda self: str(self)
54
55 if hasattr(safestring, "SafeString"):
56 if not hasattr(safestring.SafeString, "__html__"):
57 safestring.SafeString.__html__ = lambda self: str(self)
58
59 if hasattr(safestring, "SafeUnicode"):
60 if not hasattr(safestring.SafeUnicode, "__html__"):
61 safestring.SafeUnicode.__html__ = lambda self: str(self)
62
63 if hasattr(safestring, "SafeBytes"):
64 if not hasattr(safestring.SafeBytes, "__html__"):
65 safestring.SafeBytes.__html__ = lambda self: str(self)
66
67 if not hasattr(BoundField, "__html__"):
68 BoundField.__html__ = lambda self: str(self)
69
70 if not hasattr(ErrorList, "__html__"):
71 ErrorList.__html__ = lambda self: str(self)
72
73 if not hasattr(ErrorDict, "__html__"):
74 ErrorDict.__html__ = lambda self: str(self)
75
76
77 def get_match_extension(using=None):
78 """
79 Gets the extension that the template loader will match for
80 django-jinja. This returns Jinja2.match_extension.
81
82 The "using" parameter selects with Jinja2 backend to use if
83 you have multiple ones configured in settings.TEMPLATES.
84 If it is None and only one Jinja2 backend is defined then it
85 will use that, otherwise an ImproperlyConfigured exception
86 is thrown.
87 """
88 from .backend import Jinja2
89 from django.template import engines
90
91 if using is None:
92 engine = Jinja2.get_default()
93 else:
94 engine = engines[using]
95
96 return engine.match_extension
97
98
99 def match_template(template_name, extension, regex):
100 if extension:
101 matches_extension = template_name.endswith(extension)
102 if regex:
103 return matches_extension and re.match(regex, template_name)
104 else:
105 return matches_extension
106 elif regex:
107 return re.match(regex, template_name)
108 else:
109 return True