1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
# This file is part of rhizi, a collaborative knowledge graph editor.
# Copyright (C) 2014-2015 Rhizi
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Rhizi REST web API:
- make use of rz_kernel for core logic execution
- make use of rz_api_common for common API logic
"""
from flask import current_app
from flask import request
from flask import session
import flask
import logging
from db_op import DBO_match_node_set_by_id_attribute, \
DBO_load_link_set, DBO_match_node_id_set, DBO_diff_commit__topo
from model.graph import Attr_Diff
from model.graph import Topo_Diff
from model.model import Link
from rz_api import rz_mainpage
from rz_api_common import sanitize_input__attr_diff, \
__sanitize_input, sanitize_input__rzdoc_name
from rz_api_common import sanitize_input__topo_diff
from rz_api_common import validate_obj__attr_diff
from rz_kernel import RZDoc_Exception__already_exists
from rz_req_handling import common_resp_handle__success, make_response__json, \
HTTP_STATUS__204_NO_CONTENT, HTTP_STATUS__201_CREATED, \
common_resp_handle__client_error, \
common_rest_req_exception_handler, common_resp_handle__server_error
log = logging.getLogger('rhizi')
class Req_Context():
"""
Request context:
- user_name
- rzdoc
"""
def __init__(self, user_name=None, rzdoc=None):
self.user_name = user_name
self.rzdoc = rzdoc
def __context__common(rzdoc_name=None):
"""
Common request context builder passed along with kernel calls:
- set user_name
- set rzdoc if rzdoc_name argument was supplied
@raise RZDoc_Exception__not_found: if rzdoc_name arg was passed and document was not found
"""
ret = Req_Context()
if session.has_key('username'):
ret.user_name = session['username']
if None != rzdoc_name:
s_rzdoc_name = sanitize_input__rzdoc_name(rzdoc_name)
ret.rzdoc = current_app.kernel.cache_lookup__rzdoc(s_rzdoc_name)
return ret
def __load_node_set_by_id_attr_common(rzdoc_name, id_set):
"""
@param f_k: optional attribute filter key
@param f_vset: possible key values to match against
"""
ctx = __context__common(rzdoc_name)
kernel = flask.current_app.kernel
_, commit_ret = kernel.load_node_set_by_id_attr(id_set, ctx)
return common_resp_handle__success(data=commit_ret)
def diff_commit__set():
"""
commit a diff set
"""
def sanitize_input(req):
diff_set_dict = req.get_json()['diff_set']
topo_diff_dict = diff_set_dict['__diff_set_topo'][0]
topo_diff = Topo_Diff.from_json_dict(topo_diff_dict)
sanitize_input__topo_diff(topo_diff)
return topo_diff;
topo_diff = sanitize_input(request)
op = DBO_diff_commit__topo(topo_diff)
assert False
def load_link_set_by_link_ptr_set():
def deserialize_param_set(param_json):
l_ptr_set_raw = param_json['link_ptr_set']
__sanitize_input(l_ptr_set_raw)
l_ptr_set = []
for lptr_dict in l_ptr_set_raw:
src_id = lptr_dict.get('__src_id')
dst_id = lptr_dict.get('__dst_id')
l_ptr_set += [Link.Link_Ptr(src_id=src_id, dst_id=dst_id) ]
return l_ptr_set
l_ptr_set = deserialize_param_set(request.get_json())
op = DBO_load_link_set.init_from_link_ptr_set(l_ptr_set)
assert False
@common_rest_req_exception_handler
def load_node_set_by_id_attr():
"""
load node-set by ID attribute
@param id_set: list of node ids to match id attribute against
@return: a list of nodes whose id attribute matches 'id' or
an empty list if the requested node is not found
@raise exception: on error
"""
def sanitize_input(req):
req_json = request.get_json()
rzdoc_name = req_json['rzdoc_name']
id_set = req_json['id_set']
return rzdoc_name, id_set
rzdoc_name, id_set = sanitize_input(request)
return __load_node_set_by_id_attr_common(rzdoc_name, id_set)
def match_node_set_by_attr_filter_map(attr_filter_map):
"""
@param attr_filter_map
@return: a set of node DB id's
"""
op = DBO_match_node_id_set(attr_filter_map)
assert False
@common_rest_req_exception_handler
def diff_commit__topo():
"""
REST API wrapper around diff_commit__topo():
- extract topo_diff from request
- handle success/error outcomes
"""
def sanitize_input(req):
rzdoc_name = request.get_json().get('rzdoc_name')
topo_diff_dict = request.get_json()['topo_diff']
topo_diff = Topo_Diff.from_json_dict(topo_diff_dict)
sanitize_input__topo_diff(topo_diff)
return rzdoc_name, topo_diff
rzdoc_name, topo_diff = sanitize_input(request)
if topo_diff.is_empty():
return common_resp_handle__client_error()
ctx = __context__common(rzdoc_name)
kernel = flask.current_app.kernel
_, commit_ret = kernel.diff_commit__topo(topo_diff, ctx)
return common_resp_handle__success(data=commit_ret)
@common_rest_req_exception_handler
def diff_commit__attr():
"""
commit a graph attribute diff
"""
def sanitize_input(req):
rzdoc_name = request.get_json().get('rzdoc_name')
attr_diff_dict = request.get_json()['attr_diff']
attr_diff = Attr_Diff.from_json_dict(attr_diff_dict)
sanitize_input__attr_diff(attr_diff)
return rzdoc_name, attr_diff;
rzdoc_name, attr_diff = sanitize_input(request)
validate_obj__attr_diff(attr_diff)
ctx = __context__common(rzdoc_name)
kernel = flask.current_app.kernel
_, commit_ret = kernel.diff_commit__attr(attr_diff, ctx)
return common_resp_handle__success(data=commit_ret)
def diff_commit__vis():
pass
def rzdoc__via_rz_url(rzdoc_name=None):
return rz_mainpage(rzdoc_name)
@common_rest_req_exception_handler
def rzdoc_clone():
def sanitize_input(req):
rzdoc_name = req.get_json().get('rzdoc_name')
return rzdoc_name
rzdoc_name = sanitize_input(request)
if None == rzdoc_name: # load welcome doc by default
rzdoc_name = current_app.rz_config.rzdoc__mainpage_name
kernel = flask.current_app.kernel
ctx = __context__common(rzdoc_name)
topo_diff = kernel.rzdoc__clone(ctx.rzdoc, ctx)
topo_diff_json = topo_diff.to_json_dict() # serialize Topo_Diff before including in response
commit_log = kernel.rzdoc__commit_log(ctx.rzdoc, limit=10)
return common_resp_handle__success(data=[topo_diff_json, commit_log])
@common_rest_req_exception_handler
def rzdoc__create(rzdoc_name):
"""
create RZ doc
"""
s_rzdoc_name = sanitize_input__rzdoc_name(rzdoc_name)
kernel = flask.current_app.kernel
ctx = __context__common(rzdoc_name=None) # avoid rzdoc cache lookup exception
try:
kernel.rzdoc__create(s_rzdoc_name, ctx)
except RZDoc_Exception__already_exists as e:
return common_resp_handle__server_error(error='rzdoc already exists')
return make_response__json(status=HTTP_STATUS__201_CREATED)
@common_rest_req_exception_handler
def rzdoc__delete(rzdoc_name):
kernel = flask.current_app.kernel
ctx = __context__common(rzdoc_name)
kernel.rzdoc__delete(ctx.rzdoc, ctx)
return make_response__json(status=HTTP_STATUS__204_NO_CONTENT)
@common_rest_req_exception_handler
def rzdoc__search():
def sanitize_input(req):
search_query = req.get_json().get('search_query')
if search_query is None or \
len(search_query) > current_app.rz_config.rzdoc__name__max_length:
raise Exception('malformed rzdoc search query')
return search_query
search_query = sanitize_input(request)
kernel = flask.current_app.kernel
ctx = __context__common(rzdoc_name=None) # avoid rzdoc cache lookup exception
rzdoc_set = kernel.rzdoc__search(search_query, ctx)
ret = [rzdoc_dict['name'] for rzdoc_dict in rzdoc_set]
return common_resp_handle__success(data=ret)
|