<commit_before>from datetime import timedelta, datetime
import json
import os
import requests

from expfactory.battery import get_load_static, get_experiment_run
from numpy.random import choice
from optparse import make_option

from django.contrib.auth.decorators import login_required
from django.core.management.base import BaseCommand
from django.http.response import (HttpResponseRedirect, HttpResponseForbidden,
    HttpResponse, Http404, HttpResponseNotAllowed)
from django.core.urlresolvers import reverse
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.utils import get_experiment_type, select_experiments
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,
    get_unique_experiments, check_battery_dependencies)
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)


#### GETS #############################################################

# get experiment
def get_hit(hid,request,mode=None):
    keyargs = {'pk':hid}
    try:
        hit = HIT.objects.get(**keyargs)
    except HIT.DoesNotExist:
        raise Http404
    else:
        return hit

#### VIEWS ############################################################

def get_amazon_variables(request):
    '''get_amazon_variables gets the "GET" variables from the URL,
       returned in a context that can be used across different experiment
       serving functions (local or mturk)
    '''

    # An assignmentID means that the worker has accepted the task
    assignment_id = request.GET.get("assignmentId","")
    worker_id = request.GET.get("workerId","")
    hit_id = request.GET.get("hitId","")
    turk_submit_to = request.GET.get("turkSubmitTo","")

    # worker has not accepted the task
    if assignment_id in ["ASSIGNMENT_ID_NOT_AVAILABLE",""]:
        assignment_id = None

    return {"worker_id": worker_id,
            "assignment_id": assignment_id,
            "hit_id": hit_id,
            "turk_submit_to":turk_submit_to}

@login_required
def manage_hit(request,bid,hid):
    '''manage_hit shows details about workers that have completed / not completed a HIT
    :param hid: the hit id
    '''
    hit =  get_hit(hid,request)
    hit.update()
    battery = hit.battery

    # Get different groups of assignments
    context = dict()
    context["assignments_approved"] = Assignment.objects.filter(status="A",hit=hit)
    context["assignments_rejected"] = Assignment.objects.filter(status="R",hit=hit)
    context["assignments_submit"] = Assignment.objects.filter(status="S",hit=hit)
    context["hit"] = hit
    assignments_remaining = Assignment.objects.filter(status=None,hit=hit)

    # Which of the HITS have time run out, but not submit?
    assignments_attention = []
    assignments_inprogress = []
    for assignment in assignments_remaining:
        needs_attention = False
        if assignment.accept_time != None:
            time_difference = timezone.now() - (assignment.accept_time + timedelta(hours=hit.assignment_duration_in_hours))
            # If the workers ending time has passed:
            if (assignment.accept_time + timedelta(hours=hit.assignment_duration_in_hours)) < timezone.now():
                assignments_attention.append(assignment)
                needs_attention = True

        if needs_attention == False:
            assignments_inprogress.append(assignment)

    context["assignments_inprogress"] = assignments_inprogress
    context["assignments_attention"] = assignments_attention

    return render(request, "turk/manage_hit.html", context)


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 == False:
            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)  <commit_msg>get_object_or_create returns true if object was created, flipped a logic check based on this to prevent credit checks from being called too often in turk.views<commit_after>from datetime import timedelta, datetime
import json
import os
import requests

from expfactory.battery import get_load_static, get_experiment_run
from numpy.random import choice
from optparse import make_option

from django.contrib.auth.decorators import login_required
from django.core.management.base import BaseCommand
from django.http.response import (HttpResponseRedirect, HttpResponseForbidden,
    HttpResponse, Http404, HttpResponseNotAllowed)
from django.core.urlresolvers import reverse
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.utils import get_experiment_type, select_experiments
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,
    get_unique_experiments, check_battery_dependencies)
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)


#### GETS #############################################################

# get experiment
def get_hit(hid,request,mode=None):
    keyargs = {'pk':hid}
    try:
        hit = HIT.objects.get(**keyargs)
    except HIT.DoesNotExist:
        raise Http404
    else:
        return hit

#### VIEWS ############################################################

def get_amazon_variables(request):
    '''get_amazon_variables gets the "GET" variables from the URL,
       returned in a context that can be used across different experiment
       serving functions (local or mturk)
    '''

    # An assignmentID means that the worker has accepted the task
    assignment_id = request.GET.get("assignmentId","")
    worker_id = request.GET.get("workerId","")
    hit_id = request.GET.get("hitId","")
    turk_submit_to = request.GET.get("turkSubmitTo","")

    # worker has not accepted the task
    if assignment_id in ["ASSIGNMENT_ID_NOT_AVAILABLE",""]:
        assignment_id = None

    return {"worker_id": worker_id,
            "assignment_id": assignment_id,
            "hit_id": hit_id,
            "turk_submit_to":turk_submit_to}

@login_required
def manage_hit(request,bid,hid):
    '''manage_hit shows details about workers that have completed / not completed a HIT
    :param hid: the hit id
    '''
    hit =  get_hit(hid,request)
    hit.update()
    battery = hit.battery

    # Get different groups of assignments
    context = dict()
    context["assignments_approved"] = Assignment.objects.filter(status="A",hit=hit)
    context["assignments_rejected"] = Assignment.objects.filter(status="R",hit=hit)
    context["assignments_submit"] = Assignment.objects.filter(status="S",hit=hit)
    context["hit"] = hit
    assignments_remaining = Assignment.objects.filter(status=None,hit=hit)

    # Which of the HITS have time run out, but not submit?
    assignments_attention = []
    assignments_inprogress = []
    for assignment in assignments_remaining:
        needs_attention = False
        if assignment.accept_time != None:
            time_difference = timezone.now() - (assignment.accept_time + timedelta(hours=hit.assignment_duration_in_hours))
            # If the workers ending time has passed:
            if (assignment.accept_time + timedelta(hours=hit.assignment_duration_in_hours)) < timezone.now():
                assignments_attention.append(assignment)
                needs_attention = True

        if needs_attention == False:
            assignments_inprogress.append(assignment)

    context["assignments_inprogress"] = assignments_inprogress
    context["assignments_attention"] = assignments_attention

    return render(request, "turk/manage_hit.html", context)


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)  
{'fraction_non_alphanumeric': 0.10131081981781827, 'fraction_numerical': 0.001777382803821373, 'mean_word_length': 3.4391847468770544, 'pattern_counts': {'":': 10, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4193076', 'n_tokens_mistral': 3900, 'n_tokens_neox': 3756, 'n_words': 946}