-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcustom.py
133 lines (121 loc) · 5.22 KB
/
custom.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# this file imports custom routes into the experiment server
from flask import Blueprint, render_template, request, jsonify, Response, abort, current_app, flash
from jinja2 import TemplateNotFound
from functools import wraps
from sqlalchemy import or_, and_
from sqlalchemy.orm.exc import NoResultFound
from psiturk.psiturk_config import PsiturkConfig
from psiturk.experiment_errors import ExperimentError
from psiturk.psiturk_exceptions import PsiturkException
from psiturk.user_utils import PsiTurkAuthorization, nocache
# # Database setup
from psiturk.db import db_session, init_db
from psiturk.models import Participant
from json import dumps, loads
from custom_models import LegitWorker
import datetime
# load the configuration options
config = PsiturkConfig()
config.load_config()
username = config.get('Server Parameters', 'login_username')
password = config.get('Server Parameters', 'login_pw')
if not username or not password:
raise PsiturkException(message='Either login_username or login_pw is blank -- fix this!')
myauth = PsiTurkAuthorization(config) # if you want to add a password-protect route use this
# explore the Blueprint
custom_code = Blueprint('custom_code', __name__, template_folder='templates', static_folder='static')
###########################################################
# serving warm, fresh, & sweet custom, user-provided routes
# add them here
###########################################################
# Status codes
NOT_ACCEPTED = 0
ALLOCATED = 1
STARTED = 2
COMPLETED = 3
SUBMITTED = 4
CREDITED = 5
QUITEARLY = 6
BONUSED = 7
#----------------------------------------------
# example using HTTP authentication
#----------------------------------------------
@custom_code.route('/whoopsyall_dashboard', methods=['GET','POST'])
@myauth.requires_auth
def dashboard():
if 'mode' in request.form:
if request.form['mode']=='add':
if ('workerid' in request.form) and ('bonus' in request.form):
if (LegitWorker.query.filter(LegitWorker.amt_worker_id == request.form['workerid']).count() == 0):
newworker = LegitWorker(workerid=request.form['workerid'])
newworker.set_bonus(float(request.form['bonus']))
db_session.add(newworker)
db_session.commit()
else:
flash('That worker has already been added!', 'error')
elif request.form['mode']=='delete':
if ('index' in request.form):
current_app.logger.info('deleting')
try:
lw=LegitWorker.query.filter(LegitWorker.index == int(request.form['index'])).one()
db_session.delete(lw)
db_session.commit()
except:
flash(u'Sorry, was unable to delete that worker. Perhaps they were already deleted!', 'error')
elif request.form['mode']=='refresh':
failed_workers = []
workers = LegitWorker.query.all()
for lw in workers:
try:
user = Participant.query.filter(Participant.workerid == lw.amt_worker_id).one()
if user.status == BONUSED:
try:
lw.paid()
db_session.add(lw)
db_session.commit()
except Exception as ex:
current_app.logger.error('Could not update worker %s to paid status: %s',
lw.amt_worker_id,
ex)
failed_workers.append(w.amt_worker_id)
except NoResultFound:
pass # hasn't submitted yet...
if len(failed_workers) > 0:
display_str = u'Could not update the following workers:'
for w in failed_workers:
display_str += '\n%s' % (w)
flash(display_str, 'error')
try:
workers = LegitWorker.query.all()
return render_template('whoopsyall_dashboard.html', workers = workers)
except TemplateNotFound:
abort(404)
#----------------------------------------------
# verify secret code
#----------------------------------------------
@custom_code.route('/check_secret_code', methods=['POST'])
def check_secret_code():
current_app.logger.info(request.form['workerid'])
current_app.logger.info(request.form['code'])
uniqueId = request.form['uniqueid']
try:
worker = LegitWorker.query.filter(and_(LegitWorker.amt_worker_id ==request.form['workerid'], \
LegitWorker.completion_code == request.form['code'], \
LegitWorker.status=='owed')).one()
except:
abort(406)
worker.submitted()
db_session.add(worker)
db_session.commit()
try:
user = Participant.query.\
filter(Participant.uniqueid == uniqueId).one()
user.bonus = worker.bonus
user.status = COMPLETED
user.endhit = datetime.datetime.now()
db_session.add(user)
db_session.commit()
except:
abort(406)
resp = {"bonus": user.bonus}
return jsonify(**resp)