Example #1
0
def get_previous_worker_code(job_id, worker_id, treatment):
    """
    Generate a code for the user in case he already took the main task
    """
    resp_table = get_table(resp_BASE,
                           job_id=job_id,
                           schema="result",
                           treatment=treatment)
    prop_table = get_table(prop_BASE,
                           job_id=job_id,
                           schema="result",
                           treatment=treatment)
    worker_code = None
    if is_worker_available(worker_id, resp_table):
        worker_code = get_db().execute(
            f"SELECT completion_code from {resp_table} where worker_id=?",
            [worker_id]).fetchone()[0]
        if not worker_code:
            worker_code = generate_completion_code(resp_BASE, job_id)
    if is_worker_available(worker_id, prop_table):
        worker_code = get_db().execute(
            f"SELECT completion_code from {prop_table} where worker_id=?",
            [worker_id]).fetchone()[0]
        if not worker_code:
            worker_code = generate_completion_code(prop_BASE, job_id)
    return worker_code
Example #2
0
def handle_done(treatment, template=None, no_features=None):
    app.logger.debug("handle_done")
    if template is None:
        template = f"txx/resp.done.html"

    if no_features is None:
        completion_code_base = BASE
    else:
        # survey should not require tasks
        completion_code_base = BASE + "NF"
    cookie_obj = get_cookie_obj(BASE)
    worker_code_key = f"{BASE}_worker_code"
    if not cookie_obj.get(BASE, None):
        flash("Sorry, you are not allowed to use this service. ^_^")
        return render_template("error.html")
    if not cookie_obj.get(worker_code_key):
        job_id = cookie_obj["job_id"]
        worker_code = generate_completion_code(completion_code_base, job_id)
        response = cookie_obj["response"]
        response["completion_code"] = worker_code
        worker_id = cookie_obj["worker_id"]
        resp_result = resp_to_resp_result(response,
                                          job_id=job_id,
                                          worker_id=worker_id)
        try:
            #save_resp_result(TUBE_RES_FILENAME, resp_result)
            save_result2file(
                get_output_filename(BASE, job_id, treatment=treatment),
                resp_result)
        except Exception as err:
            app.log_exception(err)
        try:
            #save_resp_result2db(get_db("RESULT"), resp_result, job_id)
            save_result2db(table=get_table(base=BASE,
                                           job_id=job_id,
                                           schema="result",
                                           treatment=treatment),
                           response_result=resp_result,
                           unique_fields=["worker_id"])
            increase_worker_bonus(job_id=job_id,
                                  worker_id=worker_id,
                                  bonus_cents=0)
        except Exception as err:
            app.log_exception(err)
        cookie_obj.clear()

        cookie_obj[BASE] = True
        cookie_obj["worker_id"] = worker_id
        cookie_obj[worker_code_key] = worker_code

        auto_finalize = cookie_obj.get("auto_finalize",
                                       request.args.get("auto_finalize"))
        if auto_finalize and no_features:  # and base=="hexaco":
            #NOTE: there is an import here ^_^
            finalize_resp(job_id, worker_id, treatment)

    req_response = make_response(
        render_template(template, worker_code=cookie_obj[worker_code_key]))
    set_cookie_obj(req_response, BASE, cookie_obj)
    return req_response
Example #3
0
def handle_done(treatment, template=None, response_to_result_func=None):
    app.logger.debug("handle_done")
    if template is None:
        template = f"txx/{BASE}.done.html"
    if response_to_result_func is None:
        response_to_result_func = prop_to_prop_result
    worker_code_key = f"{BASE}_worker_code"
    worker_bonus_key = f"{BASE}_worker_bonus"
    cookie_obj = get_cookie_obj(BASE)
    ai_cookie_obj = get_cookie_obj(AI_COOKIE_KEY)
    if not cookie_obj.get(BASE, None):
        flash("Sorry, you are not allowed to use this service. ^_^")
        return render_template("error.html")
    if not (cookie_obj.get("worker_code")):
        job_id = cookie_obj["job_id"]
        worker_code = generate_completion_code(base=BASE, job_id=job_id)
        proposal = cookie_obj["proposal"]
        proposal["completion_code"] = worker_code
        proposal.update(ai_cookie_obj.get("ai_proposal", {}))
        row_info = cookie_obj["row_info"]
        worker_id = cookie_obj["worker_id"]
        row_info = get_row(get_db(), job_id, worker_id, TREATMENT)
        offer = proposal["offer_dss"]

        min_offer = row_info["min_offer"]
        worker_bonus = 0
        if offer >= min_offer:
            worker_bonus = 5
        prop_result = response_to_result_func(proposal,
                                              job_id=job_id,
                                              worker_id=worker_id,
                                              row_data=row_info)
        prop_result["worker_bonus"] = worker_bonus
        try:
            save_result2file(
                get_output_filename(base=BASE,
                                    job_id=job_id,
                                    treatment=treatment), prop_result)
        except Exception as err:
            app.log_exception(err)
        try:
            save_result2db(table=get_table(base=BASE,
                                           job_id=job_id,
                                           schema="result",
                                           treatment=treatment),
                           response_result=prop_result,
                           unique_fields=["worker_id"])
            #increase_worker_bonus(job_id=job_id, worker_id=worker_id, bonus_cents=worker_bonus, con=get_db("DB"))
        except Exception as err:
            app.log_exception(err)
        cookie_obj.clear()

        cookie_obj[BASE] = True
        cookie_obj["worker_id"] = worker_id
        cookie_obj[worker_bonus_key] = cents_repr(worker_bonus)
        cookie_obj[worker_code_key] = worker_code
    req_response = make_response(
        render_template(template, worker_code=cookie_obj[worker_code_key]))
    set_cookie_obj(req_response, BASE, cookie_obj)
    return req_response
