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')
def create_dummy_study(object_id): s = DStudy( name="Old Unnamed Study %s" % object_id, encryption_key="0"*32, object_id=str(object_id), deleted=True, ) s.save() study_id_dict[object_id] = {"pk": s.id} return s
def test_get_all_studies_by_name(self): study_names = ['My studies', 'MY STUDY', 'my_study', 'your study'] encryption_key = 'aabbccddeeffgghhiijjkkllmmnnoopp' for name in study_names: good_study = Study.create_with_object_id(name=name, encryption_key=encryption_key) self.assertIn(good_study, Study.get_all_studies_by_name()) self.assertEqual(list(Study.get_all_studies_by_name().values_list('name', flat=True)), study_names) bad_study = Study.create_with_object_id(name='name', encryption_key=encryption_key, deleted=True) self.assertNotIn(bad_study, Study.get_all_studies_by_name())
def test_settings_mongo_integrity(self): study = Study(**self.translated_reference_study) study.save() mongo_reference_settings = self.translated_reference_device_settings mongo_reference_settings['study'] = study django_settings = DeviceSettings(**mongo_reference_settings) x = compare_dictionaries(mongo_reference_settings, django_settings.as_dict(), ignore=['deleted', 'id']) self.assertTrue(x)
def test_participant_mongo_integrity(self): study = Study(**self.translated_reference_study) study.save() reference_participant = self.translated_reference_participant django_participant = Participant(study=study, **reference_participant).as_native_python() x = compare_dictionaries(reference_participant, django_participant, ignore=['id', 'deleted']) self.assertTrue(x)
def test_survey_mongo_integrity(self): study = Study(**self.translated_reference_study) study.save() mongo_reference_survey = self.translated_reference_survey mongo_reference_survey['study'] = study django_reference_survey = Survey(study=study, **self.translated_reference_survey) # This comparison requires the as_dict comparison because it has jsonfields x = compare_dictionaries(mongo_reference_survey, django_reference_survey.as_dict(), ignore=['deleted', 'id', 'last_modified']) self.assertTrue(x)
def test_study_create_with_object_id(self): self.assertEqual(Study.objects.count(), 0) self.assertEqual(DeviceSettings.objects.count(), 0) study_name = 'my study' encryption_key = 'aabbccddeeffgghhiijjkkllmmnnoopp' Study.create_with_object_id(name=study_name, encryption_key=encryption_key) new_study = Study.objects.get() new_ds = DeviceSettings.objects.get() self.assertEqual(Study.objects.count(), 1) self.assertEqual(DeviceSettings.objects.count(), 1) self.assertEqual(new_study.name, study_name) self.assertEqual(new_study.encryption_key, encryption_key) self.assertEqual(len(new_study.object_id), 24) self.assertEqual(new_study.device_settings, new_ds) self.assertFalse(new_study.deleted)
def edit_researcher(researcher_pk): researcher = Researcher.objects.get(pk=researcher_pk) admin_is_current_user = (researcher.username == session['admin_username']) current_studies = Study.get_all_studies_by_name().filter( researchers=researcher) return render_template( 'edit_researcher.html', admin=researcher, current_studies=current_studies, all_studies=Study.get_all_studies_by_name(), allowed_studies=get_admins_allowed_studies(), admin_is_current_user=admin_is_current_user, system_admin=admin_is_system_admin(), redirect_url='/edit_researcher/{:s}'.format(researcher_pk), )
def manage_studies(): studies = [ study.as_native_python() for study in Study.get_all_studies_by_name() ] return render_template('manage_studies.html', studies=json.dumps(studies), allowed_studies=get_admins_allowed_studies(), system_admin=admin_is_system_admin())
def manage_researchers(): researcher_list = [] for researcher in Researcher.get_all_researchers_by_username(): allowed_studies = Study.get_all_studies_by_name().filter( researchers=researcher).values_list('name', flat=True) researcher_list.append( (researcher.as_native_python(), list(allowed_studies))) return render_template('manage_researchers.html', admins=json.dumps(researcher_list), allowed_studies=get_admins_allowed_studies(), system_admin=admin_is_system_admin())
def get_admins_allowed_studies(as_json=True): """ Return a list of studies which the currently logged-in researcher is authorized to view and edit. """ researcher = Researcher.objects.get(username=session['admin_username']) study_set = [ study for study in Study.get_all_studies_by_name().filter(researchers=researcher) .values("name", "object_id", "id", "is_test") ] if as_json: return json.dumps(study_set) else: return study_set
def migrate_studies(): d_study_list = [] for m_study in MStudySet.iterator(): with error_handler: # Create a Django Study object modeled off the Mongolia Study study_name = m_study['name'] d_study = DStudy( name=study_name, encryption_key=m_study['encryption_key'], object_id=m_study['_id'], deleted=m_study['deleted'], is_test=m_study['is_test'], ) # Validate the new Study object and add it to the bulk create list d_study.full_clean() d_study_list.append(d_study) # Get lists of Mongolia Surveys, Admins and StudyDeviceSettings attached to this Study m_survey_list = m_study['surveys'] m_admin_list = m_study['admins'] m_device_settings = m_study['device_settings'] study_referents[study_name] = { 'survey_list': m_survey_list, 'admin_list': m_admin_list, 'device_settings': m_device_settings, } # Bulk create the Django Studies DStudy.objects.bulk_create(d_study_list) # Create a reference from Mongolia Study IDs to Django Studies that doesn't require # any future database calls. for m_study in MStudySet.iterator(): with error_handler: m_study_id = m_study['_id'] d_study_id = DStudy.objects.filter(name=m_study['name']).values('pk', 'deleted').get() study_id_dict[m_study_id] = d_study_id
def choose_study(): allowed_studies = get_admins_allowed_studies_as_query_set() # If the admin is authorized to view exactly 1 study, redirect to that study if allowed_studies.count() == 1: return redirect('/view_study/{:d}'.format( allowed_studies.values_list('pk', flat=True).get())) # Otherwise, show the "Choose Study" page allowed_studies_json = Study.query_set_as_native_json(allowed_studies) return render_template('choose_study.html', studies=allowed_studies_json, allowed_studies=allowed_studies_json, system_admin=admin_is_system_admin())
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)
def get_visible_studies_by_name(self): if self.site_admin: from database.models import Study return Study.get_all_studies_by_name() else: return self.get_researcher_studies_by_name()
def get_researcher_studies_by_name(self): from database.models import Study return Study.get_researcher_studies_by_name(self)
def get_administered_studies_by_name(self): from database.models import Study return Study._get_administered_studies_by_name(self)
def get_admins_allowed_studies_as_query_set(): researcher = Researcher.objects.get(username=session['admin_username']) return Study.get_all_studies_by_name().filter(researchers=researcher)
def test_study_mongo_integrity(self): django_reference_study = Study(**self.translated_reference_study) x = compare_dictionaries(self.translated_reference_study, django_reference_study.as_native_python(), ignore=['id']) self.assertTrue(x)