1
|
|
|
import logging |
2
|
|
|
|
3
|
|
|
from flask_restless import APIManager |
4
|
|
|
|
5
|
|
|
from groundwork_database.patterns import GwSqlPattern |
6
|
|
|
|
7
|
|
|
from groundwork_web.patterns import GwWebPattern |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class GwWebDbRestPattern(GwWebPattern, GwSqlPattern): |
11
|
|
|
""" |
12
|
|
|
Provides REST APIs for database tables. |
13
|
|
|
Based on `Flask-Restless <https://flask-restless.readthedocs.io/en/stable/>`_ |
14
|
|
|
""" |
15
|
|
|
|
16
|
|
|
def __init__(self, *args, **kwargs): |
17
|
|
|
super().__init__(*args, **kwargs) |
18
|
|
|
if not hasattr(self.app.web, "rest"): |
19
|
|
|
self.app.web.rest = WebRestApplication(self.app) |
20
|
|
|
|
21
|
|
|
#: Instance of :class:`~.WebRestPlugin`. |
22
|
|
|
#: Provides functions to manage web based objects |
23
|
|
|
self.web.rest = WebRestPlugin(self) |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
class WebRestPlugin: |
27
|
|
|
def __init__(self, plugin): |
28
|
|
|
self.plugin = plugin |
29
|
|
|
self.app = plugin.app |
30
|
|
|
self.log = plugin.log |
31
|
|
|
|
32
|
|
|
# Let's register a receiver, which cares about the deactivation process of web_databases for this plugin. |
33
|
|
|
# We do it after the original plugin deactivation, so we can be sure that the registered function is the last |
34
|
|
|
# one which cares about web_databases for this plugin. |
35
|
|
|
self.plugin.signals.connect(receiver="%s_web_rest_deactivation" % self.plugin.name, |
36
|
|
|
signal="plugin_deactivate_post", |
37
|
|
|
function=self.__deactivate_web_rest, |
38
|
|
|
description="Deactivates web rest apis for %s" % self.plugin.name, |
39
|
|
|
sender=self.plugin) |
40
|
|
|
self.log.debug("Pattern web rest initialised") |
41
|
|
|
|
42
|
|
|
def register(self, db_clazz, db_session): |
43
|
|
|
self.app.web.rest.register(db_clazz, db_session) |
44
|
|
|
|
45
|
|
|
def unregister(self): |
46
|
|
|
pass |
47
|
|
|
|
48
|
|
|
def __deactivate_web_rest(self, plugin, *args, **kwargs): |
49
|
|
|
pass |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
class WebRestApplication: |
53
|
|
|
def __init__(self, app): |
54
|
|
|
self.app = app |
55
|
|
|
self.log = logging.getLogger(__name__) |
56
|
|
|
self.flask_restless = None |
57
|
|
|
|
58
|
|
|
def register(self, db_clazz, db_session, methods=None): |
59
|
|
|
""" |
60
|
|
|
Adds a new class-based view to the flask-admin instance. |
61
|
|
|
|
62
|
|
|
:param db_clazz: SQLAlchemy class object |
63
|
|
|
:param db_session: session object |
64
|
|
|
:return: Name of the endpoint, which can be used to generate urls for it. |
65
|
|
|
""" |
66
|
|
|
# We must initialise the Flask-restless 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
|
|
|
if self.flask_restless is None: |
70
|
|
|
self.flask_restless = APIManager(self.app.web.flask, session=db_session) |
71
|
|
|
|
72
|
|
|
if methods is None: |
73
|
|
|
methods = ["GET", "POST, DELETE"] |
74
|
|
|
self.flask_restless.create_api(db_clazz, methods=methods) |
75
|
|
|
|
76
|
|
|
def unregister(self): |
77
|
|
|
pass |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
|
81
|
|
|
|
82
|
|
|
|