def _get_study_or_abort_404(study_object_id, study_pk): if study_object_id: # If the ID is incorrectly sized, we return a 400 if not is_object_id(study_object_id): print("Received invalid length objectid as study_id in the data access API.") return abort(400) # If no Study with the given ID exists, we return a 404 try: study = Study.objects.get(object_id=study_object_id) except Study.DoesNotExist: print("study '%s' does not exist." % study_object_id) return abort(404) else: return study elif study_pk: # If no Study with the given ID exists, we return a 404 try: study = Study.objects.get(pk=study_pk) except Study.DoesNotExist: print("study '%s' does not exist." % study_pk) return abort(404) else: return study else: return abort(400)
def get_users_in_study(): study_object_id = request.values.get("study_id", "") # if not is_object_id(study_object_id): if not is_object_id(study_object_id): return abort(404) try: study = Study.objects.get(object_id=study_object_id) except Study.DoesNotExist: return abort(404) get_and_validate_researcher(study) return json.dumps(list(study.participants.values_list('patient_id', flat=True)))
def create_from_file(cls, json_file): with open(json_file, 'r') as json_fd: study_configuration = json.load(json_fd) if 'STUDY_OBJECT_ID' not in study_configuration: raise ValueError( 'Configuration file {0} does not contain a STUDY_OBJECT_ID key or value' .format(json_file)) # check to see if the config alread exists, if it does, delete it and then re-add study_configs = cls.objects.exclude(deleted=True).filter( study__object_id=study_configuration['STUDY_OBJECT_ID']) for study_config in study_configs.all(): print( 'Found an existing study configuration for {0}, replacing with contents of file {1}' .format(study_configuration['STUDY_OBJECT_ID'], json_file)) study_config.mark_deleted() # connect to the database to get a list of data if not is_object_id(study_configuration['STUDY_OBJECT_ID']): raise ValueError('{0} is not a correct study object id'.format( study_configuration['STUDY_OBJECT_ID'])) try: print('Looking for study {0}'.format( study_configuration['STUDY_OBJECT_ID'])) study = Study.objects.get( object_id=study_configuration['STUDY_OBJECT_ID']) except Study.DoesNotExist: print('Study {0} does not exist.'.format( study_configuration['STUDY_OBJECT_ID'])) raise return cls.objects.create(**{ 'study': study, 'study_configuration': study_configuration })
def _get_study_or_abort_404(study_object_id, study_pk): if study_object_id: # If the ID is incorrectly sized, we return a 400 if not is_object_id(study_object_id): return abort(400) # If no Study with the given ID exists, we return a 404 try: study = Study.objects.get(object_id=study_object_id) except Study.DoesNotExist: return abort(404) else: return study elif study_pk: # If no Study with the given ID exists, we return a 404 try: study = Study.objects.get(pk=study_pk) except Study.DoesNotExist: return abort(404) else: return study else: return abort(400)