Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
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.

filesystem.py 1.5KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. """
  2. Wrapper for loading templates from the filesystem.
  3. """
  4. from django.core.exceptions import SuspiciousFileOperation
  5. from django.template import Origin, TemplateDoesNotExist
  6. from django.utils._os import safe_join
  7. from .base import Loader as BaseLoader
  8. class Loader(BaseLoader):
  9. def __init__(self, engine, dirs=None):
  10. super().__init__(engine)
  11. self.dirs = dirs
  12. def get_dirs(self):
  13. return self.dirs if self.dirs is not None else self.engine.dirs
  14. def get_contents(self, origin):
  15. try:
  16. with open(origin.name, encoding=self.engine.file_charset) as fp:
  17. return fp.read()
  18. except FileNotFoundError:
  19. raise TemplateDoesNotExist(origin)
  20. def get_template_sources(self, template_name):
  21. """
  22. Return an Origin object pointing to an absolute path in each directory
  23. in template_dirs. For security reasons, if a path doesn't lie inside
  24. one of the template_dirs it is excluded from the result set.
  25. """
  26. for template_dir in self.get_dirs():
  27. try:
  28. name = safe_join(template_dir, template_name)
  29. except SuspiciousFileOperation:
  30. # The joined path was located outside of this template_dir
  31. # (it might be inside another one, so this isn't fatal).
  32. continue
  33. yield Origin(
  34. name=name,
  35. template_name=template_name,
  36. loader=self,
  37. )