Project

General

Profile

« Previous | Next » 

Revision 56023

implemented new portal with fastapi

View differences:

modules/dli-service-portal/branches/3.0/DLI.py
1
#!/usr/bin/python
2

  
3
from flask import Flask, jsonify, request
4
from flask.json import JSONEncoder
5
from eu.dnetlib.es_connector import DLIESConnector
6
from flask_prometheus import monitor
7

  
8
from logger import dlilogger
9

  
10
DLI_INDEX = None
11
ES_HOST = None
12

  
13

  
14
def read_properies():
15
    f = open('api.properties')
16
    for line in f:
17
        s = line.strip().split('=')
18
        if s[0] == 'api.index':
19
            DLI_INDEX = s[1]
20
        if s[1] == 'es_index':
21
            ES_HOST = [x.strip() for x in s[1].split(',')]
22

  
23

  
24
class MyJSONEncoder(JSONEncoder):
25
    def default(self, obj):
26
        return obj.__dict__
27

  
28

  
29
app = Flask(__name__)
30
app.debug = False
31
app.json_encoder = MyJSONEncoder
32

  
33
base_dnet_url = "http://aggregator-dli.openaire.eu/dli/"
34

  
35
pid_resolver = {
36
    "pdb": "http://www.rcsb.org/pdb/explore/explore.do?structureId=%s",
37
    "ncbi-n": "http://www.ncbi.nlm.nih.gov/gquery/?term=%s",
38
    "pmid": "http://www.ncbi.nlm.nih.gov/pubmed/%s",
39
    "pmcid": "http://www.ncbi.nlm.nih.gov/pmc/articles/%s",
40
    "pubmedid": "http://www.ncbi.nlm.nih.gov/pubmed/%s",
41
    "doi": "http://dx.doi.org/%s",
42
    "genbank": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
43
    "nuccore": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
44
    "swiss-prot": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
45
    "arrayexpress": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
46
    "biomodels": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
47
    "bmrb": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
48
    "ena": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
49
    "geo": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
50
    "ensembl": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
51
    "mgi": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
52
    "bind": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
53
    "pride": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
54
    "ddbj": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
55
    "bioproject": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
56
    "embl": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
57
    "sra": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
58
}
59

  
60

  
61
class InvalidUsage(Exception):
62
    status_code = 400
63

  
64
    def __init__(self, message, status_code=None, payload=None):
65
        Exception.__init__(self)
66
        self.message = message
67
        if status_code is not None:
68
            self.status_code = status_code
69
        self.payload = payload
70

  
71
    def to_dict(self):
72
        rv = dict(self.payload or ())
73
        rv['message'] = self.message
74
        return rv
75

  
76

  
77
# Routes
78
@app.route('/')
79
def root():
80
    return app.send_static_file('index.html')
81

  
82

  
83
@app.route('/<path:path>')
84
def static_proxy(path):
85
    # send_static_file will guess the correct MIME type
86
    return app.send_static_file(path)
87

  
88

  
89
@app.route('/api/item/<path:identifier>', methods=['post', 'get'])
90
def get_item(identifier):
91
    if identifier:
92
        object_type = request.form.get('type')
93
        start = request.form.get('from')
94
        if object_type and len(object_type) == 0:
95
            object_type = None
96
        if start and len(start) == 0:
97
            start = None
98
        elif start:
99
            start = int(start)
100
        connector = DLIESConnector()
101
        return jsonify(result=connector.item_by_id(identifier, object_type, start))
102
    else:
103
        raise InvalidUsage('This view is gone', status_code=410)
104

  
105

  
106
@app.route('/api/main_page_stats', methods=['get'])
107
def main_page_stats():
108
    connector= DLIESConnector()
109
    return jsonify(result=connector.get_main_page_stats())
110

  
111
@app.route('/api/stats/', methods=['post', 'get'])
112
def stats():
113
    raise Exception("Method to be implemented")
114
    # q = QueryResolver(host, base_dnet_url)
115
    # return jsonify(stats=q.get_stats())
