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 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 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 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 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_admins_allowed_studies_as_query_set(): researcher = Researcher.objects.get(username=session['admin_username']) return Study.get_all_studies_by_name().filter(researchers=researcher)