-
Notifications
You must be signed in to change notification settings - Fork 1
/
views.py
96 lines (84 loc) · 3.04 KB
/
views.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
from flask import Flask, make_response, render_template, redirect, request, url_for
app = Flask(__name__)
from sqlalchemy import create_engine, asc, desc
from sqlalchemy.orm import sessionmaker
from database_setup import Base, Comparison, User
from io import StringIO
import csv
import config
engine = create_engine("sqlite:///comparisons.db")
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
@app.route('/')
@app.route('/home/')
def home_page():
return render_template('home.html')
@app.route('/set_cookie/', methods=['GET'])
def setcookie():
resp = make_response(redirect(url_for('make_comparison')))
user_id = request.cookies.get("user_id")
if user_id:
return resp
else:
session = DBSession()
if session.query(User).count() == 0:
user_id = "1"
else:
index = int(session.query(User).order_by(User.id.desc()).first().id)
user_id = str(index + 1)
session.add(User(id=user_id, responses=0))
session.commit()
session.close()
resp.set_cookie('user_id', user_id)
return resp
@app.route('/compare/')
def make_comparison():
user_id = request.cookies.get('user_id')
session = DBSession()
user = session.query(User).filter_by(id=user_id).one()
if user.responses == config.min_repetitions:
user.responses = 0
session.commit()
session.close()
return redirect(url_for('finish'))
comparison = session.query(Comparison).order_by(Comparison.total_comparisons.asc()).first()
session.close()
resp = make_response(render_template('comparison.html', comparison=comparison))
return resp
@app.route("/commit/<int:comparison_id>/", methods=['POST'])
def commit_comparison(comparison_id):
response = request.form['submit']
session = DBSession()
comparison = session.query(Comparison).filter_by(id=comparison_id).one()
if response == "Match":
comparison.is_match += 1
elif response == "Not a Match":
comparison.is_not_match += 1
else:
comparison.is_pass += 1
comparison.total_comparisons += 1
user_id = request.cookies.get('user_id')
session.query(User).filter_by(id=user_id).one().responses += 1
session.commit()
session.close()
return redirect(url_for('make_comparison'))
@app.route("/data/")
def return_data():
si = StringIO()
data_writer = csv.writer(si)
session = DBSession()
data = session.query(Comparison).with_entities(Comparison.decision_id,
Comparison.contract_id, Comparison.is_match, Comparison.is_not_match,
Comparison.is_pass)
data_writer.writerow(config.csv_columns)
data_writer.writerows(data)
output = make_response(si.getvalue())
output.headers["Content-Disposition"] = "attachment; filename=%s" % config.csv_filename
output.headers["Content-type"] = "text/csv"
return output
@app.route("/finish/")
def finish():
return render_template("finish.html")
if __name__ == "__main__":
app.debug=True
app.run(host=config.ip, port=config.port)