Example #1
0
def create_study():
    if request.method == 'GET':
        studies = [
            study.as_native_python()
            for study in Study.get_all_studies_by_name()
        ]
        return render_template('create_study.html',
                               studies=json.dumps(studies),
                               allowed_studies=get_admins_allowed_studies(),
                               system_admin=admin_is_system_admin())

    name = request.form.get('name', '')
    encryption_key = request.form.get('encryption_key', '')

    is_test = request.form.get(
        'is_test') == 'true'  # 'true' -> True, 'false' -> False
    try:
        study = Study.create_with_object_id(name=name,
                                            encryption_key=encryption_key,
                                            is_test=is_test)
        copy_existing_study_if_asked_to(study)
        flash('Successfully created study {}.'.format(name), 'success')
        return redirect('/device_settings/{:d}'.format(study.pk))
    except ValidationError as ve:
        for field, message in ve.message_dict.iteritems():
            flash('{}: {}'.format(field, message[0]), 'danger')
        return redirect('/create_study')
Example #2
0
    def test_study_validation(self):
        study_name = 'my study'
        good_encryption_key = 'aabbccddeeffgghhiijjkkllmmnnoopp'
        short_encryption_key = 'aabbccddeeffgghhiijjkkllmm'
        long_encryption_key = 'aabbccddeeffgghhiijjkkllmmnnooppqqrrsstt'
        with self.assertRaises(ValidationError):
            Study.create_with_object_id(name=study_name, encryption_key=short_encryption_key)
        with self.assertRaises(ValidationError):
            Study.create_with_object_id(name=study_name, encryption_key=long_encryption_key)

        bad_object_id = 'I am too long to be an ObjectID'
        with self.assertRaises(ValidationError):
            Study.objects.create(name=study_name, encryption_key=good_encryption_key, object_id=bad_object_id)

        Study.create_with_object_id(name=study_name, encryption_key=good_encryption_key)
        with self.assertRaises(ValidationError):
            Study.create_with_object_id(name=study_name, encryption_key=good_encryption_key)