/
server.py
111 lines (92 loc) · 3.33 KB
/
server.py
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
# -*- coding: utf-8 -*-
import simplejson as json
from flask import Flask
from flask.ext.restful import reqparse, abort, Api, Resource, request
from flask import make_response
from flask import render_template
from flask import redirect, url_for
from core.LO import *
from core.search import SearchRepository
from core.utils import repository_lo, convert_to_uri, URIRef
app = Flask(__name__)
api = Api(app)
@api.representation('application/json')
def output_json(data, code, headers=None):
resp = make_response(data, code)
resp.headers.extend(headers or {})
return resp
@api.representation('text/html')
def output_html(data, code, headers=None):
resp = make_response(data, code)
resp.headers.extend(headers or {})
return resp
# Instanciando a maquina de Busca
search = SearchRepository()
def abort_if_doesnt_exist(result, uri):
if result:
return result, 200
else:
abort(404, message="Resource doesn't exist".format(uri))
parser = reqparse.RequestParser()
parser.add_argument('uri', type=str)
parser.add_argument('title', type=str)
parser.add_argument('identifier', type=str)
parser.add_argument('callback', type=str)
class Add(Resource):
def post(self):
title = request.json['title']
identifier = request.json['identifier']
obj = LO(repository_lo[convert_to_uri(title)])
obj.title = title
obj.identifier = identifier
return '{"status": "success", "data": {"uri": "' + obj.resUri +'" }, "message": "null" }', 201
class Control(Resource):
def get(self):
args = parser.parse_args()
uri = args['uri']
result = search.uri(uri)
return abort_if_doesnt_exist(result, uri)
# Apagar Objeto [NECESSARIO IMPLEMENTAR]
def delete(self):
args = parser.parse_args()
uri = args['uri']
# #abort_if_todo_doesnt_exist(uri)
# #del [uri]
return 'Funcionou'
def post(self):
uri = request.json['uri']
description = request.json['description']
annotations = request.json['annotations']
tags = request.json['tags']
obj = LO('<'+uri+'>')
obj.description = description
if tags:
keywords = []
for value in tags:
if value.get('uri') != 'undefined':
keywords.append(URIRef(value.get('uri')))
else:
keywords.append(value.get('label'))
#keywords = [URIRef(x.get('uri')) for x in tags] # Armazenando URI como Palavra-chaves
obj.subject = keywords
# Adicionar os outros metadados.
return '{"status": "success", "data": { }, "message": "null" }', 202
class Search(Resource):
def get(self):
import pdb; pdb.set_trace()
args = parser.parse_args()
title = args['title']
result = search.title(title)
return result.read(), result.getcode()
class AddMetadataView(Resource):
def get(self):
args = parser.parse_args()
uri = args['uri']
callback = args['callback']
return render_template('index.html', uri = uri, callback = callback)
api.add_resource(Add, '/add')
api.add_resource(Control, '/control')
api.add_resource(Search, '/search')
api.add_resource(AddMetadataView, '/addmetadata')
if __name__ == '__main__':
app.run(debug=True)