-
Notifications
You must be signed in to change notification settings - Fork 0
/
flaskapp.py
executable file
·69 lines (56 loc) · 2.39 KB
/
flaskapp.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
#!/usr/bin/env python
#flask implementation of my migra web app
from migra import Migra, MigraPersonEncoder
from migrastorage import fileStorage
from flask import Flask, make_response, request, render_template, url_for, session, jsonify, send_from_directory
import sys
import os
import json
app = Flask(__name__)
app.secret_key = os.environ.get('MIGRA_SESSIONKEY',None)
migra = Migra()
def jsonresponse(data):
resp = make_response(json.dumps(data,cls=MigraPersonEncoder))
resp.headers['Content-Type'] = 'application/json'
return resp
def __allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ['ged','zip']
@app.route('/')
def index():
""" Just displays the index template
This is where all the HTML action happens.
Everything else is a JSON request/response """
return render_template('index.html')
@app.route('/favicon.ico')
def favicon():
path = os.path.join(app.root_path, 'static/img')
sys.stderr.write ( path + "\n" )
return send_from_directory(path,
'favicon.ico', mimetype='image/vnd.microsoft.icon')
@app.route('/upload',methods=['POST'])
def upload():
""" Get the file and the search term from the upload, turn it into a gedcom, do something with this """
file = request.files['gedcom']
if file and __allowed_file(file.filename):
all = migra.upload(file)
session['key'] = fileStorage().store_file(all,session.get('key',None))
return jsonresponse({'count': len(all.keys())})
else:
raise MigraError, ('File not allowed')
@app.route('/filter',methods=['GET','POST'])
def filter():
q = request.form['q']
d = fileStorage().get_file(session['key'])
return jsonresponse({'people': migra.filter ( d, q ), 'parameters': { 'query': q } } )
@app.route('/walk',methods=['GET','POST'])
def walk():
"""Now we have to find our file and send it to gedcom -- unless we can attach the gedcom created earlier via session!"""
d = fileStorage().get_file(session['key'])
return jsonresponse( migra.walk(d,request.form['i'],request.form['d']) )
@app.route('/cache',methods=['POST'])
def cache():
'''this caches the data sent. we don't care about the results (though maybe we should) '''
return jsonresponse( migra.cache(request.form) )
if __name__ == '__main__':
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 5000)), debug=True)