summaryrefslogtreecommitdiff
path: root/src/server/rz_api_rest.py
blob: b3d5dc40451be12007e47c9351d15e036e54a299 (plain)
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
"""
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 Flask
from flask import escape
from flask import jsonify
from flask import make_response
from flask import redirect
from flask import render_template
from flask import request
from flask import send_from_directory
from flask import session
from flask import url_for
import flask
import logging
import traceback

from model.graph import Attr_Diff
from model.graph import Topo_Diff
from rz_api_common import sanitize_input__attr_diff
from rz_api_common import sanitize_input__topo_diff
from rz_api_common import validate_obj__attr_diff
from rz_req_handling import common_resp_handle


log = logging.getLogger('rhizi')

db_ctl = None  # injected: DB controller

def __context__common(rzdoc_id=None):
    """
    build a common rquest context to pass along with a kernel diff commit:
       - set user_name
    """

    ret = {}
    if session.has_key('username'):
        ret['user_name'] = session['username']

    ret['rzdoc_id'] = rzdoc_id
    return ret

def rzdoc__new():
    # TODO: add doc node, set doc label, associate user-doc
    
    def sanitize_input(req):
        return request.get_json().get('rzdoc_name')

    ctx = __context__common()
    rzdoc_name = sanitize_input(request)
    try:
        kernel = flask.current_app.kernel
        rzdoc = kernel.rzdoc__new(rzdoc_name, ctx)
        return common_resp_handle(data=rzdoc)
    except Exception as e:
        log.error(e.message)
        log.error(traceback.print_exc())
        return common_resp_handle(error=e)

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

    try:
        rzdoc_name, topo_diff = sanitize_input(request)
    except Exception as e:
        return common_resp_handle(error='malformed input')

    rzdoc_id = map_rzdoc_name_to_rzdoc_id(rzdoc_name)
    ctx = __context__common(rzdoc_id=rzdoc_id)
    try:
        kernel = flask.current_app.kernel
        _, commit_ret = kernel.diff_commit__topo(topo_diff, ctx)
        return common_resp_handle(data=commit_ret)
    except Exception as e:
        log.error(e.message)
        log.error(traceback.print_exc())
        return common_resp_handle(error=e)

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;

    def on_error(e):
        # handle DB ERRORS, eg. name attr change error
        return common_resp_handle(error='error occurred')

    try:
        rzdoc_name, attr_diff = sanitize_input(request)
        validate_obj__attr_diff(attr_diff)
    except Exception as e:
        return common_resp_handle(error='malformed input')

    rzdoc_id = map_rzdoc_name_to_rzdoc_id(rzdoc_name)
    ctx = __context__common(rzdoc_id=rzdoc_id)
    try:
        kernel = flask.current_app.kernel
        _, commit_ret = kernel.diff_commit__attr(attr_diff, ctx)
        return common_resp_handle(data=commit_ret)
    except Exception as e:
        log.error(e.message)
        log.error(traceback.print_exc())
        return common_resp_handle(error=e)

def diff_commit__vis():
    pass