Skip to content

Commit

Permalink
Merge pull request #2 from rwblair/issue154
Browse files Browse the repository at this point in the history
Fix for Issue expfactory#154
  • Loading branch information
rwblair authored Jul 12, 2016
2 parents 564c31b + 94555cb commit e3d0444
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 25 deletions.
2 changes: 1 addition & 1 deletion expdj/apps/experiments/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ def serve_battery(request,bid,userid=None):
missing_batteries, blocking_batteries = check_battery_dependencies(battery, userid)
if missing_batteries or blocking_batteries:
return render_to_response(
"experiments/battery_requirements_not_met.html",
"turk/battery_requirements_not_met.html",
context={'missing_batteries': missing_batteries,
'blocking_batteries': blocking_batteries}
)
Expand Down
10 changes: 10 additions & 0 deletions expdj/apps/turk/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,13 @@ def __init__(self, *args, **kwargs):
self.helper.layout = Layout()
tab_holder = TabHolder()
self.helper.add_input(Submit("submit", "Save"))

class WorkerContactForm(forms.Form):
subject = forms.CharField(label="Subject")
message = forms.CharField(widget=forms.Textarea, label="Message")

def __init__(self, *args, **kwargs):
super(WorkerContactForm, self).__init__(*args, **kwargs)
self.helper = FormHelper(self)
self.helper.layout = Layout()
self.helper.add_input(Submit("submit", "Send"))
1 change: 0 additions & 1 deletion expdj/apps/turk/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,6 @@ class Assignment(models.Model):
(True, 'Completed')),
default=False,verbose_name="participant completed the entire assignment")


def create(self):
init_connection_callback(sender=self.hit)

Expand Down
19 changes: 19 additions & 0 deletions expdj/apps/turk/templates/turk/contact_worker_modal.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% load crispy_forms_tags %}
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title">Contact Worker {{ worker }}</h4>
</div>
<div class="modal-body">
<div>
<form method="post" action="{% url 'contact_worker' assignment.id %}">
{% crispy form %}
</form>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
11 changes: 11 additions & 0 deletions expdj/apps/turk/templates/turk/manage_hit.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ <h2>In Progress</h2>
<th>Worker ID</th>
<th>Status</th>
<th>Accept Time</th>
<th>Contact</th>
</tr>
</thead>
<tbody>
Expand All @@ -82,6 +83,7 @@ <h2>In Progress</h2>
<td>{{ assignment.worker }}</td>
<td>{{ assignment.status }}</td>
<td>{{ assignment.accept_time | localize }}</td>
<td><a class='btn btn-xs btn-default contact_worker' href='{% url 'contact_worker' assignment.id %}'> Contact Worker</a></td>
</tr>
{% endfor %}
</tbody>
Expand Down Expand Up @@ -158,6 +160,7 @@ <h2>Approved</h2>
<th>Worker ID</th>
<th>Status</th>
<th>Accept Time</th>
<th>Contact</th>
</tr>
</thead>
<tbody>
Expand All @@ -167,6 +170,7 @@ <h2>Approved</h2>
<td>{{ assignment.worker }}</td>
<td>{{ assignment.status }}</td>
<td>{{ assignment.accept_time | localize }}</td>
<td><a class='btn btn-xs btn-default contact_worker' href='{% url 'contact_worker' %}'> Contact Worker</a></td>
</tr>
{% endfor %}
</tbody>
Expand All @@ -175,6 +179,8 @@ <h2>Approved</h2>
</div>
{% endif %}
</article>
<div id="contact_modal" class="modal fade" tabindex="-1" role="dialog">
</div>
</div>
</div>
{% endblock %}
Expand All @@ -190,6 +196,11 @@ <h2>Approved</h2>
$('.collapse').collapse('hide');
})

$('.contact_worker').on("click", function(e) {
e.preventDefault();
$('#contact_modal').modal("show").load(this.href);
});

})
</script>
{% endblock %}
39 changes: 30 additions & 9 deletions expdj/apps/turk/urls.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,47 @@
from expdj.apps.turk.views import edit_hit, delete_hit, expire_hit, preview_hit, \
serve_hit, multiple_new_hit, end_assignment, finished_view, not_consent_view, \
survey_submit, manage_hit
from expdj.apps.turk.views import (edit_hit, delete_hit, expire_hit,
preview_hit, serve_hit, multiple_new_hit, end_assignment, finished_view,
not_consent_view, survey_submit, manage_hit, contact_worker)
from expdj.apps.experiments.views import sync
from django.views.generic.base import TemplateView
from django.conf.urls import patterns, url

