1
|
|
|
import logging |
2
|
|
|
|
3
|
|
|
from flask_admin import Admin |
4
|
|
|
from flask_admin.contrib.sqla import ModelView |
5
|
|
|
|
6
|
|
|
from groundwork_database.patterns import GwSqlPattern |
7
|
|
|
|
8
|
|
|
from groundwork_web.patterns import GwWebPattern |
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(GwWebDbAdminPattern, self).__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
|
|
View Code Duplication |
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, db_clazz, db_session): |
44
|
|
|
self.app.web.db.register(db_clazz, db_session) |
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
|
|
|
self.flask_admin = None |
62
|
|
|
|
63
|
|
|
def register(self, db_clazz, db_session): |
64
|
|
|
""" |
65
|
|
|
Adds a new class-based view to the flask-admin instance. |
66
|
|
|
|
67
|
|
|
:param db_clazz: SQLAlchemy class object |
68
|
|
|
:param db_session: session object |
69
|
|
|
:return: Name of the endpoint, which can be used to generate urls for it. |
70
|
|
|
""" |
71
|
|
|
# We must initialise the Flask-Admin class. |
72
|
|
|
# This can not be done during pattern initialisation, because Flask gets loaded and configured during |
73
|
|
|
# activation phase. So during initialisation it is not available. |
74
|
|
|
|
75
|
|
|
if self.flask_admin is None: |
76
|
|
|
self.flask_admin = Admin(self.app.web.flask, |
77
|
|
|
name=self.app.name, |
78
|
|
|
base_template="master.html", |
79
|
|
|
template_mode='bootstrap3') |
80
|
|
|
|
81
|
|
|
endpoint = "admin_%s" % db_clazz.__name__.lower() |
82
|
|
|
self.flask_admin.add_view(ModelView(db_clazz, db_session, endpoint=endpoint)) |
83
|
|
|
return endpoint |
84
|
|
|
|
85
|
|
|
def unregister(self): |
86
|
|
|
pass |
87
|
|
|
|
88
|
|
|
def get(self): |
89
|
|
|
# REALLY needed? |
90
|
|
|
pass |
91
|
|
|
|