mirror of
https://github.com/yweber/lodel2.git
synced 2026-11-17 16:41:38 +01:00
Regroup all code concerning webui in plugins/webui
This commit is contained in:
parent
5991bc8e6f
commit
6b5f377dc2
24 changed files with 49 additions and 52 deletions
0
plugins/webui/interface/__init__.py
Normal file
0
plugins/webui/interface/__init__.py
Normal file
26
plugins/webui/interface/controllers.py
Normal file
26
plugins/webui/interface/controllers.py
Normal 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')
|
||||
12
plugins/webui/interface/lodelrequest.py
Normal file
12
plugins/webui/interface/lodelrequest.py
Normal 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)
|
||||
20
plugins/webui/interface/router.py
Normal file
20
plugins/webui/interface/router.py
Normal 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
|
||||
0
plugins/webui/interface/template/__init__.py
Normal file
0
plugins/webui/interface/template/__init__.py
Normal file
0
plugins/webui/interface/template/api/__init__.py
Normal file
0
plugins/webui/interface/template/api/__init__.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Lodel 2 templates API : loaded by default
|
||||
0
plugins/webui/interface/template/exceptions/__init__.py
Normal file
0
plugins/webui/interface/template/exceptions/__init__.py
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
#-*- coding: utf-8 -*-
|
||||
|
||||
|
||||
class NotAllowedCustomAPIKeyError(Exception):
|
||||
|
||||
def __init__(self, message):
|
||||
self.message = message
|
||||
73
plugins/webui/interface/template/loader.py
Normal file
73
plugins/webui/interface/template/loader.py
Normal 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
|
||||
9
plugins/webui/interface/urls.py
Normal file
9
plugins/webui/interface/urls.py
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
from .controllers import *
|
||||
|
||||
urls = (
|
||||
(r'^$', index),
|
||||
(r'admin/?$', admin),
|
||||
(r'admin/(.+)$', admin),
|
||||
(r'test/(.+)$', test),
|
||||
(r'test/?$', test)
|
||||
)
|
||||
|
|
@ -1,14 +1,16 @@
|
|||
#-*- coding: utf-8 -*-
|
||||
|
||||
import os
|
||||
import os, os.path
|
||||
from lodel.plugin import LodelHook
|
||||
from lodel.settings import Settings
|
||||
|
||||
PLUGIN_PATH = os.path.dirname(__file__)
|
||||
|
||||
##@brief uwsgi startup demo
|
||||
@LodelHook('lodel2_loader_main')
|
||||
def uwsgi_fork(hook_name, caller, payload):
|
||||
if Settings.webui.standalone:
|
||||
cmd='uwsgi_python3 --http-socket {addr}:{port} --module run'
|
||||
cmd='uwsgi_python3 --http-socket {addr}:{port} --module plugins.webui.run'
|
||||
cmd = cmd.format(
|
||||
addr = Settings.webui.listen_address,
|
||||
port = Settings.webui.listen_port)
|
||||
|
|
|
|||
63
plugins/webui/run.py
Normal file
63
plugins/webui/run.py
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
import loader # Lodel2 loader
|
||||
|
||||
import os
|
||||
from werkzeug.contrib.sessions import FilesystemSessionStore
|
||||
|
||||
from lodel.settings import Settings
|
||||
from .interface.router import get_controller
|
||||
from .interface.lodelrequest import LodelRequest
|
||||
from lodel.utils.datetime import get_utc_timestamp
|
||||
|
||||
SESSION_FILES_BASE_DIR = Settings.webui.sessions.directory
|
||||
SESSION_FILES_TEMPLATE = Settings.webui.sessions.file_template
|
||||
SESSION_EXPIRATION_LIMIT = Settings.webui.sessions.expiration
|
||||
|
||||
session_store = FilesystemSessionStore(path=SESSION_FILES_BASE_DIR, filename_template=SESSION_FILES_TEMPLATE)
|
||||
|
||||
|
||||
# TODO déplacer dans un module "sessions.py"
|
||||
def delete_old_session_files(timestamp_now):
|
||||
session_files_path = os.path.abspath(session_store.path)
|
||||
session_files = [os.path.join(session_files_path, file_object) for file_object in os.listdir(session_files_path)
|
||||
if os.path.isfile(os.path.join(session_files_path, file_object))]
|
||||
|
||||
for session_file in session_files:
|
||||
last_modified = os.stat(session_file).st_mtime
|
||||
expiration_timestamp = last_modified + SESSION_EXPIRATION_LIMIT
|
||||
if timestamp_now > expiration_timestamp:
|
||||
os.unlink(session_file)
|
||||
|
||||
|
||||
def is_session_file_expired(timestamp_now, sid):
|
||||
session_file = session_store.get_session_filename(sid)
|
||||
expiration_timestamp = os.stat(session_file).st_mtime + SESSION_EXPIRATION_LIMIT
|
||||
if timestamp_now < expiration_timestamp:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
# WSGI Application
|
||||
def application(env, start_response):
|
||||
current_timestamp = get_utc_timestamp()
|
||||
delete_old_session_files(current_timestamp)
|
||||
request = LodelRequest(env)
|
||||
sid = request.cookies.get('sid')
|
||||
if sid is None or sid not in session_store.list():
|
||||
request.session = session_store.new()
|
||||
request.session['last_accessed'] = current_timestamp
|
||||
else:
|
||||
request.session = session_store.get(sid)
|
||||
if is_session_file_expired(current_timestamp, sid):
|
||||
session_store.delete(request.session)
|
||||
request.session = session_store.new()
|
||||
request.session['user_context'] = None
|
||||
request.session['last_accessed'] = current_timestamp
|
||||
|
||||
controller = get_controller(request)
|
||||
response = controller(request)
|
||||
if request.session.should_save:
|
||||
session_store.save(request.session)
|
||||
response.set_cookie('sid', request.session.sid)
|
||||
|
||||
return response(env, start_response)
|
||||
3
plugins/webui/templates/admin/admin.html
Normal file
3
plugins/webui/templates/admin/admin.html
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
{% extends "base_backend.html" %}
|
||||
{% block title %}Lodel 2 - ADMIN{% endblock %}
|
||||
{% block content %}ADMIN{% endblock %}
|
||||
15
plugins/webui/templates/base.html
Normal file
15
plugins/webui/templates/base.html
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<title>{% block title %}{% endblock %}</title>
|
||||
{% block style %}{% endblock %}
|
||||
{% block scripts %}{% endblock %}
|
||||
</head>
|
||||
<body>
|
||||
<div id="content">
|
||||
{% block content %}{% endblock %}
|
||||
</div>
|
||||
<script type="text/javascript">{% block javascript %}{% endblock %}</script>
|
||||
</body>
|
||||
</html>
|
||||
15
plugins/webui/templates/base_backend.html
Normal file
15
plugins/webui/templates/base_backend.html
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<title>{% block title %}{% endblock %}</title>
|
||||
{% block style %}{% endblock %}
|
||||
{% block scripts %}{% endblock %}
|
||||
</head>
|
||||
<body>
|
||||
<div id="content">
|
||||
{% block content %}{% endblock %}
|
||||
</div>
|
||||
<script type="text/javascript">{% block javascript %}{% endblock %}</script>
|
||||
</body>
|
||||
</html>
|
||||
5
plugins/webui/templates/errors/404.html
Normal file
5
plugins/webui/templates/errors/404.html
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
{% extends "templates/base.html" %}
|
||||
{% block title %}Error 404{% endblock %}
|
||||
{% block content %}
|
||||
<h1>404 - File Not Found</h1>
|
||||
{% endblock %}
|
||||
3
plugins/webui/templates/index/index.html
Normal file
3
plugins/webui/templates/index/index.html
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
{% extends "base.html" %}
|
||||
{% block title %}Lodel 2 - DASHBOARD{% endblock %}
|
||||
{% block content %}DASHBOARD{% endblock %}
|
||||
15
plugins/webui/templates/test.html
Normal file
15
plugins/webui/templates/test.html
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
<html>
|
||||
<head></head>
|
||||
<body>
|
||||
<form action="http://localhost:9090/admin?r=1&rand[]=7&rand[]=5" method="POST" enctype="multipart/form-data">
|
||||
<input type="text" name="re[]" value="3"><br />
|
||||
<input type="text" name="re[]" value="1"><br />
|
||||
<input type="text" name="val" value="8"><br />
|
||||
<input type="file" name="myfile1"><br />
|
||||
<input type="file" name="myfile2"><br />
|
||||
<input type="file" name="myfiles[]"><br />
|
||||
<input type="file" name="myfiles[]"><br />
|
||||
<input type="submit" value="tester"><br />
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
Loading…
Add table
Add a link
Reference in a new issue