Skip to content
Permalink
Browse files

Retainer task submission now works.

  • Loading branch information...
thisisdhaas committed Jun 20, 2015
1 parent 8921224 commit 745d710315a81e627d4e24cde193a9d27bc252bb
@@ -40,15 +40,17 @@ def delete_tasks(task_objects):

@staticmethod
def get_assignment_context(request):
request_data = request.GET if request.method == 'GET' else request.POST

# parse information from AMT in the URL
context = {
'task_id': request.GET.get('hitId'),
'worker_id': request.GET.get('workerId'),
'submit_url': request.GET.get('turkSubmitTo'),
'task_id': request_data.get('hitId'),
'worker_id': request_data.get('workerId'),
'submit_url': request_data.get('turkSubmitTo'),
}

# check for requests for a preview of the task
assignment_id = request.GET.get('assignmentId')
assignment_id = request_data.get('assignmentId')
if assignment_id == AMT_NO_ASSIGNMENT_ID:
assignment_id = None
is_accepted = False
@@ -66,16 +68,6 @@ def get_assignment_context(request):

return context

@staticmethod
def get_response_context(request):
# Extract data from the request
return {
'answers': request.POST.get('answers'),
'task_id': request.POST.get('HITId'),
'worker_id': request.POST.get('workerId'),
'assignment_id': request.POST.get('assignmentId')
}

def get_frontend_submit_url(self, crowd_config):
return (settings.POST_BACK_AMT_SANDBOX
if crowd_config['sandbox'] else settings.POST_BACK_AMT)
@@ -1,10 +1,5 @@
{% extends "basecrowd/base.html" %}

{% block jslibraries %}
{{ block.super }}
<script type='text/javascript' src='https://s3.amazonaws.com/mturk-public/externalHIT_v1.js'></script>
{% endblock jslibraries %}

{% block get_submit_context_func %}
function get_submit_context(urlParamStrings)
{
@@ -24,15 +19,10 @@
}

return {
HITId: HITId,
workerId: workerId,
assignmentId: assignmentId
task_id: HITId,
worker_id: workerId,
assignment_id: assignmentId
}

}
{% endblock get_submit_context_func %}


{% block inline_js %}
<script language="Javascript">turkSetAssignmentID();</script>
{% endblock inline_js %}
@@ -91,14 +91,15 @@ def get_assignment_context(request):
Additionally, the keys 'content', 'group_context', and 'response_url'
are reserved.
"""
# Base implementation, look for the fields in the GET dictionary.
return {'task_id': request.GET.get('task_id', None),
'worker_id': request.GET.get('worker_id', None),
'is_accepted': request.GET.get('is_accepted', True)}
# Base implementation, look for the fields in the request dictionary.
request_data = request.GET if request.method == 'GET' else request.POST
return {'task_id': request_data.get('task_id', None),
'worker_id': request_data.get('worker_id', None),
'is_accepted': request_data.get('is_accepted', True)}

@staticmethod
def get_response_context(request):
""" Extract response data from a post request.
""" Extract response data from a request.
`request` is a Django HttpRequest object created when the crowd
interface posts data from an assignment. This method should return a
@@ -111,12 +112,13 @@ def get_response_context(request):
* `answers`: the assignment responses in json form (task-type dependent)
"""
# Base implementation, look for the fields in the POST dictionary
# Base implementation, look for the fields in the request dictionary
request_data = request.GET if request.method == 'GET' else request.POST
return {
'task_id': request.POST.get('task_id', None),
'worker_id': request.POST.get('worker_id', None),
'assignment_id': request.POST.get('assignment_id', None),
'answers': request.POST.get('answers', None)
'task_id': request_data.get('task_id', None),
'worker_id': request_data.get('worker_id', None),
'assignment_id': request_data.get('assignment_id', None),
'answers': request_data.get('answers', None)
}

@staticmethod
@@ -28,6 +28,8 @@ var Retainer = {
},

