No Description
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.4KB

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