Example #4
0
def handle_survey_feedback_done(template=None):
    """ Only used for treatment t10_feedback"""
    if template is None:
        template = "txx/survey.done.html"
    cookie_obj = get_cookie_obj(BASE)
    if not cookie_obj.get(BASE):
        flash("Sorry, you are not allowed to use this service. ^_^")
        return render_template("error.html")
    worker_code_key = f"{BASE}_worker_code"
    worker_code = WORKER_CODE_DROPPED
    if True or not cookie_obj.get(worker_code_key):
        job_id = cookie_obj["job_id"]
        worker_id = cookie_obj["worker_id"]
        treatment = cookie_obj["treatment"]
        assignment_id = cookie_obj.get("assignment_id")

        worker_code = generate_completion_code(base=treatment, job_id=job_id)
        response = cookie_obj["response"]
        app.logger.debug("DONE_response: {response}")
        dropped = response.get("drop")
        if dropped == "1":
            worker_code = WORKER_CODE_DROPPED
            flash(
                f"You have been disqualified as you made more than {MAXIMUM_CONTROL_MISTAKES} mistakes on the control questions. Close this window and don't submit any completion on MTurk to avoid getting a REJECTION."
            )
        response["worker_id"] = worker_id
        response["job_id"] = job_id
        response["assignment_id"] = assignment_id
        response["completion_code"] = worker_code
        result = {
            k: v
            for k, v in response.items() if k not in {'csrf_token', 'drop'}
        }
        try:
            save_result2file(
                get_output_filename(base=BASE,
                                    job_id=job_id,
                                    treatment=treatment), result)
        except Exception as err:
            app.log_exception(err)
        try:
            save_result2db(table=get_table(base=BASE,
                                           job_id=job_id,
                                           schema="result",
                                           treatment=treatment),
                           response_result=result,
                           unique_fields=["worker_id"])
        except Exception as err:
            app.log_exception(err)

        adapter = get_adapter_from_dict(cookie_obj["adapter"])
        #adapter = get_adapter()
        app.logger.debug(f"adapter: {cookie_obj['adapter']}")
        cookie_obj.clear()

        #save_worker_id(job_id=job_id, worker_id=worker_id, worker_code=worker_code, assignment_id=adapter.assignment_id)

        app.logger.debug(
            f"request-args: {request.args}, adapter: {adapter.to_dict()} ")
        try:
            expected_max_judgments = request.args.get("expected_max_judgments")
            if expected_max_judgments is not None:
                expected_max_judgments = int(expected_max_judgments)
                api = adapter.get_api()
                max_judgments = api.get_max_judgments()
                app.logger.debug(
                    f"survey.done: max_judgments: {max_judgments} - {type(max_judgments)}, expected_max_judgments: {expected_max_judgments} - {type(expected_max_judgments)}"
                )
                if max_judgments < expected_max_judgments:
                    app.logger.debug(f"try create new assignments")
                    create_res = api.create_additional_assignments(1)
                    app.logger.debug(
                        f"post assignment creation: new-max: {api.get_max_judgments()} , mturk-api-res: {create_res}"
                    )
        except Exception as err:
            app.log_exception(err)

        app.logger.info(
            f"handle_survey_feedback_done: saved new survey - job_id: {job_id}, worker_id: {worker_id}"
        )
    req_response = make_response(
        render_template(template, worker_code=worker_code, dropped=True))
    set_cookie_obj(req_response, BASE, cookie_obj)
    for cookie in session.get(ALL_COOKIES_KEY, []):
        req_response.set_cookie(cookie, expires=0)
    session[ALL_COOKIES_KEY] = []
    return req_response