checkForWork: function(requestData){
$('#waitingDiv').show();
$('#taskFrame').hide();
$.get(WORK_ENDPOINT,
requestData,
function(data, status){
@@ -47,12 +49,15 @@ var Retainer = {
console.log('initialize task here');
alert('start now');

task_frame = $('#taskFrame');
var task_frame = $('#taskFrame');
task_frame.attr('src', data.task_url);
task_frame.load(function() {
task_frame.show();
task_frame.height(task_frame.contents().height());
$('#waitingDiv').hide();

// sneakily override the submit behavior of the iframe
task_frame[0].contentWindow.submit_to_frontend = Retainer.checkForWork;
});

}
@@ -36,22 +36,43 @@
{% block get_submit_context_func %}
function get_submit_context(urlParamStrings)
{
// dummy implementation: no context.
// dummy implementation: no custom context.
return {}
}
{% endblock get_submit_context_func %}
{% block get_submit_context_default_func %}
function get_submit_context_default()
{
return {
task_id: "{{ task_id }}",
worker_id: "{{ worker_id }}",
assignment_id: "{{ assignment_id }}"
};
}
{% endblock get_submit_context_default_func %}
{% block prepare_submit_data_func %}
function prepare_submit_data()
{
// build the user's answers into the right format
var answers = build_answer_data();
// get additional context from the URL or page
// default context
var data = get_submit_context_default();
// crowd-specific context (can override defaults)
var paramstr = window.location.search.substring(1);
var parampairs = paramstr.split("&");
var data = get_submit_context(parampairs);
var custom_data = get_submit_context(parampairs);
for (var key in custom_data) {
if (custom_data.hasOwnProperty(key) && custom_data[key] != undefined)
data[key] = custom_data[key];
}
data.answers = JSON.stringify(answers);
return data;
}
@@ -125,10 +146,10 @@
{
event.preventDefault();
if (!validate_answers()) return;
var data = prepare_submit_data();
$.when(submit_to_backend(data)).then(function(data, status)
var submit_data = prepare_submit_data();
$.when(submit_to_backend(submit_data)).then(function(data, status)
{
submit_to_frontend(data);
submit_to_frontend(submit_data);
});
});
@@ -170,7 +191,6 @@
{% block form_tag %}
<form method="post" id="submitForm" action="">
{% endblock form_tag %}
<input type="hidden" value="" name="assignmentId" id="assignmentId" />
<section class="container task-container" id="DataCollection">
<div class="row col-xs-12 col-md-12">

@@ -105,7 +105,7 @@ def create_task_group(request, crowd_name):
for point_id, point_content in content.iteritems():
task = model_spec.task_model(
task_type=configuration['task_type'],
data=json.dumps({point_id: content}),
data=json.dumps({point_id: point_content}),
create_time=pytz.utc.localize(datetime.now()),
task_id=point_id,
group=current_group,
@@ -192,7 +192,8 @@ def get_assignment(request, crowd_name):
return _get_assignment(request, crowd_name, interface, model_spec, context)


def _get_assignment(request, crowd_name, interface, model_spec, context):
def _get_assignment(request, crowd_name, interface, model_spec, context,
**custom_template_context):
# Retrieve the task based on task_id from the database
try:
current_task = model_spec.task_model.objects.get(
@@ -235,6 +236,7 @@ def _get_assignment(request, crowd_name, interface, model_spec, context):
backend_submit_url=interface.get_backend_submit_url(),
frontend_submit_url=interface.get_frontend_submit_url(crowd_config),
crowd_name=crowd_name)
context.update(**custom_template_context)

# Load the template and render it.
template = get_scoped_template(crowd_name, current_task.task_type + '.html',
@@ -348,6 +350,7 @@ def assign_retainer_task(request, crowd_name):
# Look for a task the worker is already assigned to
assignment_task = None
existing_assignments = (worker.tasks
.filter(is_complete=False)
.filter(group__retainer_pool=pool)
.exclude(task_type='retainer'))
if existing_assignments.exists():
@@ -371,6 +374,7 @@ def assign_retainer_task(request, crowd_name):
.annotate(num_workers=Count('workers'))
.filter(num_workers__lt=F('num_assignments')))


# Pick a random one and assign it to the worker
if open_tasks.exists():
assignment_task = open_tasks.order_by('?')[0]
@@ -389,6 +393,8 @@ def assign_retainer_task(request, crowd_name):
kwargs=url_args)
})
return HttpResponse(response_data)
else:
return HttpResponse(json.dumps({'start': False}))

# we need this view to load in AMT's iframe, so disable Django's built-in
# clickjacking protection.
@@ -16,8 +16,11 @@ class InternalCrowdInterface(CrowdInterface):
@staticmethod
def get_assignment_context(request):
""" Get a random task of the specified type."""
worker_id = request.GET.get('worker_id')
task_type = request.GET.get('task_type')

# Extract data from the request
request_data = request.GET if request.method == 'GET' else request.POST
worker_id = request_data.get('worker_id')
task_type = request_data.get('task_type')
eligible_tasks = (InternalCrowdInterface.get_eligible_tasks(worker_id)
.filter(task_type=task_type)
.order_by('create_time'))
@@ -1,19 +1,8 @@
{% extends "basecrowd/base.html" %}

{% block get_submit_context_func %}
function get_submit_context(urlParamStrings)
{
return {
task_id: "{{ task_id }}",
worker_id: "{{ worker_id }}",
assignment_id: "{{ assignment_id }}"
};
}
{% endblock get_submit_context_func %}

{% block form_tag %}
{{ block.super }}
<input type="hidden" value="{{ task_id }}" name="task_id"/>
<input type="hidden" value="{{ worker_id }}" name="worker_id"/>
<input type="hidden" value="{{ assignment_id }}" name="assignment_id"/>
{% endblock %}
{% endblock %}

0 comments on commit 745d710

Please sign in to comment.
You can’t perform that action at this time.