def post(self): logging.debug('Received request: POST api/patient/reading') try: patient_reading_data = _get_request_body() except: return {'HTTP 400': decoding_error}, 400 patient_reading_data = _get_request_body() # Ensure all data is valid abort_if_body_empty(patient_reading_data) is_invalid_patient = PatientValidation.check_patient_fields( patient_reading_data['patient']) is_invalid_reading = PatientValidation.check_reading_fields( patient_reading_data['reading']) if is_invalid_patient is not None: return is_invalid_patient # validate with new reading validator if is_invalid_reading is not None: return is_invalid_reading patient_data = patient_reading_data['patient'] if 'dob' in patient_data and patient_data[ 'dob'] and patient_data['patientAge'] is None: patient_reading_data['patient']['dob'] = int( patient_reading_data['patient']['dob']) patient_reading_data['patient'] = calculate_age_from_dob( patient_data) # create new reading (and patient if it does not already exist) reading_and_patient = readingManager.create_reading_and_patient( patient_reading_data['patient']['patientId'], patient_reading_data) # add patient to the facility of the user that took their reading user = userManager.read("id", patient_reading_data['reading']['userId']) userFacility = user['healthFacilityName'] patientFacilityManager.add_patient_facility_relationship( patient_reading_data['patient']['patientId'], userFacility) # associate new reading with patient reading_and_patient[ 'message'] = 'Patient reading created successfully!' return reading_and_patient, 201
def post(self): logging.debug('Received request: POST /patient') try: patient_data = self._get_request_body() except: return {'HTTP 400': decoding_error}, 400 patient_data = self._get_request_body() patient_data['dob'] = int(patient_data['dob']) # Ensure all data is valid abort_if_body_empty(patient_data) abort_if_patient_exists(patient_data['patientId']) invalid = PatientValidation.check_patient_fields(patient_data) if invalid is not None: return invalid # if age is not provided, populate age using dob if 'dob' in patient_data and patient_data[ 'dob'] and patient_data['patientAge'] is None: patient_data = calculate_age_from_dob(patient_data) response_body = patientManager.create(patient_data) return response_body, 201
def test_check_patient_fields_fails(): result = PatientValidation.check_patient_fields( patient_request_body_missing_fields) assert result[1] == 400
def test_check_patient_fields_succeeds(): result = PatientValidation.check_patient_fields(patient_request_body) assert result is None