Ejemplo n.º 1
0
def get_experiment_html(experiment,
                        experiment_folder,
                        url_prefix="",
                        deployment="local"):
    '''get_experiment_html
    return the html template to test a single experiment
    :param experiment: the loaded config.json for an experiment (json)
    :param experiment_folder: the experiment folder, needed for reading in a survey
    :param url_prefix: prefix to put before paths, in case of custom deployment
    :param deployment: deployment environment, one of docker, docker-preview, or local [default]
    '''

    css, js = get_stylejs(experiment, url_prefix)
    validation = ""

    # JsPsych experiment
    if experiment[0]["template"] in ["jspsych"]:
        html = ""
        runcode = get_experiment_run(
            experiment, deployment=deployment)[experiment[0]["exp_id"]]
        template_base = "experiment"

    # HTML survey
    elif experiment[0]["template"] in ["survey"]:
        html, validation = generate_survey(experiment, experiment_folder)
        runcode = ""
        template_base = "survey"

    # Phaser game
    elif experiment[0]["template"] in ["phaser"]:
        html = ""
        runcode = experiment[0]["deployment_variables"]["run"]
        template_base = "phaser"

    exp_template = "%s/templates/%s.html" % (get_installdir(), template_base)

    # Make substitutions
    exp_template = "".join(open(exp_template, "r").readlines())
    exp_template = sub_template(exp_template, "{{js}}", js)
    exp_template = sub_template(exp_template, "{{css}}", css)
    exp_template = sub_template(exp_template, "{{run}}", runcode)
    exp_template = sub_template(exp_template, "{{html}}", html)
    exp_template = sub_template(exp_template, "{{validation}}", validation)
    exp_template = sub_template(exp_template, "{{exp_id}}",
                                experiment[0]["exp_id"])
    return exp_template
Ejemplo n.º 2
0
def get_experiment_html(experiment,experiment_folder,url_prefix="",deployment="local"):
    '''get_experiment_html
    return the html template to test a single experiment
    :param experiment: the loaded config.json for an experiment (json)
    :param experiment_folder: the experiment folder, needed for reading in a survey
    :param url_prefix: prefix to put before paths, in case of custom deployment
    :param deployment: deployment environment, one of docker, docker-preview, or local [default]
    '''

    css,js = get_stylejs(experiment,url_prefix)
    validation = ""

    # JsPsych experiment
    if experiment[0]["template"] in ["jspsych"]:
        html = ""
        runcode = get_experiment_run(experiment,deployment=deployment)[experiment[0]["exp_id"]]
        template_base = "experiment"

    # HTML survey
    elif experiment[0]["template"] in ["survey"]:
        html,validation = generate_survey(experiment,experiment_folder)
        runcode = ""
        template_base = "survey"

    # Phaser game
    elif experiment[0]["template"] in ["phaser"]:
        html = ""
        runcode = experiment[0]["deployment_variables"]["run"]
        template_base = "phaser"


    exp_template = "%s/templates/%s.html" %(get_installdir(),template_base)

    # Make substitutions
    exp_template = "".join(open(exp_template,"r").readlines())
    exp_template = sub_template(exp_template,"{{js}}",js)
    exp_template = sub_template(exp_template,"{{css}}",css)
    exp_template = sub_template(exp_template,"{{run}}",runcode)
    exp_template = sub_template(exp_template,"{{html}}",html)
    exp_template = sub_template(exp_template,"{{validation}}",validation)
    exp_template = sub_template(exp_template,"{{exp_id}}",experiment[0]["exp_id"])
    return exp_template
Ejemplo n.º 3
0
def install_experiment_template(experiment, battery, router_url, to_dir):
    '''install_experiment_template generates a survey or jspsych experiment template,
    and is intended to be called from install_experiment_static, which takes care of other
    dependencies like creating experiment folder, etc. The expfactory repos have validation and
    testing before experiments are allowed to be submit, so we don't do extra checks.
    :param experiment: the expdj.apps.experiments.models.Experiment
    :param battery: the expdj.apps.experiments.models.Battery
    :param router_url: the experiment router URL, to direct to on form submit
    :param to_dir: the directory to install to, typically in a battery folder
    '''
    # If the experiment template is special (survey, experiment, game), we need to render the files
    context = dict()
    if experiment.template == "jspsych":
        template = get_template("experiments/serve_battery.html")
        config = load_experiment(to_dir)[0]
        runcode = get_jspsych_init(config)

    if experiment.template == "survey":
        template = get_template("surveys/serve_battery.html")
        config = load_experiment(to_dir)
        runcode, validation = generate_survey(config,
                                              to_dir,
                                              form_action=router_url,
                                              csrf_token=False)
        context["validation"] = validation

    # static path should be replaced with web path
    url_path = "%s/" % (to_dir.replace(MEDIA_ROOT, MEDIA_URL[:-1]))

    # prepare static files paths
    css, js = prepare_header_scripts(config, url_prefix=url_path)

    # Update the context dictionary, render the template
    context["run"] = runcode
    context["css"] = css
    context["js"] = js
    context["form_submit"] = router_url
    context = Context(context)
    # All templates will have the index written
    return template.render(context)
