1
|
|
|
import logging |
2
|
|
|
|
3
|
|
|
from flask_admin import Admin |
4
|
|
|
|
5
|
|
|
from groundwork_database.patterns import GwSqlPattern |
6
|
|
|
|
7
|
|
|
from groundwork_web.patterns import GwWebPattern |
8
|
|
|
from groundwork_web.patterns.gw_web_db_admin_pattern.exceptions import FlaskNotFoundException |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class GwWebDbAdminPattern(GwWebPattern, GwSqlPattern): |
12
|
|
|
""" |
13
|
|
|
Provides Views to create, read, update and delete database tables content. |
14
|
|
|
Based on `Flask-Admin <https://flask-admin.readthedocs.io/en/latest/>`_ |
15
|
|
|
""" |
16
|
|
|
|
17
|
|
|
def __init__(self, *args, **kwargs): |
18
|
|
|
super().__init__(*args, **kwargs) |
19
|
|
|
if not hasattr(self.app.web, "db"): |
20
|
|
|
self.app.web.db = WebDatabaseApplication(self.app) |
21
|
|
|
|
22
|
|
|
#: Instance of :class:`~.WebDatabasePlugin`. |
23
|
|
|
#: Provides functions to manage web based objects |
24
|
|
|
self.web.db = WebDatabasePlugin(self) |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
class WebDatabasePlugin: |
28
|
|
|
def __init__(self, plugin): |
29
|
|
|
self.plugin = plugin |
30
|
|
|
self.app = plugin.app |
31
|
|
|
self.log = plugin.log |
32
|
|
|
|
33
|
|
|
# Let's register a receiver, which cares about the deactivation process of web_databases for this plugin. |
34
|
|
|
# We do it after the original plugin deactivation, so we can be sure that the registered function is the last |
35
|
|
|
# one which cares about web_databases for this plugin. |
36
|
|
|
self.plugin.signals.connect(receiver="%s_web_db_deactivation" % self.plugin.name, |
37
|
|
|
signal="plugin_deactivate_post", |
38
|
|
|
function=self.__deactivate_web_db, |
39
|
|
|
description="Deactivates web databases for %s" % self.plugin.name, |
40
|
|
|
sender=self.plugin) |
41
|
|
|
self.log.debug("Pattern web database initialised") |
42
|
|
|
|
43
|
|
|
def register(self): |
44
|
|
|
pass |
45
|
|
|
|
46
|
|
|
def unregister(self): |
47
|
|
|
pass |
48
|
|
|
|
49
|
|
|
def get(self): |
50
|
|
|
# REALLY needed? |
51
|
|
|
pass |
52
|
|
|
|
53
|
|
|
def __deactivate_web_db(self, plugin, *args, **kwargs): |
54
|
|
|
pass |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
class WebDatabaseApplication: |
58
|
|
|
def __init__(self, app): |
59
|
|
|
self.app = app |
60
|
|
|
self.log = logging.getLogger(__name__) |
61
|
|
|
|
62
|
|
|
self._flask_provider = None |
63
|
|
|
self._flask_admin = None |
64
|
|
|
|
65
|
|
|
def register(self): |
66
|
|
|
# We must initialise the Flask-Admin class. |
67
|
|
|
# This can not be done during pattern initialisation, because Flask gets loaded and configured during |
68
|
|
|
# activation phase. So during initialisation it is not available. |
69
|
|
|
# |
70
|
|
|
|
71
|
|
|
if self._flask_provider is None: |
72
|
|
|
flask_provider = self.app.web.providers.get("flask") |
73
|
|
|
if flask_provider is None: |
74
|
|
|
raise FlaskNotFoundException("A Flask-Provider must be loaded. Please make sure plugin " |
75
|
|
|
"GwWebFlask got already loaded") |
76
|
|
|
self._flask_provider = flask_provider |
77
|
|
|
self._flask_admin = Admin(self._flask_provider.flask_app, name=self.app.name, template_mode='bootstrap3') |
78
|
|
|
|
79
|
|
|
def unregister(self): |
80
|
|
|
pass |
81
|
|
|
|
82
|
|
|
def get(self): |
83
|
|
|
# REALLY needed? |
84
|
|
|
pass |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
|
88
|
|
|
|
89
|
|
|
|
90
|
|
|
|