116

  
117

  
118
@app.route('/api/ds_info/', methods=['post', 'get'])
119
def info():
120
    raise Exception("Method to be implemented")
121

  
122

  
123
@app.route('/api/stats_detail/', methods=['post', 'get'])
124
def stats_detail():
125
    return ""
126

  
127

  
128
@app.route('/api/resolveId', methods=['get'])
129
def resolveIdentifier():
130
    pid = request.form.get('pid')
131
    pid_type = request.form.get('pid_type')
132
    if pid_type!= None:
133
        if pid_type.lower() in  pid_resolver:
134
            return pid_resolver[pid_type.lower()] % pid
135
        elif pid_type.lower().strip() == 'url':
136
            return pid
137
        else:
138
            return "http://identifiers.org/%s:%s" % (pid_type, pid)
139
    return ""
140

  
141

  
142

  
143
@app.route('/api/queryPid/', methods=['post'])
144
def query_pid():
145
    if 'pid' in request.form:
146
        print "pid Request for %s "%request.form['pid']
147
    connector = DLIESConnector()
148
    result = connector.query_by_id(request.form['pid'])
149
    return jsonify(result=result)
150

  
151

  
152
@app.route('/api/post/', methods=['post', 'get'])
153
def query_post():
154
    action = None
155
    query = None
156
    filter_key = None
157
    result = {}
158

  
159
    start = 0
160
    if 'action' in request.form:
161
        action = request.form['action']
162
    if 'query' in request.form:
163
        query = request.form['query']
164
    if 'start' in request.form:
165
        start = 0
166
        try:
167
            start = int(request.form.get('start',0))
168
        except:
169
            pass
170
    if 'filter' in request.form:
171
        filter_key = request.form['filter']
172

  
173
    if action == 'query' and query is not None:
174
        connector = DLIESConnector()
175
        try:
176
            result = connector.simple_query(query, start=start, user_filter=filter_key)
177
        except Exception as e:
178
            print e
179
        return jsonify(result=result)
180
    else:
181
        return jsonify(error={'error_code': 'an error occur during query on server'})
182

  
183

  
184
if __name__ == '__main__':
185

  
186
    app.logger.addHandler(dlilogger)
187
    # monitor(app, port=8000)
188
    # app.run()
189
    app.run(debug=True)
modules/dli-service-portal/branches/3.0/static/partials/query.html
141 141
                          style="font-size: 100%"> {{ date }}</span>
142 142
                </div>
143 143

  
144
                <div class="col-lg-12" style="margin-top: 10px">
144
                <div class="col-lg-12" style="margin-top: 10px">                
145 145
                    <a href="#/detail/{{ result.id }}">
146
                        <b ng-show="result.title">{{ result.title[0] }}</b>
147
                        <b ng-hide="result.title">Metadata non resolved for pid : {{ result.localIdentifier[0].id }}</b>
146
                        <b ng-show="result.title.length">{{ result.title[0] }}</b>
147
                        <b ng-hide="result.title.length">Metadata non resolved for pid : {{ result.localIdentifier[0].id }}</b>
148 148
                    </a>
149 149
                </div>
150 150

  
modules/dli-service-portal/branches/3.0/static/partials/index.html
14 14
            <div class="container centered">
15 15
                <div class="row">
16 16
                    <div class="col-lg-12 centered">
17
                        <img ng-hide="facet"  img class="img-responsive img-centered"  style="max-width:400px" src="img/logo_big.png" alt="">
17
                        <img ng-hide="facet"  img class="img-responsive img-centered"  style="max-width:400px" src="static/img/logo_big.png" alt="">
18 18
                        <div class="row">
19 19
                            <div class="form-group col-lg-12 ">
20 20
                                <form ng-submit="makeQuery()">
modules/dli-service-portal/branches/3.0/static/partials/api.html
30 30
                    and (iii) a corresponding XML and JSON schema.
31 31
                </div>
32 32
                <div class="row" style="text-align: center;">