Example #5
0
def handle_done_no_prop(treatment, template=None, no_features=None):
    app.logger.debug("handle_done")
    if template is None:
        template = f"txx/resp.done.html"
    if no_features is None:
        completion_code_base = BASE
    else:
        # survey should not require tasks
        completion_code_base = BASE + "NF"

    cookie_obj = get_cookie_obj(BASE)
    worker_code_key = f"{BASE}_worker_code"
    if not cookie_obj.get(BASE, None):
        flash("Sorry, you are not allowed to use this service. ^_^")
        return render_template("error.html")
    if not cookie_obj.get(worker_code_key):
        job_id = cookie_obj["job_id"]
        worker_code = generate_completion_code(completion_code_base, job_id)
        response = cookie_obj["response"]
        response["completion_code"] = worker_code
        worker_id = cookie_obj["worker_id"]
        resp_result = resp_to_resp_result(response,
                                          job_id=job_id,
                                          worker_id=worker_id)
        try:
            #save_resp_result(TUBE_RES_FILENAME, resp_result)
            save_result2file(
                get_output_filename(BASE, job_id, treatment=treatment),
                resp_result)
        except Exception as err:
            app.log_exception(err)

        bonus_cents = 0
        row_id = None
        prop_row = {}
        try:
            prop_row = get_row_ignore_job(get_db(), job_id, worker_id,
                                          treatment)
            offer = prop_row.get("offer_final", prop_row["offer"])
            row_id = prop_row.get(PK_KEY)
            if offer >= response.get("min_offer_final", response["min_offer"]):
                bonus_cents = MAX_GAIN - offer
            else:
                bonus_cents = 0
        except Exception as err:
            app.log_exception(err)
        try:
            #save_resp_result2db(get_db("RESULT"), resp_result, job_id)
            save_result2db(table=get_table(base=BASE,
                                           job_id=job_id,
                                           schema="result",
                                           treatment=treatment),
                           response_result=resp_result,
                           unique_fields=["worker_id"])
            increase_worker_bonus(job_id=job_id,
                                  worker_id=worker_id,
                                  bonus_cents=bonus_cents)
            close_row(get_db(), job_id, row_id, treatment)

            prop_result = {
                k: v
                for k, v in resp_result.items() if k not in SKIP_RESP_KEYS
            }
            prop_result = {
                k: (v if "feedback" not in k and "time" not in k else None)
                for k, v in prop_result.items()
            }
            prop_result["resp_worker_id"] = worker_id
            prop_result["worker_id"] = prop_row["prop_worker_id"]
            prop_result.update(prop_row)
            save_result2db(table=get_table(base="prop",
                                           job_id=job_id,
                                           schema="result",
                                           treatment=treatment),
                           response_result=prop_result,
                           unique_fields=["worker_id"])
        except Exception as err:
            app.log_exception(err)
        cookie_obj.clear()
        cookie_obj[BASE] = True
        cookie_obj["worker_id"] = worker_id
        cookie_obj[worker_code_key] = worker_code

    req_response = make_response(
        render_template(template, worker_code=cookie_obj[worker_code_key]))
    set_cookie_obj(req_response, BASE, cookie_obj)
    return req_response
