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 patient_fields(study_id, patient_id=None): try: patient = Participant.objects.get(pk=patient_id) except Participant.DoesNotExist: return abort(404) patient.values_dict = {tag.field.field_name: tag.value for tag in patient.field_values.all()} study = patient.study if request.method == 'GET': return render_template( 'view_patient_custom_field_values.html', fields=study.fields.all(), study=study, patient=patient, allowed_studies=get_admins_allowed_studies(), system_admin=admin_is_system_admin(), ) fields = list(study.fields.values_list('field_name', flat=True)) for key, value in request.values.iteritems(): if key in fields: pfv, created = ParticipantFieldValue.objects.get_or_create(participant=patient, field=StudyField.objects.get(study=study, field_name=key)) pfv.value = value pfv.save() return redirect('/view_study/{:d}'.format(study.id))
def pipeline_download_page(): researcher = Researcher.objects.get(username=session['admin_username']) warn_researcher_if_hasnt_yet_generated_access_key(researcher) iteratable_studies = get_admins_allowed_studies(as_json=False) # dict of {study ids : list of user ids} users_by_study = { str(study['id']): [ user.id for user in Participant.objects.filter(study__id=study['id']) ] for study in iteratable_studies } # it is a bit obnoxious to get this data, we need to deduplcate it and then turn it back into a list tags = set() for study in iteratable_studies: for tag in PipelineUploadTags.objects.filter( pipeline_upload__study__id=study['id']).values_list("tag", flat=True): tags.add(tag) tags = [_ for _ in tags] tags.sort() return render_template("data_pipeline_web_form.html", allowed_studies=get_admins_allowed_studies(), downloadable_studies=iteratable_studies, users_by_study=users_by_study, tags=tags, system_admin=admin_is_system_admin())
def view_study(study_id=None): if '/' in study_id: its = study_id.split('/') participant = its[1] study_id = its[0] else: participant = False study = Study.objects.get(pk=study_id) tracking_survey_ids = study.get_survey_ids_and_object_ids_for_study( 'tracking_survey') audio_survey_ids = study.get_survey_ids_and_object_ids_for_study( 'audio_survey') participants = study.participants.all() if participant != False: try: device_id = Participant.objects.get( patient_id=participant).device_id except: device_id = '' response_string = 'Participant with patient_id=%s ' % participant response_string += ('has been registered successfully!' if device_id != '' else 'has NOT been registered!') flash(response_string, 'success') return render_template('view_study.html', study=study, dashboards=parse_dashboards(study), TZ=session["timezone"], patients=participants, audio_survey_ids=audio_survey_ids, tracking_survey_ids=tracking_survey_ids, allowed_studies=get_admins_allowed_studies(), system_admin=admin_is_system_admin())
def download_page(): return render_template( "download_landing_page.html", system_admin=admin_is_system_admin(), allowed_studies=get_admins_allowed_studies(), domain_name=DOMAIN_NAME, )
def device_settings(study_id=None): study = Study(study_id) readonly = not admin_is_system_admin() if request.method == 'GET': settings = study.get_study_device_settings() return render_template("device_settings.html", study=study, settings=settings, readonly=not admin_is_system_admin(), system_admin=admin_is_system_admin()) if readonly: abort(403) settings = study.get_study_device_settings() params = combined_multi_dict_to_dict(request.values) params = checkbox_to_boolean(CHECKBOX_TOGGLES, params) params = string_to_int(TIMER_VALUES, params) settings.update(**params) return redirect('/edit_study/' + str(study._id))
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 choose_study(): allowed_studies = get_admins_allowed_studies() # If the admin is authorized to view exactly 1 study, redirect to that study if len(allowed_studies) == 1: return redirect('/view_study/' + str(allowed_studies[0]._id)) # Otherwise, show the "Choose Study" page return render_template('choose_study.html', allowed_studies=allowed_studies, system_admin=admin_is_system_admin())
def view_study_data_pipeline(study_id=None): study = Study.objects.get(pk=study_id) return render_template( 'data-pipeline.html', study=study, allowed_studies=get_admins_allowed_studies(), system_admin=admin_is_system_admin(), )
def manage_credentials(): username = session['admin_username'] if username == 'moht': flash( "This user is created for downloading APP only, please do not change its password!", 'danger') return redirect('/downloads') else: return render_template('manage_credentials.html', allowed_studies=get_admins_allowed_studies(), system_admin=admin_is_system_admin())
def view_study(study_id=None): study = Study(study_id) tracking_survey_ids = study.get_survey_ids_for_study('tracking_survey') audio_survey_ids = study.get_survey_ids_for_study('audio_survey') return render_template('view_study.html', study=study, patients=Users( study_id = study_id ), audio_survey_ids=audio_survey_ids, tracking_survey_ids=tracking_survey_ids, study_name=study.name, allowed_studies=get_admins_allowed_studies(), system_admin=admin_is_system_admin())
def edit_study(study_id=None): study = Study.objects.get(pk=study_id) all_researchers = Researcher.get_all_researchers_by_username() return render_template( 'edit_study.html', study=study, all_researchers=all_researchers, allowed_studies=get_admins_allowed_studies(), system_admin=admin_is_system_admin(), redirect_url='/edit_study/{:s}'.format(study_id), )
def edit_admin(admin_id): admin = Admin(admin_id) admin_is_current_user = (admin._id == session['admin_username']) current_studies = sorted(Studies(admins=admin._id), key=lambda x: x.name.lower()) return render_template('edit_admin.html', admin=admin, current_studies=current_studies, all_studies=Studies.get_all_studies(), allowed_studies=get_admins_allowed_studies(), admin_is_current_user=admin_is_current_user, system_admin=admin_is_system_admin())
def render_edit_survey(survey_id=None): survey = Survey(survey_id) study = [ study for study in Studies() if survey['_id'] in study['surveys'] ][0] if not survey: return abort(404) return render_template('edit_survey.html', survey=survey, study=study, 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 manage_admins(): admins = [] for admin in Admins(): admin_name = admin._id allowed_studies = ' | '.join( sorted(Studies(admins=admin._id, field='name'), key=lambda x: x.lower())) admins.append((admin_name, allowed_studies)) admins = sorted(admins, key=lambda s: s[0].lower()) return render_template('manage_admins.html', admins=admins, allowed_studies=get_admins_allowed_studies(), system_admin=admin_is_system_admin())
def device_settings(study_id=None): study = Study.objects.get(pk=study_id) readonly = not admin_is_system_admin() if request.method == 'GET': settings = study.get_study_device_settings() return render_template("device_settings.html", study=study.as_native_python(), allowed_studies=get_admins_allowed_studies(), settings=settings.as_native_python(), readonly=not admin_is_system_admin(), system_admin=admin_is_system_admin()) if readonly: abort(403) settings = study.get_study_device_settings() params = combined_multi_dict_to_dict(request.values) params = checkbox_to_boolean(CHECKBOX_TOGGLES, params) params = string_to_int(TIMER_VALUES, params) settings.update(**params) return redirect('/edit_study/{:d}'.format(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 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 data_api_web_form_page(): researcher = Researcher.objects.get(username=session['admin_username']) warn_researcher_if_hasnt_yet_generated_access_key(researcher) allowed_studies = get_admins_allowed_studies_as_query_set() # dict of {study ids : list of user ids} users_by_study = { study.pk: [user.patient_id for user in study.participants.all()] for study in allowed_studies } return render_template("data_api_web_form.html", allowed_studies=get_admins_allowed_studies(), users_by_study=json.dumps(users_by_study), ALL_DATA_STREAMS=ALL_DATA_STREAMS, system_admin=admin_is_system_admin())
def dashboard_page(study_id): study = get_study_or_404(study_id) """ information for the general dashboard view for a study""" participants = list(Participant.objects.filter(study=study_id).values_list("patient_id", flat=True)) return render_template( 'dashboard/dashboard.html', study=study, participants=participants, study_id=study_id, data_stream_dict=complete_data_stream_dict, allowed_studies=get_admins_allowed_studies(), system_admin=admin_is_system_admin(), page_location='dashboard_landing', )
def create_new_researcher(): if request.method == 'GET': return render_template('create_new_researcher.html', allowed_studies=get_admins_allowed_studies(), system_admin=admin_is_system_admin()) admin_id = ''.join(e for e in request.form.get('admin_id') if e.isalnum()) password = request.form.get('password') if Admins(_id=admin_id): flash("There is already a researcher with username " + admin_id, 'danger') return redirect('/create_new_researcher') else: admin = Admin.create(admin_id, password) return redirect('/edit_admin/' + admin._id)
def view_study(study_id=None): study = Study.objects.get(pk=study_id) tracking_survey_ids = study.get_survey_ids_and_object_ids_for_study( 'tracking_survey') audio_survey_ids = study.get_survey_ids_and_object_ids_for_study( 'audio_survey') participants = study.participants.all() return render_template('view_study.html', study=study, patients=participants, audio_survey_ids=audio_survey_ids, tracking_survey_ids=tracking_survey_ids, allowed_studies=get_admins_allowed_studies(), system_admin=admin_is_system_admin())
def manage_processing(): files_to_process_objects = FileToProcess.objects.filter(deleted=False) files_to_process_count = files_to_process_objects.count() files_to_process = [] for fp in files_to_process_objects: files_to_process.append([fp.created_on, fp.study.name, fp.participant.patient_id, fp.s3_file_path]) return render_template( 'manage_processing.html', files_to_process_count=files_to_process_count, files_to_process=files_to_process, allowed_studies=get_admins_allowed_studies(), system_admin=admin_is_system_admin() )
def render_edit_survey(survey_id=None): try: survey = Survey.objects.get(pk=survey_id) except Survey.DoesNotExist: return abort(404) study = survey.study return render_template( 'edit_survey.html', survey=survey.as_native_python(), study=study, allowed_studies=get_admins_allowed_studies(), system_admin=admin_is_system_admin(), domain_name= DOMAIN_NAME, # used in a Javascript alert, see survey-editor.js )
def data_api_web_form_page(): # TODO: Josh, provide access to this route via a link in the top navbar admin = Admin(session['admin_username']) warn_admin_if_hasnt_yet_generated_access_key(admin) allowed_studies = get_admins_allowed_studies() # dict of {study ids : list of user ids} users_by_study = { str(study["_id"]): [user["_id"] for user in Users(study_id=study['_id'])] for study in allowed_studies } return render_template("data_api_web_form.html", allowed_studies=allowed_studies, users_by_study=users_by_study, ALL_DATA_STREAMS=ALL_DATA_STREAMS, system_admin=admin_is_system_admin())
def create_study(): if request.method == 'GET': return render_template('create_study.html', studies=Studies.get_all_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') try: study = Study.create_default_study(name, encryption_key) flash("Successfully created a new study.", 'success') copy_existing_study_if_asked_to(study) return redirect('/device_settings/' + str(study._id)) except (InvalidEncryptionKeyError, StudyAlreadyExistsError) as e: flash(e.message, 'danger') return redirect('/create_study')
def study_fields(study_id=None): study = Study.objects.get(pk=study_id) if request.method == 'GET': return render_template( 'study_custom_fields.html', study=study, fields=study.fields.all(), allowed_studies=get_admins_allowed_studies(), system_admin=admin_is_system_admin(), ) new_field = request.values.get('new_field', None) if new_field: StudyField.objects.get_or_create(study=study, field_name=new_field) return redirect('/study_fields/{:d}'.format(study.id))
def create_new_researcher(): if request.method == 'GET': return render_template('create_new_researcher.html', allowed_studies=get_admins_allowed_studies(), system_admin=admin_is_system_admin()) # Drop any whitespace or special characters from the username username = ''.join(e for e in request.form.get('admin_id', '') if e.isalnum()) password = request.form.get('password', '') if Researcher.objects.filter(username=username).exists(): flash("There is already a researcher with username " + username, 'danger') return redirect('/create_new_researcher') else: researcher = Researcher.create_with_password(username, password) return redirect('/edit_researcher/{:d}'.format(researcher.pk))
def create_new_alias(study_id=None): if request.method == 'GET': return render_template('create_new_alias.html', study_id=study_id, allowed_studies=get_admins_allowed_studies(), system_admin=admin_is_system_admin()) # Drop any whitespace or special characters from the username reference_id = ''.join(e for e in request.form.get('reference_id', '') if e.isalnum()) alias_id = ''.join(e for e in request.form.get('alias_id', '') if e.isalnum()) for participant_id in [reference_id, alias_id]: if not Participant.objects.filter(patient_id=participant_id).exists(): flash( 'ID {0} was not found in the Participant database, alias was not added to database' .format(participant_id), 'danger') return redirect('/create_new_alias/{:d}'.format(int(study_id))) if ParticipantAliases.objects.filter(reference_id=reference_id, alias_id=alias_id).exists(): flash( "There is already an alias {0} => {1}".format( reference_id, alias_id), 'danger') return redirect('/create_new_alias/{:d}'.format(int(study_id))) try: new_alias = ParticipantAliases(study_id=study_id, reference_id=reference_id, alias_id=alias_id) new_alias.save() except: flash( "Error, There is a problem with one or both of the entered IDs ({0}, {1}). They should be 8 character alphanumeric strings." .format(reference_id, alias_id), 'danger') return redirect('/create_new_alias/{:d}'.format(int(study_id))) return redirect('/view_study/{:d}'.format(int(study_id)))