You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.py 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import functools
  2. from collections import Counter, OrderedDict
  3. from pathlib import Path
  4. from django.apps import apps
  5. from django.conf import settings
  6. from django.core.exceptions import ImproperlyConfigured
  7. from django.utils.functional import cached_property
  8. from django.utils.module_loading import import_string
  9. class InvalidTemplateEngineError(ImproperlyConfigured):
  10. pass
  11. class EngineHandler:
  12. def __init__(self, templates=None):
  13. """
  14. templates is an optional list of template engine definitions
  15. (structured like settings.TEMPLATES).
  16. """
  17. self._templates = templates
  18. self._engines = {}
  19. @cached_property
  20. def templates(self):
  21. if self._templates is None:
  22. self._templates = settings.TEMPLATES
  23. templates = OrderedDict()
  24. backend_names = []
  25. for tpl in self._templates:
  26. try:
  27. # This will raise an exception if 'BACKEND' doesn't exist or
  28. # isn't a string containing at least one dot.
  29. default_name = tpl['BACKEND'].rsplit('.', 2)[-2]
  30. except Exception:
  31. invalid_backend = tpl.get('BACKEND', '<not defined>')
  32. raise ImproperlyConfigured(
  33. "Invalid BACKEND for a template engine: {}. Check "
  34. "your TEMPLATES setting.".format(invalid_backend))
  35. tpl = {
  36. 'NAME': default_name,
  37. 'DIRS': [],
  38. 'APP_DIRS': False,
  39. 'OPTIONS': {},
  40. **tpl,
  41. }
  42. templates[tpl['NAME']] = tpl
  43. backend_names.append(tpl['NAME'])
  44. counts = Counter(backend_names)
  45. duplicates = [alias for alias, count in counts.most_common() if count > 1]
  46. if duplicates:
  47. raise ImproperlyConfigured(
  48. "Template engine aliases aren't unique, duplicates: {}. "
  49. "Set a unique NAME for each engine in settings.TEMPLATES."
  50. .format(", ".join(duplicates)))
  51. return templates
  52. def __getitem__(self, alias):
  53. try:
  54. return self._engines[alias]
  55. except KeyError:
  56. try:
  57. params = self.templates[alias]
  58. except KeyError:
  59. raise InvalidTemplateEngineError(
  60. "Could not find config for '{}' "
  61. "in settings.TEMPLATES".format(alias))
  62. # If importing or initializing the backend raises an exception,
  63. # self._engines[alias] isn't set and this code may get executed
  64. # again, so we must preserve the original params. See #24265.
  65. params = params.copy()
  66. backend = params.pop('BACKEND')
  67. engine_cls = import_string(backend)
  68. engine = engine_cls(params)
  69. self._engines[alias] = engine
  70. return engine
  71. def __iter__(self):
  72. return iter(self.templates)
  73. def all(self):
  74. return [self[alias] for alias in self]
  75. @functools.lru_cache()
  76. def get_app_template_dirs(dirname):
  77. """
  78. Return an iterable of paths of directories to load app templates from.
  79. dirname is the name of the subdirectory containing templates inside
  80. installed applications.
  81. """
  82. template_dirs = [
  83. str(Path(app_config.path) / dirname)
  84. for app_config in apps.get_app_configs()
  85. if app_config.path and (Path(app_config.path) / dirname).is_dir()
  86. ]
  87. # Immutable return value because it will be cached and shared by callers.
  88. return tuple(template_dirs)