def get_patient(dataset_id, fhir_store_id, patient_id): # noqa: E501 """Get a FHIR patient Returns the FHIR patient specified # noqa: E501 :param dataset_id: The ID of the dataset :type dataset_id: str :param fhir_store_id: The ID of the FHIR store :type fhir_store_id: str :param patient_id: The ID of the FHIR patient :type patient_id: str :rtype: Patient """ res = None status = None try: resource_name = "datasets/%s/fhirStores/%s/fhir/Patient/%s" % \ (dataset_id, fhir_store_id, patient_id) db_patient = DbPatient.objects.get(resourceName=resource_name) res = Patient.from_dict(db_patient.to_dict()) status = 200 except DoesNotExist: status = 404 res = Error("The specified resource was not found", status) except Exception as error: status = 500 res = Error("Internal error", status, str(error)) return res, status
def delete_annotation(dataset_id, annotation_store_id, annotation_id): # noqa: E501 """Delete an annotation Deletes the annotation specified # noqa: E501 :param dataset_id: The ID of the dataset :type dataset_id: str :param annotation_store_id: The ID of the annotation store :type annotation_store_id: str :param annotation_id: The ID of the annotation :type annotation_id: str :rtype: EmptyObject """ res = None status = None try: annotation_name = "datasets/%s/annotationStores/%s/annotations/%s" % \ (dataset_id, annotation_store_id, annotation_id) db_annotation = DbAnnotation.objects.get(name=annotation_name) db_annotation.delete() res = {} status = 200 except DoesNotExist: status = 404 res = Error("The specified resource was not found", status) except Exception as error: status = 500 res = Error("Internal error", status, str(error)) return res, status
def delete_dataset(dataset_id): # noqa: E501 """Delete a dataset by ID Deletes the dataset for a given ID # noqa: E501 :param dataset_id: The ID of the dataset :type dataset_id: str :rtype: EmptyObject """ res = None status = None try: dataset_name = "datasets/%s" % (dataset_id, ) db_dataset = DbDataset.objects.get(name=dataset_name) # delete resources in the dataset stores = list_annotation_stores(dataset_id)[0] for store in stores.annotation_stores: delete_annotation_store_by_name(store.name) stores = list_fhir_stores(dataset_id)[0] for store in stores.fhir_stores: delete_fhir_store_by_name(store.name) db_dataset.delete() res = {} status = 200 except DoesNotExist: status = 404 res = Error("The specified resource was not found", status) except Exception as error: status = 500 res = Error("Internal error", status, str(error)) return res, status
def get_annotation_store(dataset_id, annotation_store_id): # noqa: E501 """Get an annotation store Returns the annotation store specified # noqa: E501 :param dataset_id: The ID of the dataset :type dataset_id: str :param annotation_store_id: The ID of the annotation store :type annotation_store_id: str :rtype: AnnotationStore """ res = None status = None try: store_name = "datasets/%s/annotationStores/%s" % ( dataset_id, annotation_store_id) # noqa: E501 db_annotation_store = DbAnnotationStore.objects.get(name=store_name) res = AnnotationStore.from_dict(db_annotation_store.to_dict()) status = 200 except DoesNotExist: status = 404 res = Error("The specified resource was not found", status) except Exception as error: status = 500 res = Error("Internal error", status, str(error)) return res, status
def create_dataset(dataset_id): # noqa: E501 """Create a dataset Create a dataset with the name specified # noqa: E501 :param dataset_id: The ID of the dataset that is being created :type dataset_id: str :rtype: DatasetCreateResponse """ res = None status = None try: try: dataset_name = "datasets/%s" % (dataset_id, ) dataset = Dataset(name=dataset_name) except Exception as error: status = 400 res = Error("Invalid input", status, str(error)) return res, status try: db_dataset = DbDataset(name=dataset.name).save() dataset = Dataset.from_dict(db_dataset.to_dict()) res = DatasetCreateResponse(name=dataset.name) status = 201 except NotUniqueError as error: status = 409 res = Error("Conflict", status, str(error)) except Exception as error: status = 500 res = Error("Internal error", status, str(error)) return res, status
def delete_patient(dataset_id, fhir_store_id, patient_id): # noqa: E501 """Delete a FHIR patient Deletes the FHIR patient specified # noqa: E501 :param dataset_id: The ID of the dataset :type dataset_id: str :param fhir_store_id: The ID of the FHIR store :type fhir_store_id: str :param patient_id: The ID of the FHIR patient :type patient_id: str :rtype: EmptyObject """ res = None status = None try: store_name = "datasets/%s/fhirStores/%s" % \ (dataset_id, fhir_store_id) resource_name = "%s/fhir/Patient/%s" % \ (store_name, patient_id) delete_notes_by_patient(store_name, patient_id) DbPatient.objects.get(resourceName=resource_name).delete() res = {} status = 200 except DoesNotExist: status = 404 res = Error("The specified resource was not found", status) except Exception as error: status = 500 res = Error("Internal error", status, str(error)) return res, status
def unknownMsgError(msg): error_str = "Unknown message of type [{msg_tag}].".format(msg_tag=msg.tag) code = 404 err = Error(code, error_str) r_msg = CreateResponseMsg.create_msg(tag=message_tags['ERROR']['tag']) r_msg.data = err.to_str() return r_msg
def get_note(dataset_id, fhir_store_id, note_id): # noqa: E501 """Get a note Returns the note specified # noqa: E501 :param dataset_id: The ID of the dataset :type dataset_id: str :param fhir_store_id: The ID of the FHIR store :type fhir_store_id: str :param note_id: The ID of the note :type note_id: str :rtype: Note """ res = None status = None try: resource_name = "datasets/%s/fhirStores/%s/fhir/Note/%s" % \ (dataset_id, fhir_store_id, note_id) db_note = DbNote.objects.get(resourceName=resource_name) res = Note.from_dict(db_note.to_dict()) status = 200 except DoesNotExist: status = 404 res = Error("The specified resource was not found", status) except Exception as error: status = 500 res = Error("Internal error", status, str(error)) return res, status
def create_note(dataset_id, fhir_store_id, note_id): # noqa: E501 """Create a note Create a note # noqa: E501 :param dataset_id: The ID of the dataset :type dataset_id: str :param fhir_store_id: The ID of the FHIR store :type fhir_store_id: str :param note_id: The ID of the note that is being created :type note_id: str :rtype: NoteCreateResponse """ res = None status = None try: store_name = None try: store_name = "datasets/%s/fhirStores/%s" % \ (dataset_id, fhir_store_id) DbFhirStore.objects.get(name=store_name) except DoesNotExist: status = 400 res = Error("The specified FHIR store was not found", status) return res, status try: note_create_request = NoteCreateRequest.from_dict( connexion.request.get_json()) try: DbPatient.objects.get( fhirStoreName=store_name, identifier=note_create_request.patient_id) except DoesNotExist: status = 400 res = Error("The specified patient was not found", status) return res, status resource_name = "%s/fhir/Note/%s" % (store_name, note_id) DbNote(identifier=note_id, resourceName=resource_name, fhirStoreName=store_name, text=note_create_request.text, type=note_create_request.type, patientId=note_create_request.patient_id).save() note_resource_name = "%s/fhir/Note/%s" % (store_name, note_id) res = NoteCreateResponse(name=note_resource_name) status = 201 except NotUniqueError as error: status = 409 res = Error("Conflict", status, str(error)) except Exception as error: status = 500 res = Error("Internal error", status, str(error)) return res, status
def delete_notes_by_patient(fhir_store_name, patientId): res = None status = None try: DbNote.objects(fhirStoreName=fhir_store_name, patientId=patientId).delete() res = {} status = 200 except DoesNotExist: status = 404 res = Error("The specified resource was not found", status) except Exception as error: status = 500 res = Error("Internal error", status, str(error)) return res, status
def list_datasets(limit=None, offset=None): # noqa: E501 """Get all datasets Returns the datasets # noqa: E501 :param limit: Maximum number of results returned :type limit: int :param offset: Index of the first result that must be returned :type offset: int :rtype: PageOfDatasets """ res = None status = None try: db_objects = DbDataset.objects db_datasets = db_objects.skip(offset).limit(limit) total_results = db_objects.count() datasets = [Dataset.from_dict(d.to_dict()) for d in db_datasets] next_ = "" if len(datasets) == limit: next_ = "%s/datasets?limit=%s&offset=%s" % \ (Config().server_api_url, limit, offset + limit) res = PageOfDatasets(offset=offset, limit=limit, links={"next": next_}, total_results=total_results, datasets=datasets) status = 200 except Exception as error: status = 500 res = Error("Internal error", status, str(error)) return res, status
def create_patient(dataset_id, fhir_store_id, patient_id): # noqa: E501 """Create a FHIR patient Create a FHIR patient # noqa: E501 :param dataset_id: The ID of the dataset :type dataset_id: str :param fhir_store_id: The ID of the FHIR store :type fhir_store_id: str :param patient_id: The ID of the patient that is being created :type patient_id: str :rtype: PatientCreateResponse """ res = None status = None try: store_name = None try: store_name = "datasets/%s/fhirStores/%s" % ( dataset_id, fhir_store_id) # noqa: E501 DbFhirStore.objects.get(name=store_name) except DoesNotExist: status = 400 res = Error("The specified FHIR store was not found", status) return res, status try: patient_create_request = PatientCreateRequest.from_dict( connexion.request.get_json()) resource_name = "%s/fhir/Patient/%s" % (store_name, patient_id) DbPatient(resourceName=resource_name, fhirStoreName=store_name, identifier=patient_id, gender=patient_create_request.gender).save() patient_resource_name = "%s/fhir/Patient/%s" % \ (store_name, patient_id) res = PatientCreateResponse(name=patient_resource_name) status = 201 except NotUniqueError as error: status = 409 res = Error("Conflict", status, str(error)) except Exception as error: status = 500 res = Error("Internal error", status, str(error)) return res, status
def create_fhir_store(dataset_id, fhir_store_id): # noqa: E501 """Create a FHIR store Create a FHIR store with the ID specified # noqa: E501 :param dataset_id: The ID of the dataset :type dataset_id: str :param fhir_store_id: The ID of the FHIR store that is being created. :type fhir_store_id: str :rtype: FhirStoreCreateResponse """ res = None status = None try: fhir_store = None try: store_name = "datasets/%s/fhirStores/%s" % ( dataset_id, fhir_store_id) # noqa: E501 fhir_store = FhirStore(name=store_name) except Exception as error: status = 400 res = Error("Invalid input", status, str(error)) return status, res try: tokens = fhir_store.name.split('/') dataset_name = "/".join(tokens[:2]) DbDataset.objects.get(name=dataset_name) except DoesNotExist: status = 400 res = Error("The specified dataset was not found", status) return res, status if status is None: try: db_fhir_store = DbFhirStore(name=fhir_store.name).save() res = FhirStoreCreateResponse(name=db_fhir_store.name) status = 201 except NotUniqueError as error: status = 409 res = Error("Conflict", status, str(error)) except Exception as error: status = 500 res = Error("Internal error", status, str(error)) return res, status
def delete_annotation_store_by_name(annotation_store_by_name): res = None status = None try: db_annotation_store = DbAnnotationStore.objects.get( name=annotation_store_by_name) # delete resources in the store DbAnnotation.objects(annotationStoreName=annotation_store_by_name ).delete() # noqa: E501 db_annotation_store.delete() res = {} status = 200 except DoesNotExist: status = 404 res = Error("The specified resource was not found", status) except Exception as error: status = 500 res = Error("Internal error", status, str(error)) return res, status
def delete_fhir_store_by_name(fhir_store_name): res = None status = None try: db_fhir_store = DbFhirStore.objects.get(name=fhir_store_name) # delete resources in the store DbPatient.objects(fhirStoreName=fhir_store_name).delete() DbNote.objects(fhirStoreName=fhir_store_name).delete() FhirStore.from_dict(db_fhir_store.to_dict()) db_fhir_store.delete() res = {} status = 200 except DoesNotExist: status = 404 res = Error("The specified resource was not found", status) except Exception as error: status = 500 res = Error("Internal error", status, str(error)) return res, status
def add_menu_item(): # noqa: E501 """Create a menu item Creates a new item in the menu. Duplicates are allowed # noqa: E501 :param menu_item: Item to add to the store :type menu_item: dict | bytes :rtype: None """ if connexion.request.is_json: try: menu_item = MenuItem.from_dict( connexion.request.get_json()) # noqa: E501 return models.MenuItem.add(menu_item).serialize() except (SQLAlchemyError, TypeError): models.db.session.rollback() return Error(400), 400 else: return Error(400), 400
def get_image(image_id): # noqa: E501 """Get image Returns the image as image/png # noqa: E501 :param image_id: The imageId of the image to retrieve :type image_id: int :rtype: file """ if not (image := models.Image.get_image(image_id)): return Error(404, "image not found"), 404
def create_annotation_store(dataset_id, annotation_store_id): # noqa: E501 """Create an annotation store Create an annotation store with the ID specified # noqa: E501 :param dataset_id: The ID of the dataset :type dataset_id: str :param annotation_store_id: The ID of the annotation store that is being created. :type annotation_store_id: str :rtype: AnnotationStoreCreateResponse """ res = None status = None try: store_name = "datasets/%s/annotationStores/%s" % ( dataset_id, annotation_store_id) # noqa: E501 annotation_store = AnnotationStore(name=store_name) try: tokens = annotation_store.name.split('/') dataset_name = "/".join(tokens[:2]) DbDataset.objects.get(name=dataset_name) except DoesNotExist: status = 400 res = Error("The specified dataset was not found", status) return res, status try: DbAnnotationStore(name=annotation_store.name).save() res = AnnotationStoreCreateResponse(name=store_name) status = 201 except NotUniqueError as error: status = 409 res = Error("Conflict", status, str(error)) except Exception as error: status = 500 res = Error("Internal error", status, str(error)) return res, status
def delete_image(image_id): # noqa: E501 """Remove image The imageId must exist # noqa: E501 :param image_id: The imageId to delete :type image_id: int :rtype: None """ if image_id not in IMAGES: return Error(404, "image not found") os.remove(IMAGES.get(int(image_id))) del IMAGES[int(image_id)]
def delete_image(image_id): # noqa: E501 """Remove image The imageId must exist # noqa: E501 :param image_id: The imageId to delete :type image_id: int :rtype: None """ try: models.Image.delete_image(image_id) except (SQLAlchemyError, TypeError): models.db.session.rollback() return Error(400), 400
def get_health_check(): # noqa: E501 """Get health check information Get information about the health of the service # noqa: E501 :rtype: HealthCheck """ try: res = HealthCheck(status="pass") status = 200 except Exception as error: status = 500 res = Error("Internal error", status, str(error)) return res, status
def get_dataset(dataset_id): # noqa: E501 """Get a dataset by ID Returns the dataset for a given ID # noqa: E501 :param dataset_id: The ID of the dataset :type dataset_id: str :rtype: Dataset """ res = None status = None try: dataset_name = "datasets/%s" % (dataset_id, ) db_dataset = DbDataset.objects.get(name=dataset_name) res = Dataset.from_dict(db_dataset.to_dict()) status = 200 except DoesNotExist: status = 404 res = Error("The specified resource was not found", status) except Exception as error: status = 500 res = Error("Internal error", status, str(error)) return res, status
def get_image(image_id): # noqa: E501 """Get image Returns the image as image/png # noqa: E501 :param image_id: The imageId of the image to retrieve :type image_id: int :rtype: file """ if image_id not in IMAGES: return Error(404, "image not found") with open(IMAGES.get(int(image_id)), "rb") as f: data = f.read() return data
def add_menu_item(): # noqa: E501 """Create a menu item Creates a new item in the menu. Duplicates are allowed # noqa: E501 :param menu_item: Item to add to the store :type menu_item: dict | bytes :rtype: None """ if connexion.request.is_json: menu_item = MenuItem.from_dict(connexion.request.get_json()) # noqa: E501 if MENU.get(int(menu_item.id)): return Error(400) else: MENU.update({int(menu_item.id): menu_item})
def delete_cart_item(item_id): # noqa: E501 """Remove item from cart The item must be in the cart. If multiple of same item, call this twice # noqa: E501 :param item_id: The menu item to delete from cart :type item_id: int :rtype: None """ # this is by no means efficient try: models.Cart.delete_item_by_id(connexion.request.remote_addr, item_id) except (SQLAlchemyError, TypeError): models.db.session.rollback() return Error(403), 403 # cart is already devoid of this item
def list_annotations(dataset_id, annotation_store_id, limit=None, offset=None): # noqa: E501 """List the annotations in an annotation store Returns the annotations in an annotation store # noqa: E501 :param dataset_id: The ID of the dataset :type dataset_id: str :param annotation_store_id: The ID of the annotation store :type annotation_store_id: str :param limit: Maximum number of results returned :type limit: int :param offset: Index of the first result that must be returned :type offset: int :rtype: PageOfAnnotations """ res = None status = None try: store_name = "datasets/%s/annotationStores/%s" % ( dataset_id, annotation_store_id) # noqa: E501 db_objects = DbAnnotation.objects(annotationStoreName=store_name) db_annotations = db_objects.skip(offset).limit(limit) total_results = db_objects.count() annotations = [ Annotation.from_dict(a.to_dict()) for a in db_annotations ] # noqa: E501 next_ = "" if len(annotations) == limit: next_ = ( "%s/datasets/%s/annotationStores/%s/annotations" "?limit=%s&offset=%s") % \ (Config().server_api_url, dataset_id, annotation_store_id, limit, # noqa: E501 offset + limit) res = PageOfAnnotations(offset=offset, limit=limit, links={"next": next_}, total_results=total_results, annotations=annotations) status = 200 except Exception as error: status = 500 res = Error("Internal error", status, str(error)) return res, status
def list_patients(dataset_id, fhir_store_id, limit=None, offset=None): # noqa: E501 """List the Patients in a FHIR store Returns the Patients in a FHIR store # noqa: E501 :param dataset_id: The ID of the dataset :type dataset_id: str :param fhir_store_id: The ID of the FHIR store :type fhir_store_id: str :param limit: Maximum number of results returned :type limit: int :param offset: Index of the first result that must be returned :type offset: int :rtype: PageOfPatients """ res = None status = None try: store_name = "datasets/%s/fhirStores/%s" % (dataset_id, fhir_store_id) db_objects = DbPatient.objects(fhirStoreName=store_name) db_patients = db_objects.skip(offset).limit(limit) total_results = db_objects.count() patients = [Patient.from_dict(p.to_dict()) for p in db_patients] next_ = "" if len(patients) == limit: next_ = '{api_url}/{fhir_store_name}/fhir/Patient?limit={limit}' \ '&offset={offset}'.format( api_url=Config().server_api_url, fhir_store_name=store_name, limit=limit, offset=offset + limit ) res = PageOfPatients(offset=offset, limit=limit, links={"next": next_}, total_results=total_results, patients=patients) status = 200 except Exception as error: status = 500 res = Error("Internal error", status, str(error)) return res, status
def delete_cart_item(item_id): # noqa: E501 """Remove item from cart The item must be in the cart. If multiple of same item, call this twice # noqa: E501 :param item_id: The menu item to delete from cart :type item_id: int :rtype: None """ # this is by no means efficient for item in CART: if item.id == item_id: CART.remove(item) break else: return Error(403) # cart is already devoid of this item
def add_cart_item(): # noqa: E501 """Add a menu item a cart Creates a new item in the cart. Duplicates are allowed # noqa: E501 :param menu_item: Item to add to the cart :type menu_item: dict | bytes :rtype: None """ if connexion.request.is_json: menu_item = MenuItem.from_dict( connexion.request.get_json()) # noqa: E501 try: models.Cart.add_item(connexion.request.remote_addr, menu_item) # models.Cart.add_item(connexion.request.host.split(':')[0], menu_item) except (SQLAlchemyError, TypeError, AttributeError): models.db.session.rollback() return Error(400), 400
def add_image(): # noqa: E501 """Add an image to the restaraunt Creates an image. Duplicates are allowed. Returns the image id # noqa: E501 :param file_name: :type file_name: str :rtype: InlineResponse200 """ uploaded_file = connexion.request.files["fileName"] # save the file to the path and then save the path to the 'db' try: image = models.Image.add(uploaded_file.read()) return InlineResponse200(image_id=image.id) except (SQLAlchemyError, TypeError): models.db.session.rollback() return Error(400), 400