def update(self, request, *args, **kwargs): remote_serializer = CreateRemotePatientSerializer(data=request.data) remote_serializer.is_valid(raise_exception=True) obj = self.get_object() credentials = { "username": obj.hospital.clinic_remote_admin_username, "password": obj.hospital.clinic_remote_admin_password } response = requests.post(settings.IDR_AUTH_URL, data=credentials) if response.status_code != 200: return Response("Authorization fail", status=status.HTTP_400_BAD_REQUEST) data = response.json() requests.patch(url=settings.IDR_AUTH_PATIENT_URL + str(obj.remote_id) + '/', headers={'Authorization': 'Token ' + data.get('token')}, data=remote_serializer.data) reset_patients_cache(hospital=request.user.hospital) patient_data = get_patients_cache(hospital=obj.hospital).get( 'patients', {}).get(obj.remote_id, {}) patient_name = patient_data.get( 'first_name', '') + ' ' + patient_data.get('last_name', ' ') Chat.objects.filter(patient=obj).update(patient_name=patient_name) # TODO: uncomment location # if 'state' in request.data: # obj.state = Locations.get_state_name_by_code(request.data.get('state')) # obj.country = Locations.get_country_by_state_code(request.data.get('state')) # obj.save() return super(PatientViewSet, self).update(request, *args, **kwargs)
def set_chat_patient_name(apps, schema_editor): Chat = apps.get_model('notifications', 'Chat') for ch in Chat.objects.all(): if ch.patient: remote_patient = get_patients_cache( hospital=ch.patient.responsible_doctor.hospital).get( 'patients', {}).get(ch.patient.remote_id, {}) ch.patient_name = remote_patient.get( "first_name") + ' ' + remote_patient.get("last_name") ch.save()
def perform_update(self, serializer): patient_data = get_patients_cache( hospital=serializer.validated_data.get('patient').hospital).get( 'patients', {}).get(serializer.validated_data.get('patient').remote_id) saved_data = { 'patient_name': patient_data.get('first_name', '') + ' ' + patient_data.get('last_name', '') if 'patient' in serializer.validated_data else None } if serializer.validated_data.get( 'patient') and self.get_object().title: saved_data['title'] = None serializer.save(**saved_data) send_ws_group_message( 5, ChatGetSerializer(instance=serializer.instance).data, serializer.instance.recipient_id_list)
def perform_create(self, serializer): patient_data = get_patients_cache( hospital=serializer.validated_data.get('patient').hospital).get( 'patients', {}).get( serializer.validated_data.get('patient').remote_id ) if 'patient' in serializer.validated_data else None saved_data = { 'creator': self.request.user, 'patient_name': patient_data.get('first_name', '') + ' ' + patient_data.get('last_name', '') if 'patient' in serializer.validated_data else None } serializer.save(**saved_data) send_ws_group_message( 4, ChatGetSerializer(instance=serializer.instance).data, serializer.instance.recipient_id_list)
def _hospital_cache(self): return get_patients_cache(self.hospital)