def encode_url(url, _s_id, _e_id): adict = {'s_id': _s_id, 'p_id': participant_id, 'g_id': condition_group_id, 'e_id': _e_id, 'URL': url} return '/audio/' + utilities.encrypt_data(adict) + '.wav'
def hearing_test(): """ Determines if the user is eligible to take the hearing test (i.e. has not exceeded `MAX_HEARING_TEST_ATTEMPTS`, and then renders the hearing test, which consists of the assessor counting the tones in two audio files. If caqe.settings.HEARING_TEST_REJECTION_ENABLED is set to False, then pass them through after they had their 2 attempts. Returns ------- flask.Response """ participant = get_current_participant(session) if request.method == 'GET': if participant.hearing_test_attempts >= app.config[ 'MAX_HEARING_TEST_ATTEMPTS']: logger.info('Max hearing test attempts reached - %r' % participant) return render_template( 'sorry.html', message='Sorry. You have exceed the number of allowed attempts. ' 'Please try again tomorrow.') while True: hearing_test_audio_index1 = random.randint( configuration.MIN_HEARING_TEST_AUDIO_INDEX, configuration.MAX_HEARING_TEST_AUDIO_INDEX) hearing_test_audio_index2 = random.randint( configuration.MIN_HEARING_TEST_AUDIO_INDEX, configuration.MAX_HEARING_TEST_AUDIO_INDEX) if hearing_test_audio_index1 != hearing_test_audio_index2: # encrypt the data so that someone can't figure out the pattern on the client side logger.info('Hearing test indices %d and %d assigned to %r' % (hearing_test_audio_index1, hearing_test_audio_index2, participant)) session['hearing_test_audio_index1'] = utilities.encrypt_data( hearing_test_audio_index1) session['hearing_test_audio_index2'] = utilities.encrypt_data( hearing_test_audio_index2) break return render_template('hearing_screening.html') elif request.method == 'POST': try: hearing_test_audio_index1 = session['hearing_test_audio_index1'] hearing_test_audio_index2 = session['hearing_test_audio_index2'] except KeyError as e: hearing_test_audio_index1 = None hearing_test_audio_index2 = None logger.error("Invalid state - %r" % e) if (int(request.form['audiofile1_tones']) == int(int(utilities.decrypt_data(hearing_test_audio_index1)) / configuration.HEARING_TEST_AUDIO_FILES_PER_TONES)) \ and (int(request.form['audiofile2_tones']) == int(int(utilities.decrypt_data(hearing_test_audio_index2)) / configuration.HEARING_TEST_AUDIO_FILES_PER_TONES)): logger.info('Hearing test passed - %r' % participant) participant.set_passed_hearing_test(True) db.session.commit() return pre_evaluation_tasks() else: logger.info('Hearing test failed - %r' % participant) participant.set_passed_hearing_test(False) db.session.commit() if participant.hearing_test_attempts < app.config[ 'MAX_HEARING_TEST_ATTEMPTS']: flash( 'You answered incorrectly. If you are unable to pass this test, it is likely that your output ' 'device (e.g. your headphones) is not producing the full range of frequencies required for this ' 'task. Try using better headphones.', 'danger') else: if not app.config['HEARING_TEST_REJECTION_ENABLED']: # They attempted, but they failed, but pass them through since rejection is not enabled logger.info( 'Hearing test rejection enabled. Passing failed participant to evaluation.' ) return pre_evaluation_tasks() return redirect( url_for('hearing_test', _method='GET', _external=True, _scheme=app.config['PREFERRED_URL_SCHEME']))
def hearing_test(): """ Determines if the user is eligible to take the hearing test (i.e. has not exceeded `MAX_HEARING_TEST_ATTEMPTS`, and then renders the hearing test, which consists of the assessor counting the tones in two audio files. If caqe.settings.HEARING_TEST_REJECTION_ENABLED is set to False, then pass them through after they had their 2 attempts. Returns ------- flask.Response """ participant = get_current_participant(session) if request.method == 'GET': if participant.hearing_test_attempts >= app.config['MAX_HEARING_TEST_ATTEMPTS']: logger.info('Max hearing test attempts reached - %r' % participant) return render_template('sorry.html', message='Sorry. You have exceed the number of allowed attempts. ' 'Please try again tomorrow.') while True: hearing_test_audio_index1 = random.randint(configuration.MIN_HEARING_TEST_AUDIO_INDEX, configuration.MAX_HEARING_TEST_AUDIO_INDEX) hearing_test_audio_index2 = random.randint(configuration.MIN_HEARING_TEST_AUDIO_INDEX, configuration.MAX_HEARING_TEST_AUDIO_INDEX) if hearing_test_audio_index1 != hearing_test_audio_index2: # encrypt the data so that someone can't figure out the pattern on the client side logger.info('Hearing test indices %d and %d assigned to %r' % (hearing_test_audio_index1, hearing_test_audio_index2, participant)) session['hearing_test_audio_index1'] = utilities.encrypt_data(hearing_test_audio_index1) session['hearing_test_audio_index2'] = utilities.encrypt_data(hearing_test_audio_index2) break return render_template('hearing_screening.html') elif request.method == 'POST': try: hearing_test_audio_index1 = session['hearing_test_audio_index1'] hearing_test_audio_index2 = session['hearing_test_audio_index2'] except KeyError as e: hearing_test_audio_index1 = None hearing_test_audio_index2 = None logger.error("Invalid state - %r" % e) if (int(request.form['audiofile1_tones']) == (int(utilities.decrypt_data(hearing_test_audio_index1)) / configuration.HEARING_TEST_AUDIO_FILES_PER_TONES)) \ and (int(request.form['audiofile2_tones']) == (int(utilities.decrypt_data(hearing_test_audio_index2)) / configuration.HEARING_TEST_AUDIO_FILES_PER_TONES)): logger.info('Hearing test passed - %r' % participant) participant.set_passed_hearing_test(True) db.session.commit() return pre_evaluation_tasks() else: logger.info('Hearing test failed - %r' % participant) participant.set_passed_hearing_test(False) db.session.commit() if participant.hearing_test_attempts < app.config['MAX_HEARING_TEST_ATTEMPTS']: flash('You answered incorrectly. If you are unable to pass this test, it is likely that your output ' 'device (e.g. your headphones) is not producing the full range of frequencies required for this ' 'task. Try using better headphones.', 'danger') else: if not app.config['HEARING_TEST_REJECTION_ENABLED']: # They attempted, but they failed, but pass them through since rejection is not enabled logger.info('Hearing test rejection enabled. Passing failed participant to evaluation.') return pre_evaluation_tasks() return redirect(url_for('hearing_test', _method='GET', _external=True, _scheme=app.config['PREFERRED_URL_SCHEME']))