Skip to content

Commit

Permalink
nivturk-v1.2-prolific (6)
Browse files Browse the repository at this point in the history
- add incomplete data saving (#93)
  • Loading branch information
szorowi1 committed Jun 1, 2022
1 parent 3a177f4 commit 975b7e5
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ build/
# project specific
data/**
metadata/**
incomplete/**
reject/**
3 changes: 3 additions & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
if not os.path.isdir(data_dir): os.makedirs(data_dir)
meta_dir = os.path.join(ROOT_DIR, cfg['IO']['METADATA'])
if not os.path.isdir(meta_dir): os.makedirs(meta_dir)
incomplete_dir = os.path.join(ROOT_DIR, cfg['IO']['INCOMPLETE'])
if not os.path.isdir(incomplete_dir): os.makedirs(incomplete_dir)
reject_dir = os.path.join(ROOT_DIR, cfg['IO']['REJECT'])
if not os.path.isdir(reject_dir): os.makedirs(reject_dir)

Expand Down Expand Up @@ -55,6 +57,7 @@ def index():
## Store directories in session object.
session['data'] = data_dir
session['metadata'] = meta_dir
session['incomplete'] = incomplete_dir
session['reject'] = reject_dir
session['allow_restart'] = allow_restart

Expand Down
3 changes: 3 additions & 0 deletions app/app.ini
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,8 @@ METADATA = ../metadata
# Path to data folder [default: ../data]
DATA = ../data

# Path to incomplete data folder [default: ../incomplete]
INCOMPLETE = ../incomplete

# Path to reject folder [default: ../reject]
REJECT = ../reject
23 changes: 23 additions & 0 deletions app/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,29 @@ def pass_message():
## https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
return ('', 200)

@bp.route('/incomplete_save', methods=['POST'])
def incomplete_save():
"""Save incomplete jsPsych dataset to disk."""

if request.is_json:

## Retrieve jsPsych data.
JSON = request.get_json()

## Save jsPsch data to disk.
write_data(session, JSON, method='incomplete')

## Flag partial data saving.
session['MESSAGE'] = 'incomplete dataset saved'
write_metadata(session, ['MESSAGE'], 'a')

## DEV NOTE:
## This function returns the HTTP response status code: 200
## Code 200 signifies the POST request has succeeded.
## For a full list of status codes, see:
## https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
return ('', 200)

@bp.route('/redirect_success', methods = ['POST'])
def redirect_success():
"""Save complete jsPsych dataset to disk."""
Expand Down
2 changes: 2 additions & 0 deletions app/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,7 @@ def write_data(session, json, method='pass'):
fout = os.path.join(session['data'], '%s.json' %session['subId'])
elif method == 'reject':
fout = os.path.join(session['reject'], '%s.json' %session['subId'])
elif method == 'incomplete':
fout = os.path.join(session['incomplete'], '%s.json' %session['subId'])

with open(fout, 'w') as f: f.write(json)
16 changes: 16 additions & 0 deletions app/static/js/nivturk-plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@ function pass_message(msg) {

}

// Save an incomplete dataset.
function incomplete_save() {

$.ajax({
url: "/incomplete_save",
method: 'POST',
data: JSON.stringify(jsPsych.data.get().json()),
contentType: "application/json; charset=utf-8",
}).done(function(data, textStatus, jqXHR) {
// do nothing
}).fail(function(error) {
// do nothing
});

}

// Successful completion of experiment: redirect with completion code.
function redirect_success(workerId, assignmentId, hitId, code_success) {

Expand Down
1 change: 1 addition & 0 deletions app/templates/experiment.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
function verify_unload(e){
e.preventDefault();
(e || window.event).returnValue = null;
incomplete_save();
return null;
};
window.addEventListener("beforeunload", verify_unload);
Expand Down

0 comments on commit 975b7e5

Please sign in to comment.