Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@app.route('/complete', methods=['POST'])
def completed():
"""
This is sent in when the participant completes the debriefing. The
participant can accept the debriefing or declare that they were not
adequately debriefed, and that response is logged in the database.
"""
print "accessing the /complete route"
if not (request.form.has_key('assignmentid') and request.form.has_key('agree')):
raise ExperimentError('improper_inputs')
assignmentId = request.form['assignmentid']
workerId = request.form['workerid']
agreed = request.form['agree']
print workerId, assignmentId, agreed
user = Participant.query.\
filter(Participant.assignmentid == assignmentId).\
filter(Participant.workerid == workerId).\
one()
user.status = DEBRIEFED
user.debriefed = agreed == 'true'
db_session.add(user)
db_session.commit()
return render_template('closepopup.html')
@app.route('/debrief', methods=['GET'])
def savedata():
"""
User has finished the experiment and is posting their data in the form of a
(long) string. They will receive a debreifing back.
"""
print request.args.keys()
if not request.args.has_key('uniqueId'):
raise ExperimentError('improper_inputs')
else:
uniqueId = request.args['uniqueId']
print "/debrief called with", uniqueId
user = Participant.query.\
filter(Participant.uniqueid == uniqueId).\
one()
user.status = COMPLETED
user.endhit = datetime.datetime.now()
db_session.add(user)
db_session.commit()
return render_template('debriefing.html', workerId=user.workerid, assignmentId=user.assignmentid)
@app.route('/inexp', methods=['POST'])
def enterexp():
"""
AJAX listener that listens for a signal from the user's script when they
leave the instructions and enter the real experiment. After the server
receives this signal, it will no longer allow them to re-access the
experiment applet (meaning they can't do part of the experiment and
referesh to start over).
"""
print "Accessing /inexp"
if not request.form.has_key('uniqueId'):
raise ExperimentError('improper_inputs')
uniqueId = request.form['uniqueId']
user = Participant.query.\
filter(Participant.uniqueid == uniqueId).\
one()
user.status = STARTED
user.beginexp = datetime.datetime.now()
db_session.add(user)
db_session.commit()
return "Success"
# 1: They've already done an assignment, then we should tell them they can't do another one
# 2: They've already worked on this assignment, and got too far to start over.
# 3: They're in the database twice for the same assignment, that should never happen.
# 4: They're returning and all is well.
already_entered = False
nrecords = 0
for record in matches:
other_assignment = False
if record.assignmentid != assignmentId:
other_assignment = True
else:
nrecords += 1
if nrecords <= 1 and not other_assignment:
part = matches[0]
if part.status>=STARTED: # in experiment (or later) can't restart at this point
raise ExperimentError('already_started_exp')
else:
if nrecords > 1:
print "Error, hit/assignment appears in database more than once (serious problem)"
raise ExperimentError('hit_assign_appears_in_database_more_than_once')
if other_assignment:
raise ExperimentError('already_did_exp_hit')
return render_template('exp.html', uniqueId=part.uniqueid)
@app.route('/consent', methods=['GET'])
def give_consent():
"""
Serves up the consent in the popup window.
"""
if not (request.args.has_key('hitId') and request.args.has_key('assignmentId') and request.args.has_key('workerId')):
raise ExperimentError('hit_assign_worker_id_not_set_in_consent')
hitId = request.args['hitId']
assignmentId = request.args['assignmentId']
workerId = request.args['workerId']
print "Accessing /consent: ", hitId, assignmentId, workerId
return render_template('consent.html', hitid = hitId, assignmentid=assignmentId, workerid=workerId)
This is the url we give for our 'external question'.
This page will be called from within mechanical turk, with url arguments
hitId, assignmentId, and workerId.
If the worker has not yet accepted the hit:
These arguments will have null values, we should just show an ad for the
experiment.
If the worker has accepted the hit:
These arguments will have appropriate values and we should enter the person
in the database and provide a link to the experiment popup.
"""
if not SUPPORT_IE:
# Handler for IE users if IE is not supported.
if request.user_agent.browser == "msie":
return render_template( 'ie.html' )
if not (request.args.has_key('hitId') and request.args.has_key('assignmentId')):
raise ExperimentError('hit_assign_worker_id_not_set_in_mturk')
# Person has accepted the HIT, entering them into the database.
hitId = request.args['hitId']
# Turn assignmentId into unique combination of assignment and worker Id
assignmentId = request.args['assignmentId']
already_in_db = False
if request.args.has_key('workerId'):
workerId = request.args['workerId']
# first check if this workerId has completed the task before (v1)
nrecords = Participant.query.\
filter(Participant.assignmentid != assignmentId).\
filter(Participant.workerid == workerId).\
count()
if nrecords > 0:
# already completed task
already_in_db = True
return render_template('thanks.html',
using_sandbox=USING_SANDBOX,
hitid = hitId,
assignmentid = assignmentId,
workerid = workerId)
elif already_in_db:
raise ExperimentError('already_did_exp_hit')
elif status == ALLOCATED or not status:
# Participant has not yet agreed to the consent. They might not
# even have accepted the HIT.
return render_template('mturkindex.html',
hitid = hitId,
assignmentid = assignmentId,
workerid = workerId)
else:
raise ExperimentError( "STATUS_INCORRECTLY_SET" )
@app.route('/exp', methods=['GET'])
def start_exp():
"""
Serves up the experiment applet.
"""
if not (request.args.has_key('hitId') and request.args.has_key('assignmentId') and request.args.has_key('workerId')):
raise ExperimentError( 'hit_assign_worker_id_not_set_in_exp' )
hitId = request.args['hitId']
assignmentId = request.args['assignmentId']
workerId = request.args['workerId']
print "Accessing /exp: ", hitId, assignmentId, workerId
# check first to see if this hitId or assignmentId exists. if so check to see if inExp is set
matches = Participant.query.\
filter(Participant.workerid == workerId).\
all()
numrecs = len(matches)
if numrecs == 0:
# Choose condition and counterbalance
subj_cond, subj_counter = get_random_condcount()
ip = "UNKNOWN" if not request.remote_addr else request.remote_addr
browser = "UNKNOWN" if not request.user_agent.browser else request.user_agent.browser
for record in matches:
other_assignment = False
if record.assignmentid != assignmentId:
other_assignment = True
else:
nrecords += 1
if nrecords <= 1 and not other_assignment:
part = matches[0]
if part.status>=STARTED: # in experiment (or later) can't restart at this point
raise ExperimentError('already_started_exp')
else:
if nrecords > 1:
print "Error, hit/assignment appears in database more than once (serious problem)"
raise ExperimentError('hit_assign_appears_in_database_more_than_once')
if other_assignment:
raise ExperimentError('already_did_exp_hit')
return render_template('exp.html', uniqueId=part.uniqueid)
raise ExperimentError('already_started_exp_mturk')
elif status == COMPLETED:
# They've done the whole task, but haven't signed the debriefing yet.
return render_template('debriefing.html',
workerId = workerId,
assignmentId = assignmentId)
elif status == DEBRIEFED:
# They've done the debriefing but perhaps haven't submitted the HIT yet..
# Turn asignmentId into original assignment id before sending it back to AMT
return render_template('thanks.html',
using_sandbox=USING_SANDBOX,
hitid = hitId,
assignmentid = assignmentId,
workerid = workerId)
elif already_in_db:
raise ExperimentError('already_did_exp_hit')
elif status == ALLOCATED or not status:
# Participant has not yet agreed to the consent. They might not
# even have accepted the HIT.
return render_template('mturkindex.html',
hitid = hitId,
assignmentid = assignmentId,
workerid = workerId)
else:
raise ExperimentError( "STATUS_INCORRECTLY_SET" )