Esempio n. 1
0
    def create(self, request, *args, **kwargs):
        remote_serializer = CreateRemotePatientSerializer(data=request.data)
        remote_serializer.is_valid(raise_exception=True)
        request.data['remote_id'] = 0
        serializer = self.get_serializer(data=request.data)
        if serializer.is_valid():
            response = requests.post(
                settings.IDR_AUTH_PATIENT_URL,
                data=remote_serializer.data,
                headers={'Authorization': get_authorization_header(request)})
            if response.status_code != 201:
                return Response(response.content,
                                status=status.HTTP_400_BAD_REQUEST)

            remote_data = response.json()
            remote_id = remote_data.get('id')
            if not remote_id:
                msg = _(
                    'Got incorrect response data from IDR AUTH server on doctor creating'
                )
                return Response({"authorization": msg},
                                status=status.HTTP_400_BAD_REQUEST)

            # we update request.data for perform_create
            reset_patients_cache(hospital=request.user.hospital)
            request.data['remote_id'] = remote_id
            return super(PatientViewSet, self).create(request, *args, **kwargs)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 2
0
    def import_csv(self, request):
        response = requests.get(
            settings.IDR_AUTH_PATIENT_URL,
            headers={'Authorization': get_authorization_header(request)})
        if response.status_code != 200:
            return Response(response.content,
                            status=status.HTTP_400_BAD_REQUEST)

        remote_clinic_patients = response.json()
        res = parse_patient_csv(request.FILES['file'], remote_clinic_patients,
                                request)
        patients = res.pop('patients')
        if patients:
            remote_serializer = CreateRemotePatientSerializer(data=patients,
                                                              many=True)
            if remote_serializer.is_valid(raise_exception=True):
                response = requests.post(
                    '{}{}/create_patients_from_list/'.format(
                        settings.IDR_AUTH_DOCTOR_URL, request.user.remote_id),
                    data=json.dumps({"patient_list": remote_serializer.data}),
                    headers={
                        'Authorization': get_authorization_header(request),
                        'Content-Type': 'application/json'
                    })
                if response.status_code != 201:
                    return Response(response.content,
                                    status=status.HTTP_400_BAD_REQUEST)

                remote_data = response.json()
                for l_p, r_p in zip(patients, remote_data):
                    remote_id = r_p.pop('id')
                    serializer = PatientSerializer(data={**l_p, **r_p})
                    if serializer.is_valid(raise_exception=True):
                        serializer.save(hospital=self.request.user.hospital,
                                        remote_id=remote_id)
                reset_patients_cache(self.request.user.hospital)
        sheet = res.pop('sheet')
        sheet.seek(0)
        patient_import = CsvImportLog(author=request.user,
                                      import_type=1,
                                      **res)
        patient_import.sheet.save('sheet.csv', ContentFile(sheet.read()))
        patient_import.save()

        return Response(CsvImportLogSerializer(instance=patient_import).data,
                        status=status.HTTP_200_OK)
Esempio n. 3
0
 def destroy(self, request, *args, **kwargs):
     response = requests.delete(
         '{}{}/'.format(settings.IDR_AUTH_PATIENT_URL,
                        kwargs.get('remote_id')),
         headers={'Authorization': get_authorization_header(request)})
     if response.status_code != 204:
         return Response(response.content,
                         status=status.HTTP_400_BAD_REQUEST)
     return super(PatientViewSet, self).destroy(request, *args, **kwargs)
Esempio n. 4
0
 def destroy(self, request, *args, **kwargs):
     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 'fail'
     data = response.json()
     response = requests.get(
         url=settings.IDR_AUTH_DOCTOR_URL + str(obj.remote_id) + '/',
         headers={'Authorization': 'Token ' + data.get('token')})
     email_context = {
         'user':
         response.json().get('first_name') + ' ' +
         response.json().get('last_name'),
         'domain_name':
         settings.IDR_DOMAIN_NAME,
         'domain':
         settings.IDR_SITE_URL,
     }
     send_emails_by_notification_template.delay(
         NotificationTemplate.DECLINE_REGISTRATION, obj.email,
         email_context)
     response = requests.delete(
         '{}{}/'.format(settings.IDR_AUTH_DOCTOR_URL,
                        kwargs.get('remote_id')),
         headers={'Authorization': get_authorization_header(request)})
     if response.status_code != 204:
         return Response(response.content,
                         status=status.HTTP_400_BAD_REQUEST)
     update_cached_doctor_data(doctor_remote_id=obj.remote_id,
                               hospital=obj.hospital,
                               deleted=True)
     return super(DoctorViewSet, self).destroy(request, *args, **kwargs)
Esempio n. 5
0
    def import_csv(self, request):
        response = requests.get(
            settings.IDR_AUTH_DOCTOR_URL,
            headers={'Authorization': get_authorization_header(request)})
        if response.status_code != 200:
            return Response(response.content,
                            status=status.HTTP_400_BAD_REQUEST)

        current_clinic_doctors = response.json()
        res = parse_doctor_csv(request.FILES['file'], current_clinic_doctors,
                               self.request)
        doctors = res.pop('doctors')
        if doctors:
            remote_serializer = CreateRemoteDoctorSerializer(data=doctors,
                                                             many=True)
            if remote_serializer.is_valid(raise_exception=True):
                response = requests.post(
                    '{}{}/create_from_list/'.format(
                        settings.IDR_AUTH_DOCTOR_URL, request.user.remote_id),
                    data=json.dumps({"doctor_list": remote_serializer.data}),
                    headers={
                        'Authorization': get_authorization_header(request),
                        'Content-Type': 'application/json'
                    })
                if response.status_code != 201:
                    return Response(response.content,
                                    status=status.HTTP_400_BAD_REQUEST)

                remote_data = response.json()
                for doctor in remote_data:
                    doctor['remote_id'] = doctor.pop('id')

                doctors = map_records_by_slug(doctors, remote_data, 'username')
                for doctor in doctors:
                    serializer = DoctorCreateOrUpdateSerializer(data=doctor)
                    if serializer.is_valid(raise_exception=True):
                        serializer.save(remote_id=doctor.get('remote_id'),
                                        username=doctor.get('username'),
                                        is_approved=True,
                                        patient_permission=False,
                                        doctor_permission=False,
                                        create_data_cell_permission=False,
                                        edit_data_cell_permission=False,
                                        export_permission=False)
                        email_context = {
                            'admin':
                            request.user.get_full_name(),
                            'domain_name':
                            settings.IDR_DOMAIN_NAME,
                            'site_url':
                            settings.IDR_SITE_URL,
                            'password':
                            doctor.get('password'),
                            'user':
                            '******'.format(doctor.get('first_name'),
                                           doctor.get('last_name')).strip()
                        }

                        send_emails_by_notification_template.delay(
                            NotificationTemplate.NEW_USER_ACCOUNT_IS_READY,
                            doctor.get('email'), email_context)
                async_refresh_doctors_cache.delay(
                    self.request.user.hospital.id)
        sheet = res.pop('sheet')
        sheet.seek(0)
        patient_import = CsvImportLog(author=request.user,
                                      import_type=2,
                                      **res)
        patient_import.sheet.save('sheet.csv', ContentFile(sheet.read()))
        patient_import.save()

        return Response(CsvImportLogSerializer(instance=patient_import).data,
                        status=status.HTTP_200_OK)