-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
84 lines (69 loc) · 2.51 KB
/
app.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
import os
from flask import Flask, jsonify, request, session, send_from_directory
from flask_session import Session
from inmemorydbadapter import InMemoryDBAdapter
app = Flask(__name__, static_folder="public/static")
# Set the secret key to some random bytes. Keep this really secret!
app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
app.config['SESSION_TYPE'] = 'filesystem'
app.config['SESSION_PERMANENT'] = False
Session(app)
API_BASE_ADDRESS = "/api"
def get_db_adapter():
return InMemoryDBAdapter(session)
@app.route(API_BASE_ADDRESS + "/getActive", methods=['GET'])
def get_active():
db = get_db_adapter()
return jsonify(db.get_surveys())
@app.route(API_BASE_ADDRESS + "/getSurvey", methods=['GET'])
def get_survey():
db = get_db_adapter()
survey_id = request.args.get('surveyId')
return jsonify(db.get_survey(survey_id))
@app.route(API_BASE_ADDRESS + "/changeName", methods=['GET'])
def change_name():
db = get_db_adapter()
obj_id = request.args.get('id')
new_name = request.args.get('name')
return jsonify(db.change_name(obj_id, new_name))
@app.route(API_BASE_ADDRESS + "/create", methods=['GET'])
def create():
db = get_db_adapter()
name = request.args.get('name')
return jsonify(db.add_survey(name))
@app.route(API_BASE_ADDRESS + "/changeJson", methods=['POST'])
def change_json():
db = get_db_adapter()
data = request.get_json()
obj_id = data.get('id')
json_data = data.get('json')
return jsonify(db.store_survey(obj_id, None, json_data))
@app.route(API_BASE_ADDRESS + "/post", methods=['POST'])
def post_results():
db = get_db_adapter()
data = request.get_json()
post_id = data.get('postId')
survey_result = data.get('surveyResult')
return jsonify(db.post_results(post_id, survey_result))
@app.route(API_BASE_ADDRESS + "/delete", methods=['GET'])
def delete():
db = get_db_adapter()
obj_id = request.args.get('id')
db.delete_survey(obj_id)
return jsonify({'id': obj_id})
@app.route(API_BASE_ADDRESS + "/results", methods=['GET'])
def get_results():
db = get_db_adapter()
post_id = request.args.get('postId')
return jsonify(db.get_results(post_id))
@app.route('/about')
@app.route('/run/<path:path>')
@app.route('/edit/<path:path>')
@app.route('/results/<path:path>')
@app.route('/', defaults={'path': 'index.html'})
def serve_static(path):
return send_from_directory('public', 'index.html')
if __name__ == '__main__':
if not os.path.exists('flask_session'):
os.makedirs('flask_session')
app.run(debug=True)