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.9KB

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