暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

loader.py 3.7KB

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