33
                    <img src="img/ScholixDM.png" style="max-width: 80%;">
33
                    <img src="static/img/ScholixDM.png" style="max-width: 80%;">
34 34
                </div>
35 35
                <div class="row" style="text-align: justify; text-justify: inter-word;  margin-top: 10px">
36 36
                    <b>How to expose Scholix data from this hub</b> Scholexplorer harvests scholarly links
modules/dli-service-portal/branches/3.0/static/angular/mainPageController.js
61 61
    };
62 62

  
63 63
    $scope.makeQuery= function () {
64
        if ($scope.query === undefined || $scope.query === null)
65
            $scope.query = '*'
64 66
        $location.path("/query/q=" + $scope.query);
65 67
    };
66 68

  
modules/dli-service-portal/branches/3.0/static/angular/service_v10.js
9 9
    var factory = {}
10 10
    factory.query = function (query, page, filter, value) {
11 11
        $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8";
12
        if (page === null){
13
            return $http.post('/api/post/', $.param({'action': "query", 'query': query, 'filter':filter, 'value':value}));
12
        if (page === undefined || page ==null){
13
            return $http.post('/api/post/', $.param({'action': "query", 'query': query, 'filter':filter, start:0}));
14 14
        }
15 15
        else {
16 16
            return $http.post('/api/post/', $.param({'action': "query", 'query': query, 'start': (page - 1) * 10, 'filter':filter, 'value':value}));
......
25 25

  
26 26
    factory.getStatsDetail = function () {
27 27
        //$http.defaults.headers.get["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8";
28
        return $http.get('currentStats.json');
28
        return $http.get('static/currentStats.json');
29 29
    };
30 30

  
31 31
    factory.getMainPageStats = function () {
......
35 35

  
36 36
    factory.getStats = function () {
37 37
        //$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8";
38
        return $http.get('stats2.json');
38
        return $http.get('static/stats2.json');
39 39
    };
40 40

  
41 41
    factory.getDatasources = function() {
......
51 51

  
52 52
    factory.getItem = function (identifier, type, from) {
53 53
        $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8";
54
        return $http.post('/api/item/' + identifier, $.param({'type': type, 'from': from}));
54
        return $http.post('/api/item/' + identifier, $.param({'object_type': type, 'start': from}));
55 55
    };
56 56

  
57 57
    factory.getDsInfo = function (identifier, name) {
modules/dli-service-portal/branches/3.0/static/angular/dliApplication.js
5 5
dliApp.config(function ($routeProvider) {
6 6
    $routeProvider.
7 7
        when('/', {
8
            templateUrl: 'partials/index.html',
8
            templateUrl: 'static/partials/index.html',
9 9
            controller: 'mainPageController'
10 10
        })
11 11
        .when('/query/q=:q*', {
12
            templateUrl: 'partials/query.html',
12
            templateUrl: 'static/partials/query.html',
13 13
            controller: 'queryController'
14 14
        })
15 15
        .when('/query/page=:page/q=:q*', {
16
            templateUrl: 'partials/query.html',
16
            templateUrl: 'static/partials/query.html',
17 17
            controller: 'queryController'
18 18
        })
19 19
        .when('/query/:f/q=:q*', {
20
            templateUrl: 'partials/query.html',
20
            templateUrl: 'static/partials/query.html',
21 21
            controller: 'queryController'
22 22
        })
23 23
        .when('/query/:f/page=:page/q=:q*', {
24
            templateUrl: 'partials/query.html',
24
            templateUrl: 'static/partials/query.html',
25 25
            controller: 'queryController'
26 26
        })
27 27
        .when('/detail/:id*', {
28
            templateUrl: 'partials/detail.html',
28
            templateUrl: 'static/partials/detail.html',
29 29
            controller: 'detailController'
30 30
        }).when('/datasources', {
31
            templateUrl: 'partials/datasources.html',
31
            templateUrl: 'static/partials/datasources.html',
32 32
            controller: 'datasourcesController'
33 33
        }).when('/datasource/id/:id', {
34
            templateUrl: 'partials/datasourceDetail.html',
34
            templateUrl: 'static/partials/datasourceDetail.html',
35 35
            controller: 'datasourceDetailController'
36 36
        }).when('/datasource/name/:name', {
37
            templateUrl: 'partials/datasourceDetail.html',
37
            templateUrl: 'static/partials/datasourceDetail.html',
38 38
            controller: 'datasourceDetailController'
39 39
        }).when('/api', {
40
            templateUrl: 'partials/api.html',
40
            templateUrl: 'static/partials/api.html',
41 41
            controller: 'mainPageController'
42 42
        }).when('/about', {
43
            templateUrl: 'partials/about.html',
43
            templateUrl: 'static/partials/about.html',
44 44
            controller: 'mainPageController'
45 45
        }).when('/contact', {
46
            templateUrl: 'partials/contactus.html',
46
            templateUrl: 'static/partials/contactus.html',
47 47
            controller: 'mainPageController'
48 48
        }).when('/statistics', {
49
            templateUrl: 'partials/statistics.html',
49
            templateUrl: 'static/partials/statistics.html',
50 50
            controller: 'statsController'
51 51
        })
52 52

  
modules/dli-service-portal/branches/3.0/static/index.html
10 10
    <meta name="author" content="">
11 11

  
12 12
    <title>ScholeXplorer - The Data Literature Interlinking Service</title>
13
    <link rel="icon" type="image/png" href="favicon.png">
13
    <link rel="icon" type="static/image/png" href="static/favicon.png">
14 14

  
15 15
    <!-- Bootstrap Core CSS - Uses Bootswatch Flatly Theme: http://bootswatch.com/flatly/ -->
16
    <link href="css/bootstrap.min.css" rel="stylesheet">
16
    <link href="static/css/bootstrap.min.css" rel="stylesheet">
17 17

  
18
    <link href="css/jquery-jvectormap-2.0.3.css" rel="stylesheet">
18
    <link href="static/css/jquery-jvectormap-2.0.3.css" rel="stylesheet">
19 19

  
20 20
    <!-- Custom CSS -->
21
    <link href="css/freelancer.css" rel="stylesheet">
22
    <link href="bower/c3/c3.css" rel="stylesheet" type="text/css">
21
    <link href="static/css/freelancer.css" rel="stylesheet">
22
    <link href="static/bower/c3/c3.css" rel="stylesheet" type="text/css">
23 23

  
24 24
    <!-- Custom Fonts -->
25
    <link href="font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
25
    <link href="static/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
26 26

  
27
    <link rel="stylesheet" type="text/css" href="bower_components/jssocials/dist/jssocials.css" />
28
    <link rel="stylesheet" type="text/css" href="bower_components/jssocials/dist/jssocials-theme-classic.css" />
27
    <link rel="stylesheet" type="text/css" href="static/bower_components/jssocials/dist/jssocials.css" />
28
    <link rel="stylesheet" type="text/css" href="static/bower_components/jssocials/dist/jssocials-theme-classic.css" />
29 29

  
30 30

  
31 31

  
......
90 90
                <span class="icon-bar"></span>
91 91
                <span class="icon-bar"></span>
92 92
            </button>
93
            <a class="pull-left" href="index.html#/"><img src="img/logo_wt.png" alt="ScholeXplorer"/></a>
93
            <a class="pull-left" href="#/"><img src="static/img/logo_wt.png" alt="ScholeXplorer"/></a>
94 94
        </div>
95 95

  
96 96
        <!-- Collect the nav links, forms, and other content for toggling -->
......
143 143

  
144 144
                <div class="footer-col col-md-12">
145 145
                    <a class="white-text" href="http://www.openaire.eu"><img style="height: 40px; padding-left: 8px"
146
                                                                             src="images/OpenAIREplus_logo.png"></a>
146
                                                                             src="static/images/OpenAIREplus_logo.png"></a>
147 147
                    <a class="white-text" href="https://rd-alliance.org/groups/rdawds-publishing-data-ig.html"><img
148
                            style="height: 40px; padding-left: 8px" src="images/rda_logo.png"></a>
148
                            style="height: 40px; padding-left: 8px" src="static/images/rda_logo.png"></a>
149 149
                    <a class="white-text" href="http://www.icsu-wds.org"><img style="height: 40px; padding-left: 8px"
150
                                                                              src="images/wds_logo.png"></a>
150
                                                                              src="static/images/wds_logo.png"></a>
151 151
                </div>
152 152
            </div>
153 153
        </div>
......
180 180

  
181 181

  
182 182
<!-- jQuery -->
183
<script src="js/jquery.js"></script>
183
<script src="static/js/jquery.js"></script>
184 184

  
185 185
<!-- Bootstrap Core JavaScript -->
186
<script src="js/bootstrap.min.js"></script>
186
<script src="static/js/bootstrap.min.js"></script>
187 187

  
188 188
<!-- Plugin JavaScript -->
189 189
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.3/jquery.easing.min.js"></script>
190
<script src="js/classie.js"></script>
191
<script src="js/cbpAnimatedHeader.js"></script>
190
<script src="static/js/classie.js"></script>
191
<script src="static/js/cbpAnimatedHeader.js"></script>
192 192

  
193 193

  
194 194
<!-- Custom Theme JavaScript -->
195
<script src="js/freelancer.js"></script>
195
<script src="static/js/freelancer.js"></script>
196 196

  
197 197

  
198
<script src="js/spin.js"></script>
198
<script src="static/js/spin.js"></script>
199 199

  
200
<script src="bower/angular/angular.min.js"></script>
201
<script src="bower/angular-route/angular-route.min.js"></script>
202
<script src="angular/dliApplication.js"></script>
203
<script src="angular/service_v10.js"></script>
200
<script src="static/bower/angular/angular.min.js"></script>
201
<script src="static/bower/angular-route/angular-route.min.js"></script>
202
<script src="static/angular/dliApplication.js"></script>
203
<script src="static/angular/service_v10.js"></script>
204 204

  
205
<script src="angular/datasourceController.js"></script>
206
<script src="angular/mainPageController.js"></script>
207
<script src="angular/queryController.js"></script>
208
<script src="angular/statisticController.js"></script>
205
<script src="static/angular/datasourceController.js"></script>
206
<script src="static/angular/mainPageController.js"></script>
207
<script src="static/angular/queryController.js"></script>
208
<script src="static/angular/statisticController.js"></script>
209 209

  
210 210

  
211
<script src="bower/d3/d3.min.js"></script>
212
<script src="bower/c3/c3.min.js"></script>
213
<script src="js/jquery-jvectormap-2.0.3.min.js"></script>
214
<script src="js/jquery-jvectormap-world-mill.js"></script>
211
<script src="static/bower/d3/d3.min.js"></script>
212
<script src="static/bower/c3/c3.min.js"></script>
213
<script src="static/js/jquery-jvectormap-2.0.3.min.js"></script>
214
<script src="static/js/jquery-jvectormap-world-mill.js"></script>
215 215

  
216 216
<script>
217 217
    (function (i, s, o, g, r, a, m) {
......
233 233
</script>
234 234

  
235 235

  
236
<script src="bower_components/jssocials/dist/jssocials.min.js"></script>
236
<script src="static/bower_components/jssocials/dist/jssocials.min.js"></script>
237 237

  
238 238
<script>
239 239

  
modules/dli-service-portal/branches/3.0/eu/dnetlib/util.py
1
import logging
2
import os
3

  
4
def get_index_properties():
5
    if 'DLI_CONF_PATH' not in os.environ:
6
        logging.error("ENVIRONEMENT VARIABLE DLI_CONF_PATH DOES NOT EXISTS ")
7
        raise Exception("ENVIRONEMENT VARIABLE DLI_CONF_PATH DOES NOT EXISTS ")
8

  
9
    if not os.path.exists(os.environ['DLI_CONF_PATH']):
10
        logging.error("FILE {} DOES NOT EXISTS ".format(os.environ['DLI_CONF_PATH']))
11
        raise Exception("FILE {} DOES NOT EXISTS ".format(os.environ['DLI_CONF_PATH']))
12

  
13

  
14
    with open(os.environ['DLI_CONF_PATH']) as f:
15
        p = {}
16
        for line in f:
17
            if not line.startswith("#"):
18
                data = line.strip().split("=")
19
                p[data[0].strip()] = data[1].strip()
20
        return p
21
        
modules/dli-service-portal/branches/3.0/eu/dnetlib/es_connector.py
1 1
from json import JSONEncoder
2

  
3 2
import sys
4

  
5 3
import re
6 4
from elasticsearch import Elasticsearch
7 5
from elasticsearch_dsl import *
6
import logging
7
from eu.dnetlib.util import get_index_properties
8 8

  
9 9
import os
10 10
from os import path
11 11

  
12

  
13
log = logging.getLogger("scholexplorer-portal")
14

  
12 15
pid_resolver = {
13 16
    "pdb": "http://www.rcsb.org/pdb/explore/explore.do?structureId=%s",
14 17
    "ncbi-n": "http://www.ncbi.nlm.nih.gov/gquery/?term=%s",
......
39 42
    if pid_type != None:
40 43
        regex = r"\b(10[.][0-9]{4,}(?:[.][0-9]+)*/(?:(?![\"&\'<>])\S)+)\b"
41 44
        if re.match(regex,pid):
42
            print "It should be doi"
45
            log.debug("It should be doi")
43 46
            pid_type = 'doi'
44 47
        if pid_type.lower() in pid_resolver:
45 48
            return pid_resolver[pid_type.lower()] % pid
......
53 56
    return ""
54 57

  
55 58

  
56
def get_property():
57
    f = open(path.join(os.path.dirname(os.path.realpath(__file__)), '../../api.properties'))
58
    p = {}
59
    for line in f:
60
        data = line.strip().split("=")
61
        p[data[0].strip()] = data[1].strip()
62
    return p
63 59

  
64 60

  
61

  
65 62
def create_typology_filter(value):
66 63
    return Q('match', typology=value)
67 64

  
......
101 98

  
102 99
class DLIESConnector(object):
103 100
    def __init__(self):
104
        props = get_property()
101
        props = get_index_properties()
105 102
        self.index_host = [x.strip() for x in props['es_index'].split(',')]
106 103
        self.client = Elasticsearch(hosts=self.index_host)
107 104
        self.index_name = props['api.index']
108 105

  
109 106
    def get_main_page_stats(self):
110
        stats = dict(total =Search(using=self.client, index=self.index_name).doc_type('scholix').execute().hits.total/2)
107
        stats = dict(total =int(Search(using=self.client, index=self.index_name).doc_type('scholix').count()/2))
111 108
        for item in ['dataset', 'publication']:
112 109
            s= Search(using=self.client, index=self.index_name).doc_type('object').query(Q('match', typology=item))
113
            stats[item] = s.execute().hits.total
110
            stats[item] = s.count()
114 111
        return stats
115 112

  
116 113
    def query_by_id(self, id):
......
164 161

  
165 162
    def simple_query(self, textual_query, start=None, end=None, user_filter=None):
166 163
        s = Search(using=self.client, index=self.index_name).doc_type('object')
167
        q = Q('match', _all=textual_query)
164

  
165
        if not textual_query  == '*':
166
            q = Q('match', _all=textual_query)
167
        else:
168
            q = Q()
168 169
        s.aggs.bucket('typologies', 'terms', field='typology')
169 170
        s.aggs.bucket('all_datasources', 'nested', path='datasources').bucket('all_names', 'terms',
170 171
                                                                              field='datasources.datasourceName')
......
323 324

  
324 325
            return DLIESResponse(total=1, hits=hits)
325 326
        except Exception as e:
326
            print "Error on getting item "
327
            print e
328
            print "on line %i" % sys.exc_traceback.tb_lineno
327
            log.error("Error on getting item ")
328
            log.error(e)
329
            log.error("on line %i" % sys.exc_traceback.tb_lineno)
329 330
            return DLIESResponse()
modules/dli-service-portal/branches/3.0/main.py
1
import os
2
from fastapi import FastAPI, Form
3
import logging
4
from starlette.staticfiles import StaticFiles
5
from starlette.responses import FileResponse
6
from eu.dnetlib.es_connector import DLIESConnector
7
import sys
8

  
9
_CURDIR = os.path.dirname(os.path.abspath(__file__))
10

  
11
app = FastAPI()
12

  
13
app.mount('/static', StaticFiles(directory=os.path.join(_CURDIR, 'static' )))
14
base_dnet_url = "http://aggregator-dli.openaire.eu/dli/"
15
pid_resolver = {
16
    "pdb": "http://www.rcsb.org/pdb/explore/explore.do?structureId=%s",
17
    "ncbi-n": "http://www.ncbi.nlm.nih.gov/gquery/?term=%s",
18
    "pmid": "http://www.ncbi.nlm.nih.gov/pubmed/%s",
19
    "pmcid": "http://www.ncbi.nlm.nih.gov/pmc/articles/%s",
20
    "pubmedid": "http://www.ncbi.nlm.nih.gov/pubmed/%s",
21
    "doi": "http://dx.doi.org/%s",
22
    "genbank": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
23
    "nuccore": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
24
    "swiss-prot": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
25
    "arrayexpress": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
26
    "biomodels": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
27
    "bmrb": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
28
    "ena": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
29
    "geo": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
30
    "ensembl": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
31
    "mgi": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
32
    "bind": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
33
    "pride": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
34
    "ddbj": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
35
    "bioproject": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
36
    "embl": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
37
    "sra": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
38
}
39

  
40
log = logging.getLogger("scholexplorer-portal")
41
log.setLevel(logging.INFO)
42
fh = logging.StreamHandler(sys.stdout) 
43
fh.setLevel(logging.INFO)
44
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
45
fh.setFormatter(formatter)
46
log.addHandler(fh)
47
log.info("Scholexplorer Portal Restarted")
48

  
49
@app.get('/')
50
def root():
51
    return FileResponse(os.path.join(os.path.join(_CURDIR, 'static' ),'index.html'))
52

  
53
@app.get("/favicon.ico")
54
def favicon():
55
    return FileResponse(os.path.join(os.path.join(_CURDIR, 'static' ),'favicon.ico'))
56

  
57
@app.get("/api/main_page_stats")
58
def main_page_stats():
59
    connector= DLIESConnector()
60
    return dict(result=connector.get_main_page_stats())
61

  
62
@app.post('/api/queryPid/')
63
def query_pid(*, pid:str= Form(...)):   
64
    print (pid) 
65
    connector = DLIESConnector()    
66
    return dict(result=connector.query_by_id(pid))
67

  
68

  
69

  
70
@app.post('/api/post/')
71
def query_post(*, start:int= Form(default=0), action:str=Form(default="query"), query:str=Form(...),filter:str=Form(default=None)):
72
    filter_key = None
73
    result = {}  
74
    if filter:
75
        filter_key = filter
76
    if action == 'query' and query is not None:
77
        connector = DLIESConnector()
78
        try:
79
            result = connector.simple_query(query, start=start, user_filter=filter_key)
80
        except Exception as e:
81
            log.error(e)
82
        return dict(result=result)
83
    else:
84
        return dict(error={'error_code': 'an error occur during query on server'})
85

  
86

  
87
@app.get('/api/item/{identifier}')
88
@app.post('/api/item/{identifier}')
89
def get_item(identifier:str, object_type:str=Form(default=None), start:int=Form(default=0)):
90
    if identifier:        
91
        connector = DLIESConnector()
92
        return dict(result=connector.item_by_id(identifier, object_type, start))
93
    else:
94
        return None

Also available in: Unified diff