def assoc_physio_with_patient(request, patient_id): """ Method that associates a physio with a patient :param request: request of association :param patient_id: id of the patient :return: id of patient, physio and of association """ physiotherapist_id = request['physiotherapist_id'] return_value = { 'patient_id': patient_id, 'physiotherapist_id': physiotherapist_id } PatientService.is_valid_patient(patient_id) PhysiotherapistService.is_valid_physiotherapist(physiotherapist_id) if not PatientPhysioService.check_if_patient_has_physio(patient_id): new_patient_physio = PatientPhysiotherapist.objects.create( patient_id=patient_id, physiotherapist_id=physiotherapist_id) else: new_patient_physio = PatientPhysiotherapist.objects.get( patient=patient_id) new_patient_physio.physiotherapist_id = physiotherapist_id new_patient_physio.save() return_value['id'] = new_patient_physio.id return return_value
def list_all_treatments(id_patient, id_treatment_cycle, page_num, page_size, path): """ Method to list all treatments on treatment cycle :param id_patient: Specified Patient :param id_treatment_cycle: Specified treatment cycle :param page_num: Specified page number :param page_size: Specified number of elements per page :param path: Current endpoint url :return: list of information of patients on the system """ PatientService.is_valid_patient(id_patient) TreatmentService.is_valid_treatment_cycle(id_treatment_cycle, id_patient) all_treatments = Treatment.objects.filter( treatment_cycle_id=id_treatment_cycle) paginator = Paginator(all_treatments, page_size) PaginationService.is_valid_page_number(paginator, int(page_num)) results = TreatmentListSerializer(paginator.page(page_num), many=True).data return PaginationService.get_paginated_results(paginator, page_num, path, results)
def test_true_is_valid_patient(self, mock_patient_objects): """ Tests if the method is successful when the patient from the database is valid :param mock_patient_objects: Mock of the patient model """ mock_patient_objects.filter.return_value.exists.return_value = True PatientService.is_valid_patient(self.id_patient) mock_patient_objects.filter.assert_called_once_with(id=self.id_patient) mock_patient_objects.filter.return_value.exists.assert_called_once()
def get(request): """ Action when calling the endpoint with GET Return list of patients :param request: request for patients list :return: json response with patients list """ pagination_args = PaginationViewUtils.get_pagination_args(request) current_path = request.build_absolute_uri() query = "" if 'query' in request.GET: query = request.GET['query'] if request.auth_user['is_admin']: all_patient_info = PatientService.list_patients( pagination_args['page_num'], pagination_args['page_size'], current_path, query) else: all_patient_info = PhysiotherapistService.list_physios_patients( request.auth_user['id'], pagination_args['page_num'], pagination_args['page_size'], current_path, query) return JsonResponse(all_patient_info, safe=False)
def test_add_patient(self, mock_person_service, mock_patient): """ Tests if the method saves the patient successfully :param mock_person_service: Mock of the person service :param mock_patient: Mock of the patient service """ mock_new_model = mock_patient.return_value return_value = PatientService.add_patient(self.patient_request) mock_person_service.assert_called_once_with( { 'street': self.patient_request['address']['street'], 'zip_code': self.patient_request['address']['zip_code'], 'city': self.patient_request['address']['city'] }, { 'nif': self.patient_request['nif'], 'first_name': self.patient_request['first_name'], 'last_name': self.patient_request['last_name'], 'birth_date': self.patient_request['birth_date'], 'telephone_number': self.patient_request['telephone_number'], 'email': self.patient_request['email'], 'gender': self.patient_request['gender'] }) mock_patient.assert_called_once_with( profession=self.patient_request['profession'], diagnostic=self.patient_request['diagnostic'], clinical_history=self.patient_request['clinical_history'], person_id=mock_person_service.return_value) mock_new_model.save.assert_called_once_with() self.assertIn(mock_new_model.id, str(return_value))
def get(request, id_patient): """ Action when calling the endpoint with GET :param request: GET request for patient information :return: json response with patient info """ return JsonResponse(PatientService.patient_info(id_patient))
def post(request): """ Action when calling the endpoint with POST :param request: request for patient adding :return: json response with new patient info """ patient_request = json.loads(request.body.decode('utf-8')) PatientView.validate_patient_request(patient_request) new_patient_info = PatientService.add_patient(patient_request) return JsonResponse(new_patient_info)
def test_list_patients(self, mock_pag_service_valid_page, mock_pag_service_get_results, mock_patient_list_serializer, mock_query_patients): """ Tests if method returns all patients successfully :param mock_pag_service_valid_page: Mock of page number validation method :param mock_pag_service_get_results: Mock of paginated results method :param mock_patient_list_serializer: Mock of patient list serializer :param mock_query_patients: Mock of list of searched patients """ with patch.object(Paginator, '__init__', return_value=None) as mock_paginator: with patch.object(Paginator, 'page', return_value=Mock()) as mock_paginator_page: pagination_args = PaginationDataRepository.get_valid_pagination( ) mock_results = mock_pag_service_get_results.return_value mock_patients_list = mock_query_patients.return_value return_value = PatientService.list_patients( pagination_args.GET['page_num'], pagination_args.GET['page_size'], pagination_args.build_absolute_uri(), self.query) mock_query_patients.assert_called_once_with( Patient.objects, self.query, mock.ANY) mock_paginator.assert_called_once_with( mock_patients_list, pagination_args.GET['page_size']) mock_pag_service_valid_page.assert_called_once_with( mock.ANY, int(pagination_args.GET['page_num'])) mock_patient_list_serializer.assert_called_once_with( mock_paginator_page(pagination_args.GET['page_num'])) self.assertEqual(return_value, mock_results)
def treat_patient(treatment_request, id_patient, id_treatment_cycle, id_physio): """ Method to treat a patient, creating a new treatment :param treatment_request: Information for the requested new treatment :param id_patient: ID of the patient to be treated :param id_treatment_cycle: Treatment cycle where the new treatment belongs :return: Treatment created """ return_value = {} PatientService.is_valid_patient(id_patient) PhysiotherapistService.is_physiotherapist_attributed_patient( id_patient, id_physio) TreatmentService.is_valid_treatment_cycle(id_treatment_cycle, id_patient) TreatmentService.has_treatment_cycle_remaining_sessions( id_treatment_cycle) new_treatment = Treatment.objects.create( treatment_cycle_id=id_treatment_cycle, start_date=treatment_request['start_date'], end_date=treatment_request['end_date'] if 'end_date' in treatment_request else None, summary=treatment_request['summary'] if 'summary' in treatment_request else None, pain_level=treatment_request['pain_level'] if 'pain_level' in treatment_request else None, medication=treatment_request['medication'] if 'medication' in treatment_request else None, treatment=treatment_request['treatment'] if 'treatment' in treatment_request else None, periodic_evaluation=treatment_request['periodic_evaluation'] if 'periodic_evaluation' in treatment_request else None) new_treatment.save() return_value['treatment_id'] = new_treatment.id if "perimetries" in treatment_request: return_value['perimetries_id'] = [] for perimetry in treatment_request['perimetries']: return_value['perimetries_id'].append( PerimetryService.add_perimetry(perimetry, new_treatment.id)) if "muscle_tests" in treatment_request: return_value['muscle_tests_id'] = [] for muscle_test in treatment_request['muscle_tests']: return_value['muscle_tests_id'].append( MuscleTestService.add_muscle_test(muscle_test, new_treatment.id)) if "goniometries" in treatment_request: return_value['treatment_requests_id'] = [] for goniometry in treatment_request['goniometries']: return_value['treatment_requests_id'].append( GoniometryService.add_goniometry(goniometry, new_treatment.id)) return return_value