1
0
Fork 0
mirror of https://github.com/yweber/lodel2.git synced 2026-10-30 00:01:33 +01:00

Regroup all code concerning webui in plugins/webui

This commit is contained in:
Yann 2016-05-20 17:42:28 +02:00
commit 6b5f377dc2
24 changed files with 49 additions and 52 deletions

View file

View file

@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
import os.path
from werkzeug.wrappers import Response
from .template.loader import TemplateLoader
# This module contains the web UI controllers that will be called from the web ui class
def get_response(tpl, mimetype = 'text/html', status_code = 200):
loader = TemplateLoader()
response = Response( loader.render_to_response(tpl),
mimetype = 'text/html')
response.status_code = status_code
return response
def admin(request):
return get_response('admin/admin.html')
def index(request):
return get_response('index/index.html')
def not_found(request):
return get_response('errors/404.html', status_code = 404)
def test(request):
return get_response('test.html')

View file

@ -0,0 +1,12 @@
from werkzeug.wrappers import Request
from werkzeug.urls import url_decode
class LodelRequest(Request):
def __init__(self, environ):
super().__init__(environ)
self.PATH = self.path.lstrip('/')
self.FILES = self.files.to_dict(flat=False)
self.GET = url_decode(self.query_string).to_dict(flat=False)
self.POST = self.form.to_dict(flat=False)

View file

@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
import re
from .controllers import *
from .urls import urls
def get_controller(request):
url_rules = []
for url in urls:
url_rules.append((url[0], url[1]))
# Returning the right controller to call
for regex, callback in url_rules:
match = re.search(regex, request.PATH)
if match is not None:
request.url_args = match.groups()
return callback
return not_found

View file

@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-
# Lodel 2 templates API : loaded by default

View file

@ -0,0 +1,7 @@
#-*- coding: utf-8 -*-
class NotAllowedCustomAPIKeyError(Exception):
def __init__(self, message):
self.message = message

View file

@ -0,0 +1,73 @@
# -*- coding: utf-8 -*-
import jinja2
import os
import settings
from .api import api_lodel_templates
from .exceptions.not_allowed_custom_api_key_error import NotAllowedCustomAPIKeyError
from ...main import PLUGIN_PATH
TEMPLATE_PATH = os.path.realpath(os.path.join(PLUGIN_PATH, 'templates/'))
class TemplateLoader(object):
_reserved_template_keys = ['lodel']
## @brief Initializes a template loader
#
# @param search_path str : the base path from which the templates are searched. To use absolute paths, you can set
# it to the root "/". By default, it will be the root of the project, defined in the settings of the application.
# @param follow_links bool : indicates whether or not to follow the symbolic links (default: True)
# @param is_cache_active bool : indicates whether or not the cache should be activated or not (default: True)
# @todo connect this to the new settings system
def __init__(self, search_path=TEMPLATE_PATH, follow_links=True, is_cache_active=True):
self.search_path = search_path
self.follow_links = follow_links
self.is_cache_active = is_cache_active
## @brief Renders a HTML content of a template
#
# @see template.loader.TemplateLoader.render_to_response
#
# @return str. String containing the HTML output of the processed templated
def render_to_html(self, template_file, template_vars={}, template_extra=None):
loader = jinja2.FileSystemLoader(searchpath=self.search_path)
environment = jinja2.Environment(loader=loader) if self.is_cache_active else jinja2.Environment(loader=loader,
cache_size=0)
template = environment.get_template(template_file)
# lodel2 default api is loaded
# TODO change this if needed
template.globals['lodel'] = api_lodel_templates
# Extra modules are loaded
if template_extra is not None:
for extra in template_extra:
if not self._is_allowed_template_key(extra[0]):
raise NotAllowedCustomAPIKeyError("The name '%s' is a reserved one for the loaded APIs in "
"templates" % extra[0])
template.globals[extra[0]] = extra[1]
return template.render(template_vars)
## @brief Renders a template into an encoded form ready to be sent to a wsgi response
#
# @param template_file str : path to the template file (starting from the base path used to instanciate the
# TemplateLoader)
# @param template_vars dict : parameters to be used in the template
# @param template_extra list : list of tuples indicating the custom modules to import in the template
# (default: None).
#
# The modules are given as tuples with the format : ('name_to_use_in_the_template', module)
#
# @return str
def render_to_response(self, template_file, template_vars={}, template_extra=None):
return self.render_to_html(template_file=template_file, template_vars=template_vars,
template_extra=template_extra).encode()
## @brief Checks if the key used for the template is allowed
#
# @param key str
# @return bool
def _is_allowed_template_key(self, key):
return False if key in self.__class__.__reserved_template_keys else True

View file

@ -0,0 +1,9 @@
from .controllers import *
urls = (
(r'^$', index),
(r'admin/?$', admin),
(r'admin/(.+)$', admin),
(r'test/(.+)$', test),
(r'test/?$', test)
)