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

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