Ejemplo n.º 4
0
def deploy_battery(deployment,battery,experiment_type,context,task_list,template,result,next_page=None,last_experiment=False):
    '''deploy_battery is a general function for returning the final view to deploy a battery, either local or MTurk
    :param deployment: either "docker-mturk" or "docker-local"
    :param battery: models.Battery object
    :param experiment_type: experiments,games,or surveys
    :param context: context, which should already include next_page,
    :param next_page: the next page to navigate to [optional] default is to reload the page to go to the next experiment
    :param task_list: list of models.Experiment instances
    :param template: html template to render
    :param result: the result object, turk.models.Result
    :param last_experiment: boolean if true will redirect the user to a page to submit the result (for surveys)
    '''
    if next_page == None:
        next_page = "javascript:window.location.reload();"
    context["next_page"] = next_page

    # Check the user blacklist status
    try:
        blacklist = Blacklist.objects.get(worker=result.worker,battery=battery)
        if blacklist.active == True:
            return render_to_response("experiments/blacklist.html")
    except:
        pass

    # Get experiment folders
    experiment_folders = [os.path.join(media_dir,experiment_type,x.template.exp_id) for x in task_list]
    context["experiment_load"] = get_load_static(experiment_folders,url_prefix="/")

    # Get code to run the experiment (not in external file)
    runcode = ""

    # Experiments templates
    if experiment_type in ["experiments"]:
        runcode = get_experiment_run(experiment_folders,deployment=deployment)[task_list[0].template.exp_id]
        if result != None:
            runcode = runcode.replace("{{result.id}}",str(result.id))
        runcode = runcode.replace("{{next_page}}",next_page)
    elif experiment_type in ["games"]:
        experiment = load_experiment(experiment_folders[0])
        runcode = experiment[0]["deployment_variables"]["run"]
    elif experiment_type in ["surveys"]:
        experiment = load_experiment(experiment_folders[0])
        resultid = ""
        if result != None:
            resultid = result.id
        runcode,validation = generate_survey(experiment,experiment_folders[0],
                                             form_action="/local/%s/" %resultid,
                                             csrf_token=True)

        # Field will be filled in by browser cookie, and hidden fields are added for data
        csrf_field = '<input type="hidden" name="csrfmiddlewaretoken" value="hello">'
        csrf_field = '%s\n<input type="hidden" name="djstatus" value="FINISHED">' %(csrf_field)
        csrf_field = '%s\n<input type="hidden" name="url" value="chickenfingers">' %(csrf_field)

        runcode = runcode.replace("{% csrf_token %}",csrf_field)
        context["validation"] = validation

        if last_experiment == True:
            context["last_experiment"] = last_experiment

    context["run"] = runcode
    response = render_to_response(template, context)

    # without this header, the iFrame will not render in Amazon
    response['x-frame-options'] = 'this_can_be_anything'
    return response
Ejemplo n.º 5
0
def deploy_battery(deployment,
                   battery,
                   experiment_type,
                   context,
                   task_list,
                   template,
                   result,
                   next_page=None,
                   last_experiment=False):
    '''deploy_battery is a general function for returning the final view to deploy a battery, either local or MTurk
    :param deployment: either "docker-mturk" or "docker-local"
    :param battery: models.Battery object
    :param experiment_type: experiments,games,or surveys
    :param context: context, which should already include next_page,
    :param next_page: the next page to navigate to [optional] default is to reload the page to go to the next experiment
    :param task_list: list of models.Experiment instances
    :param template: html template to render
    :param result: the result object, turk.models.Result
    :param last_experiment: boolean if true will redirect the user to a page to submit the result (for surveys)
    '''
    if next_page == None:
        next_page = "javascript:window.location.reload();"
    context["next_page"] = next_page

    # Check the user blacklist status
    try:
        blacklist = Blacklist.objects.get(worker=result.worker,
                                          battery=battery)
        if blacklist.active == True:
            return render_to_response("experiments/blacklist.html")
    except:
        pass

    # Get experiment folders
    experiment_folders = [
        os.path.join(media_dir, experiment_type, x.template.exp_id)
        for x in task_list
    ]
    context["experiment_load"] = get_load_static(experiment_folders,
                                                 url_prefix="/")

    # Get code to run the experiment (not in external file)
    runcode = ""

    # Experiments templates
    if experiment_type in ["experiments"]:
        runcode = get_experiment_run(
            experiment_folders,
            deployment=deployment)[task_list[0].template.exp_id]
        if result != None:
            runcode = runcode.replace("{{result.id}}", str(result.id))
        runcode = runcode.replace("{{next_page}}", next_page)
    elif experiment_type in ["games"]:
        experiment = load_experiment(experiment_folders[0])
        runcode = experiment[0]["deployment_variables"]["run"]
    elif experiment_type in ["surveys"]:
        experiment = load_experiment(experiment_folders[0])
        resultid = ""
        if result != None:
            resultid = result.id
        runcode, validation = generate_survey(experiment,
                                              experiment_folders[0],
                                              form_action="/local/%s/" %
                                              resultid,
                                              csrf_token=True)

        # Field will be filled in by browser cookie, and hidden fields are added for data
        csrf_field = '<input type="hidden" name="csrfmiddlewaretoken" value="hello">'
        csrf_field = '%s\n<input type="hidden" name="djstatus" value="FINISHED">' % (
            csrf_field)
        csrf_field = '%s\n<input type="hidden" name="url" value="chickenfingers">' % (
            csrf_field)

        runcode = runcode.replace("{% csrf_token %}", csrf_field)
        context["validation"] = validation

        if last_experiment == True:
            context["last_experiment"] = last_experiment

    context["run"] = runcode
    response = render_to_response(template, context)

    # without this header, the iFrame will not render in Amazon
    response['x-frame-options'] = 'this_can_be_anything'
    return response