Example #6
0
def handle_task_done(base, response_to_result_func=None, response_to_bonus=None, numeric_fields=None, unique_fields=None):
    """
    :param base: (str)
    :param response_to_result_func: (func)
    :param response_to_bonus: (func)
    :param numeric_fields: (None| '*' | list)  if '*' all fields are converted to float
    :param unique_fields: (str|list)
    """
    app.logger.debug(f"handle_task_done: {base}")
    worker_code_key = f"{base}_worker_code"
    worker_bonus_key = f"{base}_worker_bonus"
    cookie_obj = get_cookie_obj(base)
    if response_to_result_func is None:
        response_to_result_func = response_to_result
    if not cookie_obj.get(base):
        flash("Sorry, you are not allowed to use this service. ^_^")
        return render_template("error.html")
    if response_to_bonus is None:
        response_to_bonus = lambda args, **kwargs: 0
    if not cookie_obj.get(worker_code_key) or (app.config["DEBUG"] and False):
        job_id = cookie_obj["job_id"]
        worker_code = generate_completion_code(base, job_id)
        response = cookie_obj["response"]
        if numeric_fields is not None:
            if isinstance(numeric_fields, (list, tuple)):
                for field in numeric_fields:
                    try:
                        response[field] = value_to_numeric(response[field])
                    except Exception as err:
                        app.log_exception(err)
            elif numeric_fields == "*":
                for field in response:
                    try:
                        response[field] = value_to_numeric(response[field])
                    except Exception as err:
                        app.log_exception(err)

        worker_id = cookie_obj["worker_id"]
        response["completion_code"] = worker_code
        response_result = response_to_result_func(response, job_id=job_id, worker_id=worker_id)
        worker_bonus = response_to_bonus(response)
        response_result["worker_bonus"] = worker_bonus
        try:
            save_result2file(get_output_filename(base, job_id, is_task=True), response_result)
        except Exception as err:
            app.log_exception(err)
        try:
            #TODO: check later
            save_result2db(get_table(base, job_id=job_id, schema="result", is_task=True), response_result, unique_fields=unique_fields)
            increase_worker_bonus(job_id=job_id, worker_id=worker_id, bonus_cents=worker_bonus)
        except Exception as err:
            app.log_exception(err)
        
        #NOTE: hexaco is the LAST task required from the user!!!
        auto_finalize = cookie_obj.get("auto_finalize")
        if auto_finalize:# and base=="hexaco":
            #NOTE: there is an import here ^_^
            from survey.txx.helpers import finalize_resp
            treatment = cookie_obj.get("treatment")
            finalize_resp(job_id, worker_id, treatment)
            # client = app.test_client()
            # url = url_for(f"{treatment}.webhook", job_id=job_id, worker_id=worker_id, auto_finalize=auto_finalize, _external=False)
            # app.logger.debug(f"WEBHOOK-URL: {url}")
            # response = client.get(url)
            # # response = requests.get(url)
            # # response = make_response(url)
            # if response.status_code != 200:
            #     app.logger.warning(f"handle_task_done: Something went wrong when: auto: {auto_finalize}, base: {base}, resp-status: {response.data}")
        cookie_obj.clear()
        cookie_obj[base] = True
        cookie_obj["worker_id"] = worker_id
        cookie_obj[worker_code_key] = worker_code
        cookie_obj[worker_bonus_key] = worker_bonus
    
    req_response = make_response(render_template("tasks/done.html", worker_code=cookie_obj[worker_code_key], worker_bonus=cents_repr(cookie_obj[worker_bonus_key])))
    set_cookie_obj(req_response, base, cookie_obj)
    return req_response
Example #7
0
def handle_done(treatment,
                template=None,
                response_to_result_func=None,
                ignore_job=None):
    app.logger.debug("handle_done")
    if template is None:
        template = f"txx/{BASE}.done.html"
    if response_to_result_func is None:
        response_to_result_func = prop_to_prop_result
    worker_code_key = f"{BASE}_worker_code"
    worker_bonus_key = f"{BASE}_worker_bonus"
    cookie_obj = get_cookie_obj(BASE)
    ai_cookie_obj = get_cookie_obj(AI_COOKIE_KEY)
    if not cookie_obj.get(BASE, None):
        flash("Sorry, you are not allowed to use this service. ^_^")
        return render_template("error.html")
    if not (cookie_obj.get("worker_code")):
        job_id = cookie_obj["job_id"]
        worker_code = generate_completion_code(base=BASE, job_id=job_id)
        proposal = cookie_obj["proposal"]
        proposal["completion_code"] = worker_code
        proposal.update(ai_cookie_obj.get("ai_proposal", {}))
        row_info = cookie_obj["row_info"]
        worker_id = cookie_obj["worker_id"]
        close_row(get_db("DATA"),
                  job_id,
                  row_info[PK_KEY],
                  treatment=treatment,
                  ignore_job=ignore_job)
        # worker_bonus = gain(int(row_info["min_offer"]), proposal["offer"])
        prop_result = response_to_result_func(proposal,
                                              job_id=job_id,
                                              worker_id=worker_id,
                                              row_data=row_info)
        try:
            save_result2file(
                get_output_filename(base=BASE,
                                    job_id=job_id,
                                    treatment=treatment), prop_result)
        except Exception as err:
            app.log_exception(err)
        try:
            save_result2db(table=get_table(base=BASE,
                                           job_id=job_id,
                                           schema="result",
                                           treatment=treatment),
                           response_result=prop_result,
                           unique_fields=["worker_id"])
            # increase_worker_bonus(job_id=job_id, worker_id=worker_id, bonus_cents=0, con=get_db("DB"))
        except Exception as err:
            app.log_exception(err)
        auto_finalize = request.args.get("auto_finalize")
        if auto_finalize:
            # url = url_for(f"{treatment}.webhook", job_id=job_id, worker_id=worker_id, auto_finalize=auto_finalize)
            # client = app.test_client()
            # client.get(url)
            finalize_round(job_id,
                           prop_worker_id=worker_id,
                           treatment=treatment)

        cookie_obj.clear()

        cookie_obj[BASE] = True
        cookie_obj["worker_id"] = worker_id
        # cookie_obj[worker_bonus_key] = cents_repr(worker_bonus)
        cookie_obj[worker_code_key] = worker_code
    req_response = make_response(
        render_template(template, worker_code=cookie_obj[worker_code_key]))
    set_cookie_obj(req_response, BASE, cookie_obj)
    return req_response