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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #
  2. # This file is part of Lodel 2 (https://github.com/OpenEdition)
  3. #
  4. # Copyright (C) 2015-2017 Cléo UMS-3287
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as published
  8. # by the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import jinja2
  20. import os
  21. from lodel.context import LodelContext
  22. LodelContext.expose_modules(globals(), {'lodel.settings': ['Settings']})
  23. LodelContext.expose_dyncode(globals())
  24. from ...client import WebUiClient as WebUiClient
  25. from .api import api_lodel_templates
  26. from .exceptions.not_allowed_custom_api_key_error import NotAllowedCustomAPIKeyError
  27. from ...main import root_url as root_url
  28. from ...main import static_url as static_url
  29. from ...main import PLUGIN_PATH
  30. TEMPLATE_PATH = os.path.realpath(os.path.join(PLUGIN_PATH, 'templates/'))
  31. class TemplateLoader(object):
  32. _reserved_template_keys = ['lodel']
  33. ## @brief Initializes a template loader
  34. #
  35. # @param search_path str : the base path from which the templates are searched. To use absolute paths, you can set
  36. # it to the root "/". By default, it will be the root of the project, defined in the settings of the application.
  37. # @param follow_links bool : indicates whether or not to follow the symbolic links (default: True)
  38. # @param is_cache_active bool : indicates whether or not the cache should be activated or not (default: True)
  39. # @todo connect this to the new settings system
  40. def __init__(self, search_path=TEMPLATE_PATH, follow_links=True, is_cache_active=True):
  41. self.search_path = search_path
  42. self.follow_links = follow_links
  43. self.is_cache_active = is_cache_active
  44. ## @brief Renders a HTML content of a template
  45. #
  46. # @see template.loader.TemplateLoader.render_to_response
  47. #
  48. # @return str. String containing the HTML output of the processed templated
  49. def render_to_html(self, template_file, template_vars={}, template_extra=None):
  50. loader = jinja2.FileSystemLoader(searchpath=self.search_path)
  51. environment = jinja2.Environment(loader=loader) if self.is_cache_active else jinja2.Environment(loader=loader,
  52. cache_size=0)
  53. template = environment.get_template(template_file)
  54. # lodel2 default api is loaded
  55. # TODO change this if needed
  56. template.globals['lodel'] = api_lodel_templates
  57. template.globals['leapi'] = leapi_dyncode
  58. template.globals['settings'] = Settings
  59. template.globals['client'] = WebUiClient
  60. template.globals['root_url'] = root_url()
  61. template.globals['static_url'] = static_url()
  62. template.globals['url'] = lambda sufix='': root_url()\
  63. + ('' if sufix.startswith('/') else '/')\
  64. + sufix
  65. # Extra modules are loaded
  66. if template_extra is not None:
  67. for extra in template_extra:
  68. if not self._is_allowed_template_key(extra[0]):
  69. raise NotAllowedCustomAPIKeyError("The name '%s' is a reserved one for the loaded APIs in "
  70. "templates" % extra[0])
  71. template.globals[extra[0]] = extra[1]
  72. return template.render(template_vars)
  73. ## @brief Renders a template into an encoded form ready to be sent to a wsgi response
  74. #
  75. # @param template_file str : path to the template file (starting from the base path used to instanciate the
  76. # TemplateLoader)
  77. # @param template_vars dict : parameters to be used in the template
  78. # @param template_extra list : list of tuples indicating the custom modules to import in the template
  79. # (default: None).
  80. #
  81. # The modules are given as tuples with the format : ('name_to_use_in_the_template', module)
  82. #
  83. # @return str
  84. def render_to_response(self, template_file, template_vars={}, template_extra=None):
  85. return self.render_to_html(template_file=template_file, template_vars=template_vars,
  86. template_extra=template_extra).encode()
  87. ## @brief Checks if the key used for the template is allowed
  88. #
  89. # @param key str
  90. # @return bool
  91. def _is_allowed_template_key(self, key):
  92. return False if key in self.__class__.__reserved_template_keys else True