Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 102 lines (78 sloc) 2.989 kB
a0a3201 @nickstenning Initial commit
nickstenning authored
1 from flask import Flask, Module
4c816d2 @nickstenning Add ability to reject requests missing x-annotator auth headers
nickstenning authored
2 from flask import abort, json, redirect, request, url_for
a0a3201 @nickstenning Initial commit
nickstenning authored
3
f48ce77 @nickstenning Move config into separate file
nickstenning authored
4 from .model import Annotation, Range, session
4c816d2 @nickstenning Add ability to reject requests missing x-annotator auth headers
nickstenning authored
5 from . import auth
a0a3201 @nickstenning Initial commit
nickstenning authored
6
4b94184 @nickstenning Refactor annotator.annotator into annotator.store
nickstenning authored
7 __all__ = ["app", "store", "setup_app"]
8
4c816d2 @nickstenning Add ability to reject requests missing x-annotator auth headers
nickstenning authored
9 app = Flask('annotator')
af20592 @nickstenning Save to an SQLite database instead
nickstenning authored
10 store = Module(__name__)
a0a3201 @nickstenning Initial commit
nickstenning authored
11
4b94184 @nickstenning Refactor annotator.annotator into annotator.store
nickstenning authored
12 def setup_app():
13 app.register_module(store, url_prefix=app.config['MOUNTPOINT'])
14
37fba1f @nickstenning More useful comments.
nickstenning authored
15 # We define our own jsonify rather than using flask.jsonify because we wish
16 # to jsonify arbitrary objects (e.g. index returns a list) rather than kwargs.
4c816d2 @nickstenning Add ability to reject requests missing x-annotator auth headers
nickstenning authored
17 def jsonify(obj, *args, **kwargs):
a0a3201 @nickstenning Initial commit
nickstenning authored
18 res = json.dumps(obj, indent=None if request.is_xhr else 2)
4c816d2 @nickstenning Add ability to reject requests missing x-annotator auth headers
nickstenning authored
19 return app.response_class(res, mimetype='application/json', *args, **kwargs)
a0a3201 @nickstenning Initial commit
nickstenning authored
20
21 def unjsonify(str):
22 return json.loads(str)
23
4c816d2 @nickstenning Add ability to reject requests missing x-annotator auth headers
nickstenning authored
24 @store.before_request
25 def before_request():
26 if app.config['AUTH_ON'] and not auth.verify_request(request):
27 return jsonify("Cannot authorise request. Perhaps you didn't send the x-annotator headers?", status=401)
28
29
4b94184 @nickstenning Refactor annotator.annotator into annotator.store
nickstenning authored
30 @store.after_request
31 def after_request(response):
4c816d2 @nickstenning Add ability to reject requests missing x-annotator auth headers
nickstenning authored
32 if response.status_code < 300:
33 response.headers['Access-Control-Allow-Origin'] = '*'
34 response.headers['Access-Control-Expose-Headers'] = 'Location'
35 response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, DELETE'
36 response.headers['Access-Control-Max-Age'] = '86400'
37
4b94184 @nickstenning Refactor annotator.annotator into annotator.store
nickstenning authored
38 return response
39
37fba1f @nickstenning More useful comments.
nickstenning authored
40 # INDEX
96811fa @nickstenning Switch to expecting content-type of 'application/json' in POST/PUT re…
nickstenning authored
41 @store.route('/annotations')
a0a3201 @nickstenning Initial commit
nickstenning authored
42 def index():
af20592 @nickstenning Save to an SQLite database instead
nickstenning authored
43 annotations = [a.to_dict() for a in Annotation.query.all()]
44 return jsonify(annotations)
a0a3201 @nickstenning Initial commit
nickstenning authored
45
37fba1f @nickstenning More useful comments.
nickstenning authored
46 # CREATE
96811fa @nickstenning Switch to expecting content-type of 'application/json' in POST/PUT re…
nickstenning authored
47 @store.route('/annotations', methods=['POST'])
a0a3201 @nickstenning Initial commit
nickstenning authored
48 def create_annotation():
96811fa @nickstenning Switch to expecting content-type of 'application/json' in POST/PUT re…
nickstenning authored
49 if request.json:
af20592 @nickstenning Save to an SQLite database instead
nickstenning authored
50 annotation = Annotation()
96811fa @nickstenning Switch to expecting content-type of 'application/json' in POST/PUT re…
nickstenning authored
51 annotation.from_dict(request.json)
af20592 @nickstenning Save to an SQLite database instead
nickstenning authored
52
53 session.commit()
54
712016e @nickstenning Don't redirect on create. This isn't particularly desirable, but for …
nickstenning authored
55 return jsonify(annotation.to_dict())
a0a3201 @nickstenning Initial commit
nickstenning authored
56 else:
4c816d2 @nickstenning Add ability to reject requests missing x-annotator auth headers
nickstenning authored
57 return jsonify('No parameters given. Annotation not created.', status=400)
a0a3201 @nickstenning Initial commit
nickstenning authored
58
37fba1f @nickstenning More useful comments.
nickstenning authored
59 # READ
96811fa @nickstenning Switch to expecting content-type of 'application/json' in POST/PUT re…
nickstenning authored
60 @store.route('/annotations/<int:id>')
a0a3201 @nickstenning Initial commit
nickstenning authored
61 def read_annotation(id):
e92d85d @nickstenning Use #get, not #get_by
nickstenning authored
62 annotation = Annotation.get(id)
af20592 @nickstenning Save to an SQLite database instead
nickstenning authored
63
64 if annotation:
65 return jsonify(annotation.to_dict())
a0a3201 @nickstenning Initial commit
nickstenning authored
66 else:
4c816d2 @nickstenning Add ability to reject requests missing x-annotator auth headers
nickstenning authored
67 return jsonify('Annotation not found.', status=404)
a0a3201 @nickstenning Initial commit
nickstenning authored
68
37fba1f @nickstenning More useful comments.
nickstenning authored
69 # UPDATE
96811fa @nickstenning Switch to expecting content-type of 'application/json' in POST/PUT re…
nickstenning authored
70 @store.route('/annotations/<int:id>', methods=['PUT'])
a0a3201 @nickstenning Initial commit
nickstenning authored
71 def update_annotation(id):
e92d85d @nickstenning Use #get, not #get_by
nickstenning authored
72 annotation = Annotation.get(id)
af20592 @nickstenning Save to an SQLite database instead
nickstenning authored
73
74 if annotation:
96811fa @nickstenning Switch to expecting content-type of 'application/json' in POST/PUT re…
nickstenning authored
75 if request.json:
76 annotation.from_dict(request.json)
af20592 @nickstenning Save to an SQLite database instead
nickstenning authored
77
78 session.commit()
79
80 return jsonify(annotation.to_dict())
a0a3201 @nickstenning Initial commit
nickstenning authored
81 else:
4c816d2 @nickstenning Add ability to reject requests missing x-annotator auth headers
nickstenning authored
82 return jsonify('Annotation not found. No update performed.', status=404)
a0a3201 @nickstenning Initial commit
nickstenning authored
83
37fba1f @nickstenning More useful comments.
nickstenning authored
84 # DELETE
96811fa @nickstenning Switch to expecting content-type of 'application/json' in POST/PUT re…
nickstenning authored
85 @store.route('/annotations/<int:id>', methods=['DELETE'])
a0a3201 @nickstenning Initial commit
nickstenning authored
86 def delete_annotation(id):
e92d85d @nickstenning Use #get, not #get_by
nickstenning authored
87 annotation = Annotation.get(id)
af20592 @nickstenning Save to an SQLite database instead
nickstenning authored
88
89 if annotation:
90 annotation.delete()
91 session.commit()
92
a0a3201 @nickstenning Initial commit
nickstenning authored
93 return None, 204
94 else:
4c816d2 @nickstenning Add ability to reject requests missing x-annotator auth headers
nickstenning authored
95 return jsonify('Annotation not found. No delete performed.', status=404)
96811fa @nickstenning Switch to expecting content-type of 'application/json' in POST/PUT re…
nickstenning authored
96
97 # Search
98 @store.route('/search')
99 def search_annotations():
100 # TODO: actually do some searching.
101 annotations = [a.to_dict() for a in Annotation.query.all()]
102 return jsonify({'results': annotations})
Something went wrong with that request. Please try again.