fredcallaway / heroku-experiment

Starter kit for running a psiturk experiment on heroku with jspsych.
http://salty-meadow-30207.herokuapp.com/
MIT License
7 stars 9 forks source link

Add support for `endhit` and `status` update for Prolific #9

Open cgc opened 3 years ago

cgc commented 3 years ago

May be worth considering how to write to endhit and status from Prolific, since those variables are part of the logic for counterbalancing. Code here.

fredcallaway commented 3 years ago

I did this in a separate project by adding a function in custom.py

@custom_code.route('/complete_exp', methods=['POST'])
def complete_exp():
    if not 'uniqueId' in request.form:
        raise ExperimentError('improper_inputs')
    unique_id = request.form['uniqueId']

    current_app.logger.info("completed experimente")
    try:
        user = Participant.query.\
            filter(Participant.uniqueid == unique_id).one()
        user.status = COMPLETED
        user.endhit = datetime.datetime.now()
        db_session.add(user)
        db_session.commit()
        resp = {"status": "success"}
    except exc.SQLAlchemyError:
        current_app.logger.error("DB error: Unique user not found.")
        resp = {"status": "error, uniqueId not found"}
    return jsonify(**resp)

and then added a

await $.ajax("complete_exp", {
    type: "POST",
    data: {uniqueId}
});

in the final completion function (which is async). I know you had a different solution though. Do you think yours might be better?