Project

General

Profile

1 39920 sandro.lab
#!/usr/bin/python
2 40330 sandro.lab
3 39920 sandro.lab
import json
4
5
from flask import Flask, jsonify, request
6
from flask.json import JSONEncoder
7
8
from DLIQueryResolver import QueryResolver
9 40330 sandro.lab
from DLI_ObjectResolver import DLIObject, DLIRelation
10 39920 sandro.lab
from logger import dlilogger
11
12
13
class MyJSONEncoder(JSONEncoder):
14
    def default(self, obj):
15
        if isinstance(obj, DLIObject):
16
            return obj.__dict__
17
        if isinstance(obj, DLIRelation):
18
            return obj.__dict__
19
        return super(MyJSONEncoder, self).default(obj)
20
21
22
app = Flask(__name__)
23
app.debug = False
24
app.json_encoder = MyJSONEncoder
25
26 40330 sandro.lab
host = "http://localhost:8983/solr/DLIF-index-cleaned"
27 40420 sandro.lab
base_dnet_url = "http://localhost:8080/dli"
28 39920 sandro.lab
29
30
# q = QueryResolver("http://localhost:8983/solr/DLIF-index-cleaned_shard1_replica1")
31
32
class InvalidUsage(Exception):
33
    status_code = 400
34
35
    def __init__(self, message, status_code=None, payload=None):
36
        Exception.__init__(self)
37
        self.message = message
38
        if status_code is not None:
39
            self.status_code = status_code
40
        self.payload = payload
41
42
    def to_dict(self):
43
        rv = dict(self.payload or ())
44
        rv['message'] = self.message
45
        return rv
46
47
48
# Routes
49
@app.route('/')
50
def root():
51
    return app.send_static_file('index.html')
52
53
54
@app.route('/<path:path>')
55
def static_proxy(path):
56
    # send_static_file will guess the correct MIME type
57
    return app.send_static_file(path)
58
59
60
@app.route('/api/item/<path:identifier>', methods=['post', 'get'])
61
def getItem(identifier):
62
    if identifier:
63
        q = QueryResolver(host, base_dnet_url)
64
        return jsonify(result=q.get_item(identifier))
65
    else:
66
        raise InvalidUsage('This view is gone', status_code=410)
67
68
69
@app.route('/api/stats/', methods=['post', 'get'])
70
def stats():
71
    q = QueryResolver(host, base_dnet_url)
72
    return jsonify(stats=q.get_stats())
73
74
75
@app.route('/api/ds_info/', methods=['post', 'get'])
76
def info():
77
    q = QueryResolver(host, base_dnet_url)
78
    if 'id' in request.form:
79
        id = request.form['id']
80
        name = request.form['name']
81
        result = q.get_ds_info(id, name)
82
        return jsonify(info=result)
83
    result = q.get_all_ds()
84
    return jsonify(info=result)
85
86
87
@app.route('/api/stats_detail/', methods=['post', 'get'])
88
def stats_detail():
89
    q = QueryResolver(host, base_dnet_url)
90
    return jsonify(stats=q.get_stats_detail())
91
92
93
@app.route('/api/post/', methods=['post', 'get'])
94
def query_post():
95
    action = None
96
    query = None
97
    only_result = False
98
    filter_query = None
99
    start = 0
100
    if 'action' in request.form:
101
        action = request.form['action']
102
    if 'query' in request.form:
103
        query = request.form['query']
104
    if 'only_result' in request.form:
105
        only_result = True
106
107
    if 'browse_field' in request.form:
108
        try:
109
            filter_query = json.loads(request.form['browse_field'])
110
        except:
111
            app.logger.error("Unable to parse browse_field, current value %s" % request.form['browse_field'])
112
113
    if 'start' in request.form:
114
        start = int(request.form['start'])
115
116
    if action == 'query' and query is not None:
117
        q = QueryResolver(host, base_dnet_url)
118
        try:
119 40969 sandro.lab
            result = q.query(q=query, browse_fields=["entitytype", "provenance", "publisher"], start=start, only_result=only_result,
120 40330 sandro.lab
                             filter_query=filter_query)
121 39920 sandro.lab
        except Exception as e:
122
            print e
123
        return jsonify(result=result)
124
    else:
125
        return jsonify(error={'error_code': 'an error occur during query on server'})
126
127
128
if __name__ == '__main__':
129
    app.logger.addHandler(dlilogger)
130
    app.run(debug=False, host='0.0.0.0', processes=5)