def post():
        json = request.get_json(force=True)
        error_message = patients.validate(json)
        if error_message is not None:
            abort(400, message=error_message)
        patient = marshal.unmarshal(Patient, json)

        if crud.read(Patient, patientId=patient.patientId):
            abort(409, message=f"A patient already exists with id: {patient.patientId}")

        # Resolve invariants and set the creation timestamp for the patient ensuring
        # that both the created and lastEdited fields have the exact same value.
        invariant.resolve_reading_invariants(patient)
        creation_time = get_current_time()
        patient.created = creation_time
        patient.lastEdited = creation_time

        crud.create(patient, refresh=True)

        # Associate the patient with the user who created them
        user = util.current_user()
        assoc.associate_by_user_role(patient, user)

        # If the patient has any readings, and those readings have referrals, we
        # associate the patient with the facilities they were referred to
        for reading in patient.readings:
            referral = reading.referral
            if referral and not assoc.has_association(patient, referral.healthFacility):
                assoc.associate(patient, facility=referral.healthFacility)
                # The associate function performs a database commit, since this will
                # wipe out the patient we want to return we must refresh it.
                data.db_session.refresh(patient)
        return marshal.marshal(patient), 201
Пример #2
0
 def get():
     user = util.current_user()
     patients = view.patient_view_for_user(user)
     if util.query_param_bool(request, name="simplified"):
         # TODO: Compute simplified view for each patient
         return []
     else:
         return [marshal.marshal(p) for p in patients]
 def get():
     facilities = crud.read_all(HealthFacility)
     if util.query_param_bool(request, "simplified"):
         # If responding to a "simplified" request, only return the names of the
         # facilities and no other information
         return [f.healthFacilityName for f in facilities]
     else:
         # Otherwise, return all information about the health facilities
         return [marshal.marshal(f) for f in facilities]
Пример #4
0
    def post():
        json = request.get_json(force=True)
        error_message = facilities.validate(json)
        if error_message is not None:
            abort(400, message=error_message)

        facility = marshal.unmarshal(HealthFacility, json)
        crud.create(facility)
        return marshal.marshal(facility), 201
Пример #5
0
    def get():
        user = util.current_user()
        referrals = view.referral_view_for_user(user)

        # If the request does not specifically specify "all=true", then only return the
        # referrals which have not been assessed.
        if not util.query_param_bool(request, "all"):
            referrals = [r for r in referrals if not r.isAssessed]

        return [marshal.marshal(r) for r in referrals]
    def post():

        # Get key-value pairs from parser and remove pairs with a None value
        data = Root.parser.parse_args()
        data = util.filterPairsWithNone(data)

        # Create a DB Model instance for the new facility and load into DB
        facility = marshal.unmarshal(HealthFacility, data)
        crud.create(facility)

        # Get back a dict for return
        facilityDict = marshal.marshal(
            crud.read(HealthFacility,
                      healthFacilityName=data["healthFacilityName"]))
        return facilityDict, 201
Пример #7
0
    def post():
        json = request.get_json(force=True)
        error_message = referrals.validate(json)
        if error_message is not None:
            abort(400, message=error_message)

        referral = marshal.unmarshal(Referral, json)
        crud.create(referral)

        # Creating a referral also associates the corresponding patient to the health
        # facility they were referred to.
        patient = referral.patient
        facility = referral.healthFacility
        if not assoc.has_association(patient, facility):
            assoc.associate(patient, facility=facility)

        return marshal.marshal(referral), 201
Пример #8
0
    def post():
        json = request.get_json(force=True)
        error_message = readings.validate(json)
        if error_message is not None:
            abort(400, message=error_message)

        reading = marshal.unmarshal(Reading, json)

        if crud.read(Reading, readingId=reading.readingId):
            abort(
                409,
                message=f"A reading already exists with id: {reading.readingId}"
            )

        invariant.resolve_reading_invariants(reading)
        crud.create(reading, refresh=True)
        return marshal.marshal(reading), 201
Пример #9
0
    def put(patient_id: str):
        json = request.get_json(force=True)
        error_message = patients.validate_put_request(json, patient_id)
        if error_message is not None:
            abort(400, message=error_message)

        # If the inbound JSON contains a `base` field then we need to check if it is the
        # same as the `lastEdited` field of the existing patient. If it is then that
        # means that the patient has not been edited on the server since this inbound
        # patient was last synced and we can apply the changes. If they are not equal,
        # then that means the patient has been edited on the server after it was last
        # synced with the client. In these cases, we reject the changes for the client.
        #
        # You can think of this like aborting a git merge due to conflicts.
        base = json.get("base")
        if base:
            last_edited = crud.read(Patient, patientId=patient_id).lastEdited
            if base != last_edited:
                abort(409,
                      message="Unable to merge changes, conflict detected")

            # Delete the `base` field once we are done with it as to not confuse the
            # ORM as there is no "base" column in the database for patients.
            del json["base"]

        crud.update(Patient, json, patientId=patient_id)
        patient = crud.read(Patient, patientId=patient_id)

        # Update the patient's lastEdited timestamp only if there was no `base` field
        # in the request JSON. If there was then that means that this edit happened some
        # time in the past and is just being synced. In this case we want to keep the
        # `lastEdited` value which is present in the request.
        if not base:
            patient.lastEdited = get_current_time()
            data.db_session.commit()
            data.db_session.refresh(
                patient)  # Need to refresh the patient after commit

        return marshal.marshal(patient)
Пример #10
0
    def get(assessment_id: int):
        follow_up = crud.read(FollowUp, id=assessment_id)
        if not follow_up:
            abort(404, message=f"No assessment with id {id}")

        return marshal.marshal(follow_up)
Пример #11
0
 def get():
     follow_ups = crud.read_all(FollowUp)
     return [marshal.marshal(f) for f in follow_ups]
Пример #12
0
    def get(referral_id: int):
        referral = crud.read(Referral, id=referral_id)
        if not referral:
            abort(404, message=f"No referral with id {id}")

        return marshal.marshal(referral)
Пример #13
0
    def get(reading_id: str):
        reading = crud.read(Reading, readingId=reading_id)
        if not reading:
            abort(404, message=f"No reading with id {reading_id}")

        return marshal.marshal(reading)
Пример #14
0
 def get(patient_id: str):
     patient = crud.read(Patient, patientId=patient_id)
     if not patient:
         abort(404, message=f"No patient with id {patient_id}")
     return marshal.marshal(patient)
Пример #15
0
 def get(patient_id: str):
     patient = crud.read(Patient, patientId=patient_id)
     return [marshal.marshal(r) for r in patient.readings]