def serve_hit(request,hid): '''serve_hit runs the experiment after accepting a hit :param hid: the hit id :param wid: the worker id :param aid: the assignment id ''' # No robots allowed! if request.user_agent.is_bot: return render_to_response("turk/robot_sorry.html") # Not allowed on tablet or phone if request.user_agent.is_pc: hit = get_hit(hid,request) # Update the hit, only allow to continue if HIT is valid hit.update() if hit.status in ["D"]: return render_to_response("turk/hit_expired.html") battery = hit.battery aws = get_amazon_variables(request) if "" in [aws["worker_id"],aws["hit_id"]]: return render_to_response("turk/error_sorry.html") # Get Experiment Factory objects for each worker = get_worker(aws["worker_id"]) check_battery_response = check_battery_view(battery, aws["worker_id"]) if (check_battery_response): return check_battery_response # This is the submit URL, either external or sandbox host = get_host(hit) # Only supporting chrome if request.user_agent.browser.family != "Chrome": return render_to_response("turk/browser_sorry.html") # Try to get some info about browser, language, etc. browser = "%s,%s" %(request.user_agent.browser.family,request.user_agent.browser.version_string) platform = "%s,%s" %(request.user_agent.os.family,request.user_agent.os.version_string) # Initialize Assignment object, obtained from Amazon, and Result assignment,already_created = Assignment.objects.get_or_create(mturk_id=aws["assignment_id"], worker=worker, hit=hit) # if the assignment is new, we need to set up a task to run when the worker time runs out to allocate credit if already_created == True: assignment.accept_time = datetime.now() if hit.assignment_duration_in_hours != None: assign_experiment_credit.apply_async([worker.id],countdown=360*(hit.assignment_duration_in_hours)) assignment.save() # Does the worker have experiments remaining for the hit? uncompleted_experiments = get_worker_experiments(worker,hit.battery) experiments_left = len(uncompleted_experiments) if experiments_left == 0: # Thank you for your participation - no more experiments! return render_to_response("turk/worker_sorry.html") # if it's the last experiment, we will submit the result to amazon (only for surveys) last_experiment = False if experiments_left == 1: last_experiment = True task_list = select_experiments(battery,uncompleted_experiments) experimentTemplate = ExperimentTemplate.objects.filter(exp_id=task_list[0].template.exp_id)[0] experiment_type = get_experiment_type(experimentTemplate) task_list = battery.experiments.filter(template=experimentTemplate) template = "%s/mturk_battery.html" %(experiment_type) # Generate a new results object for the worker, assignment, experiment result,_ = Result.objects.update_or_create(worker=worker, experiment=experimentTemplate, assignment=assignment, # assignment has record of HIT battery=hit.battery, defaults={"browser":browser,"platform":platform}) result.save() # Add variables to the context aws["amazon_host"] = host aws["uniqueId"] = result.id # If this is the last experiment, the finish button will link to a thank you page. if experiments_left == 1: next_page = "/finished" return deploy_battery( deployment="docker-mturk", battery=battery, experiment_type=experiment_type, context=aws, task_list=task_list, template=template, next_page=None, result=result, last_experiment=last_experiment, experiments_left=experiments_left-1 ) else: return render_to_response("turk/error_sorry.html")
def serve_hit(request,hid): next_page=None uncompleted_experiments = None result = None # No robots allowed! if request.user_agent.is_bot: return render_to_response("robot_sorry.html") if request.user_agent.is_pc: hit = get_hit(hid,request) battery = hit.battery # This is the submit URL, either external or sandbox host = get_host() # An assignmentID means that the worker has accepted the task assignment_id = request.GET.get("assignmentId","") # worker has not accepted the task if assignment_id in ["ASSIGNMENT_ID_NOT_AVAILABLE",""]: template = "mturk_battery_preview.html" task_list = [battery.experiments.all()[0]] context = dict() deployment = "docker-preview" # worker has accepted the task else: template = "mturk_battery.html" worker_id = request.GET.get("workerId","") hit_id = request.GET.get("hitId","") turk_submit_to = request.GET.get("turkSubmitTo","") if "" in [worker_id,hit_id]: return render_to_response("error_sorry.html") # Get Experiment Factory objects for each worker = get_worker(worker_id) # Try to get some info about browser, language, etc. browser = "%s,%s" %(request.user_agent.browser.family,request.user_agent.browser.version_string) platform = "%s,%s" %(request.user_agent.os.family,request.user_agent.os.version_string) deployment = "docker" # Initialize Assignment object, obtained from Amazon, and Result assignment,already_created = Assignment.objects.get_or_create(mturk_id=assignment_id,hit=hit,worker=worker) # if the assignment is new, we need to set up a task to run when the worker time runs out to allocate credit if already_created == False: assign_experiment_credit.apply_async(countdown=hit.assignment_duration_in_seconds) assignment.save() # Does the worker have experiments remaining for the hit? uncompleted_experiments = get_worker_experiments(worker,hit.battery) if len(uncompleted_experiments) == 0: # Thank you for your participation - no more experiments! return render_to_response("worker_sorry.html") task_list = select_random_n(uncompleted_experiments,1) experimentTemplate = ExperimentTemplate.objects.filter(exp_id=task_list[0])[0] task_list = battery.experiments.filter(template=experimentTemplate) # Generate a new results object for the worker, assignment, experiment result,_ = Result.objects.update_or_create(worker=worker, experiment=experimentTemplate, assignment=assignment, # assignment has record of HIT battery=hit.battery, defaults={"browser":browser,"platform":platform}) result.save() context = { "worker_id": worker_id, "assignment_id": assignment_id, "amazon_host": host, "hit_id": hit_id, "uniqueId":result.id } # If this is the last experiment, the finish button will link to a thank you page. if len(uncompleted_experiments) == 1: next_page = "/finished" return deploy_battery(deployment=deployment, battery=battery, context=context, task_list=task_list, template=template, uncompleted_experiments=uncompleted_experiments, next_page=next_page, result=result) else: return render_to_response("pc_sorry.html")