urlpatterns = patterns('',
# HITS
url(r'^hits/(?P<bid>\d+|[A-Z]{8})/new$',edit_hit,name='new_hit'),
url(r'^hits/(?P<bid>\d+|[A-Z]{8})/(?P<hid>\d+|[A-Z]{8})/manage$',manage_hit,name='manage_hit'),
url(r'^hits/(?P<bid>\d+|[A-Z]{8})/multiple$',multiple_new_hit,name='multiple_new_hit'),
url(r'^hits/(?P<bid>\d+|[A-Z]{8})/(?P<hid>\d+|[A-Z]{8})/edit$',edit_hit,name='edit_hit'),
url(
r'^hits/(?P<bid>\d+|[A-Z]{8})/(?P<hid>\d+|[A-Z]{8})/manage$',
manage_hit,
name='manage_hit'
),
url(
r'^hits/(?P<bid>\d+|[A-Z]{8})/multiple$',
multiple_new_hit,
name='multiple_new_hit'
),
url(
r'^hits/(?P<bid>\d+|[A-Z]{8})/(?P<hid>\d+|[A-Z]{8})/edit$',
edit_hit,
name='edit_hit'
),
url(r'^hits/(?P<hid>\d+|[A-Z]{8})/delete$',delete_hit,name='delete_hit'),
url(r'^hits/(?P<hid>\d+|[A-Z]{8})/expire$',expire_hit,name='expire_hit'),

# Turk Deployments
url(r'^accept/(?P<hid>\d+|[A-Z]{8})',serve_hit,name='serve_hit'),
url(r'^turk/(?P<hid>\d+|[A-Z]{8})',preview_hit,name='preview_hit'),
url(r'^turk/preview',not_consent_view,name='not_consent_view'),
url(r'^turk/end/(?P<rid>\d+|[A-Z]{8})',end_assignment,name='end_assignment'),
url(r'^surveys/(?P<rid>\d+|[A-Z]{8})/(?P<hid>[A-Za-z0-9]{30})/submit$',survey_submit,name='survey_submit'),
url(
r'^turk/end/(?P<rid>\d+|[A-Z]{8})',
end_assignment,
name='end_assignment'
),
url(
r'^surveys/(?P<rid>\d+|[A-Z]{8})/(?P<hid>[A-Za-z0-9]{30})/submit$',
survey_submit,
name='survey_submit'
),
url(r'^sync/(?P<rid>\d+|[A-Z]{8})/$',sync,name='sync_data'),
url(r'^sync/$',sync,name='sync_data'),
url(r'^finished$', finished_view, name="finished_view")
url(r'^finished$', finished_view, name="finished_view"),
url(r'^worker/contact/(?P<aid>\d+)',contact_worker,name='contact_worker')
)
18 changes: 12 additions & 6 deletions expdj/apps/turk/utils.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
from expdj.apps.experiments.models import Experiment
from boto.mturk.connection import MTurkConnection
from expdj.settings import BASE_DIR, MTURK_ALLOW
from boto.mturk.question import ExternalQuestion
from boto.mturk.price import Price
import ConfigParser
import datetime
import pandas
import json
import os

from boto.mturk.connection import MTurkConnection
from boto.mturk.price import Price
from boto.mturk.question import ExternalQuestion
import pandas

from django.conf import settings

from expdj.apps.experiments.models import Experiment
from expdj.settings import BASE_DIR, MTURK_ALLOW



# RESULTS UTILS
def to_dict(input_ordered_dict):
Expand Down Expand Up @@ -132,3 +135,6 @@ def get_time_difference(d1,d2,format='%Y-%m-%d %H:%M:%S'):
if isinstance(d2,str):
d2 = datetime.datetime.strptime(d2, format)
return (d2 - d1).total_seconds() / 60



53 changes: 45 additions & 8 deletions expdj/apps/turk/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,22 @@

from django.contrib.auth.decorators import login_required
from django.core.management.base import BaseCommand
from django.http.response import HttpResponseRedirect, HttpResponseForbidden, HttpResponse, Http404
from django.http.response import (HttpResponseRedirect, HttpResponseForbidden,
HttpResponse, Http404, HttpResponseNotAllowed)
from django.shortcuts import get_object_or_404, render_to_response, render, redirect
from django.utils import timezone
from django.views.decorators.csrf import ensure_csrf_cookie

from expdj.apps.experiments.models import Battery, ExperimentTemplate
from expdj.apps.experiments.views import check_battery_edit_permission, check_mturk_access, \
get_battery_intro, deploy_battery
from expdj.apps.experiments.models import (Battery, ExperimentTemplate)
from expdj.apps.experiments.views import (check_battery_edit_permission,
check_mturk_access, get_battery_intro, deploy_battery)
from expdj.apps.experiments.utils import get_experiment_type, select_experiments
from expdj.apps.turk.forms import HITForm
from expdj.apps.turk.forms import HITForm, WorkerContactForm
from expdj.apps.turk.models import Worker, HIT, Assignment, Result, get_worker
from expdj.apps.turk.tasks import (assign_experiment_credit,
check_battery_dependencies, get_unique_experiments)
from expdj.apps.turk.utils import (get_connection, get_host, get_worker_url,
get_worker_experiments)
get_unique_experiments)
from expdj.apps.turk.utils import (get_connection, get_credentials, get_host,
get_worker_url, get_worker_experiments)
from expdj.settings import BASE_DIR,STATIC_ROOT,MEDIA_ROOT

media_dir = os.path.join(BASE_DIR,MEDIA_ROOT)
Expand Down Expand Up @@ -339,6 +340,42 @@ def edit_hit(request, bid, hid=None):
else:
return HttpResponseForbidden()

@login_required
def contact_worker(request, aid):
mturk_permission = check_mturk_access(request)

if mturk_permission == False:
return HttpResponseForbidden()

assignment = Assignment.objects.get(id=aid)
worker = assignment.worker
if request.method == "GET":
form = WorkerContactForm()
context = {
"form": form,
"worker": worker,
"assignment": assignment
}
return render(request, "turk/contact_worker_modal.html", context)
elif request.method == "POST":
form = WorkerContactForm(request.POST)
if form.is_valid():
AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY_ID = get_credentials(
battery=assignment.hit.battery
)
conn = get_connection(
AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY_ID,
hit=assignment.hit
)
subject = form.cleaned_data['subject']
message = form.cleaned_data['message']
conn.notify_workers([worker.id], subject, message)
return redirect('manage_hit', bid=assignment.hit.battery.id,
hid=assignment.hit.id)
else:
return HttpResponseNotAllowed()

# Expire a hit
@login_required
def expire_hit(request, hid):
Expand Down

0 comments on commit e3d0444

Please sign in to comment.