Açıklama Yok
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.

loader.py 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # -*- coding: utf-8 -*-
  2. import jinja2
  3. import os
  4. import settings
  5. from lodel.template.api import api_lodel_templates
  6. from lodel.template.exceptions.not_allowed_custom_api_key_error import NotAllowedCustomAPIKeyError
  7. class TemplateLoader(object):
  8. _reserved_template_keys = ['lodel']
  9. ## @brief Initializes a template loader
  10. #
  11. # @param search_path str : the base path from which the templates are searched. To use absolute paths, you can set
  12. # it to the root "/". By default, it will be the root of the project, defined in the settings of the application.
  13. # @param follow_links bool : indicates whether or not to follow the symbolic links (default: True)
  14. # @param is_cache_active bool : indicates whether or not the cache should be activated or not (default: True)
  15. # @todo connect this to the new settings system
  16. def __init__(self, search_path=settings.base_path, follow_links=True, is_cache_active=True):
  17. self.search_path = search_path
  18. self.follow_links = follow_links
  19. self.is_cache_active = is_cache_active
  20. ## @brief Renders a HTML content of a template
  21. #
  22. # @see template.loader.TemplateLoader.render_to_response
  23. #
  24. # @return str. String containing the HTML output of the processed templated
  25. def render_to_html(self, template_file, template_vars={}, template_extra=None):
  26. loader = jinja2.FileSystemLoader(searchpath=self.search_path)
  27. environment = jinja2.Environment(loader=loader) if self.is_cache_active else jinja2.Environment(loader=loader,
  28. cache_size=0)
  29. template = environment.get_template(template_file)
  30. # lodel2 default api is loaded
  31. # TODO change this if needed
  32. template.globals['lodel'] = api_lodel_templates
  33. # Extra modules are loaded
  34. if template_extra is not None:
  35. for extra in template_extra:
  36. if not self._is_allowed_template_key(extra[0]):
  37. raise NotAllowedCustomAPIKeyError("The name '%s' is a reserved one for the loaded APIs in "
  38. "templates" % extra[0])
  39. template.globals[extra[0]] = extra[1]
  40. return template.render(template_vars)
  41. ## @brief Renders a template into an encoded form ready to be sent to a wsgi response
  42. #
  43. # @param template_file str : path to the template file (starting from the base path used to instanciate the
  44. # TemplateLoader)
  45. # @param template_vars dict : parameters to be used in the template
  46. # @param template_extra list : list of tuples indicating the custom modules to import in the template
  47. # (default: None).
  48. #
  49. # The modules are given as tuples with the format : ('name_to_use_in_the_template', module)
  50. #
  51. # @return str
  52. def render_to_response(self, template_file, template_vars={}, template_extra=None):
  53. return self.render_to_html(template_file=template_file, template_vars=template_vars,
  54. template_extra=template_extra).encode()
  55. ## @brief Checks if the key used for the template is allowed
  56. #
  57. # @param key str
  58. # @return bool
  59. def _is_allowed_template_key(self, key):
  60. return False if key in self.__class__.__reserved_template_keys else True