1
|
|
|
import logging |
2
|
|
|
from flask import Flask, render_template |
3
|
|
|
from docutils.core import publish_parts |
4
|
|
|
|
5
|
|
|
from groundwork.patterns import GwBasePattern |
6
|
|
|
|
7
|
|
|
from groundwork_web.patterns.gw_web_pattern.server import ServerManagerApplication, ServerManagerPlugin |
8
|
|
|
from groundwork_web.patterns.gw_web_pattern.context import ContextManagerApplication, ContextManagerPlugin |
9
|
|
|
from groundwork_web.patterns.gw_web_pattern.route import RouteManagerApplication, RouteManagerPlugin |
10
|
|
|
from groundwork_web.patterns.gw_web_pattern.menu import MenuApplication, MenuPlugin |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class GwWebPattern(GwBasePattern): |
14
|
|
|
|
15
|
|
|
def __init__(self, *args, **kwargs): |
16
|
|
|
super().__init__(*args, **kwargs) |
17
|
|
|
if not hasattr(self.app, "web"): |
18
|
|
|
self.app.web = WebApplication(self.app) |
19
|
|
|
|
20
|
|
|
#: Instance of :class:`~.WebPlugin`. |
21
|
|
|
#: Provides functions to manage web based objects |
22
|
|
|
self.web = WebPlugin(self) |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
class WebPlugin: |
26
|
|
|
def __init__(self, plugin): |
27
|
|
|
self.plugin = plugin |
28
|
|
|
self.app = plugin.app |
29
|
|
|
self.log = plugin.log |
30
|
|
|
|
31
|
|
|
self.servers = ServerManagerPlugin(plugin) |
32
|
|
|
self.contexts = ContextManagerPlugin(plugin) |
33
|
|
|
self.routes = RouteManagerPlugin(plugin) |
34
|
|
|
self.menus = MenuPlugin(plugin) |
35
|
|
|
|
36
|
|
|
self.plugin.signals.connect(receiver="%s_flask_activation" % self.plugin.name, |
37
|
|
|
signal="plugin_activate_pre", |
38
|
|
|
function=self.__init_flask, |
39
|
|
|
description="Initialised flask during plugin activation of %s" % self.plugin.name, |
40
|
|
|
sender=self.plugin) |
41
|
|
|
|
42
|
|
|
@property |
43
|
|
|
def flask(self): |
44
|
|
|
return self.app.web.flask |
45
|
|
|
|
46
|
|
|
@flask.setter |
47
|
|
|
def flask(self, value): |
48
|
|
|
self.app.web.flask = value |
49
|
|
|
|
50
|
|
|
def __init_flask(self, plugin, *args, **kwargs): |
51
|
|
|
self.app.web.init_flask() |
52
|
|
|
|
53
|
|
|
def render(self, template, **kwargs): |
54
|
|
|
return self.app.web.render(template, **kwargs) |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
class WebApplication: |
58
|
|
|
def __init__(self, app): |
59
|
|
|
self.app = app |
60
|
|
|
self.log = logging.getLogger(__name__) |
61
|
|
|
self.flask = None |
62
|
|
|
|
63
|
|
|
self.servers = ServerManagerApplication(app) |
64
|
|
|
self.contexts = ContextManagerApplication(app) |
65
|
|
|
self.routes = RouteManagerApplication(app) |
66
|
|
|
self.menus = MenuApplication(app) |
67
|
|
|
|
68
|
|
|
def init_flask(self): |
69
|
|
|
if self.flask is None: |
70
|
|
|
self.flask = Flask(self.app.name) |
71
|
|
|
|
72
|
|
|
# Inject send_signal() to jinja templates |
73
|
|
|
# Use it like {{ send_signal("my_signal") }} |
74
|
|
|
self.flask.jinja_env.globals.update(send_signal=self.app.signals.send) |
75
|
|
|
|
76
|
|
|
self.flask.jinja_env.globals.update(get_menu=self.__get_menu) |
77
|
|
|
self.flask.jinja_env.globals.update(get_config=self.app.config.get) |
78
|
|
|
self.flask.jinja_env.globals.update(rst2html=self.__rst2html) |
79
|
|
|
|
80
|
|
|
def __get_menu(self, cluster="base"): |
81
|
|
|
return self.menus.get(cluster=cluster) |
82
|
|
|
|
83
|
|
|
def __rst2html(self, document, part="body"): |
84
|
|
|
if document is not None and type(document) == str: |
85
|
|
|
doc_rendered = publish_parts(document, writer_name="html") |
86
|
|
|
if part not in doc_rendered.keys(): |
87
|
|
|
raise KeyError("%s is not a valid key for part parameter of rst2html.\nValid options: " % |
88
|
|
|
(part, ",".join(doc_rendered.keys()))) |
89
|
|
|
return doc_rendered[part] |
90
|
|
|
return document |
91
|
|
|
|
92
|
|
|
def render(self, template, **kwargs): |
93
|
|
|
""" |
94
|
|
|
Renders a template and returns a strings, which represents the rendered data. |
95
|
|
|
|
96
|
|
|
Internally render_template() from flask is used. |
97
|
|
|
|
98
|
|
|
:param template: Name of the template |
99
|
|
|
:param kwargs: Optional key-word arguments, which get passed to the template engine. |
100
|
|
|
:return: Rendered template as string |
101
|
|
|
""" |
102
|
|
|
|
103
|
|
|
return render_template(template, **kwargs) |
104
|
|
|
|
105
|
|
|
|