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.

renderers.py 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import functools
  2. from pathlib import Path
  3. from django.conf import settings
  4. from django.template.backends.django import DjangoTemplates
  5. from django.template.loader import get_template
  6. from django.utils.functional import cached_property
  7. from django.utils.module_loading import import_string
  8. @functools.lru_cache
  9. def get_default_renderer():
  10. renderer_class = import_string(settings.FORM_RENDERER)
  11. return renderer_class()
  12. class BaseRenderer:
  13. # RemovedInDjango50Warning: When the deprecation ends, replace with
  14. # form_template_name = "django/forms/div.html"
  15. # formset_template_name = "django/forms/formsets/div.html"
  16. form_template_name = "django/forms/default.html"
  17. formset_template_name = "django/forms/formsets/default.html"
  18. def get_template(self, template_name):
  19. raise NotImplementedError("subclasses must implement get_template()")
  20. def render(self, template_name, context, request=None):
  21. template = self.get_template(template_name)
  22. return template.render(context, request=request).strip()
  23. class EngineMixin:
  24. def get_template(self, template_name):
  25. return self.engine.get_template(template_name)
  26. @cached_property
  27. def engine(self):
  28. return self.backend(
  29. {
  30. "APP_DIRS": True,
  31. "DIRS": [Path(__file__).parent / self.backend.app_dirname],
  32. "NAME": "djangoforms",
  33. "OPTIONS": {},
  34. }
  35. )
  36. class DjangoTemplates(EngineMixin, BaseRenderer):
  37. """
  38. Load Django templates from the built-in widget templates in
  39. django/forms/templates and from apps' 'templates' directory.
  40. """
  41. backend = DjangoTemplates
  42. class Jinja2(EngineMixin, BaseRenderer):
  43. """
  44. Load Jinja2 templates from the built-in widget templates in
  45. django/forms/jinja2 and from apps' 'jinja2' directory.
  46. """
  47. @cached_property
  48. def backend(self):
  49. from django.template.backends.jinja2 import Jinja2
  50. return Jinja2
  51. class DjangoDivFormRenderer(DjangoTemplates):
  52. """
  53. Load Django templates from django/forms/templates and from apps'
  54. 'templates' directory and use the 'div.html' template to render forms and
  55. formsets.
  56. """
  57. # RemovedInDjango50Warning Deprecate this class in 5.0 and remove in 6.0.
  58. form_template_name = "django/forms/div.html"
  59. formset_template_name = "django/forms/formsets/div.html"
  60. class Jinja2DivFormRenderer(Jinja2):
  61. """
  62. Load Jinja2 templates from the built-in widget templates in
  63. django/forms/jinja2 and from apps' 'jinja2' directory.
  64. """
  65. # RemovedInDjango50Warning Deprecate this class in 5.0 and remove in 6.0.
  66. form_template_name = "django/forms/div.html"
  67. formset_template_name = "django/forms/formsets/div.html"
  68. class TemplatesSetting(BaseRenderer):
  69. """
  70. Load templates using template.loader.get_template() which is configured
  71. based on settings.TEMPLATES.
  72. """
  73. def get_template(self, template_name):
  74. return get_template(template_name)