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

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