示例#1
0
    def upload_report(cls, report):
        formats = ['pdf', 'xml']
        try:
            for format in formats:
                if format == 'pdf':
                    report_url = report.pdf_url
                else:
                    report_url = report.xml_url

                request = requests.get(report_url, stream=True)
                filename = "appointment_%s_report.%s" % (
                    report.integrator_response.object_id, format)
                lf = TemporaryUploadedFile(filename, 'byte', 1000, 'utf-8')

                for block in request.iter_content(1024 * 8):

                    # If no more file then stop
                    if not block:
                        break

                    # Write image block to temporary file
                    lf.write(block)

                lf.seek(0)
                lf.content_type = "application/%s" % format
                in_memory_file = InMemoryUploadedFile(lf, None, filename,
                                                      lf.content_type,
                                                      lf.tell(), None)

                lab_report, created = LabReport.objects.update_or_create(
                    appointment_id=report.integrator_response.object_id)
                if lab_report:
                    LabReportFile.objects.create(report_id=lab_report.id,
                                                 name=in_memory_file)
                    # Send Reports to Patient
                    from ondoc.notification.tasks import send_lab_reports
                    if format == 'pdf':
                        try:
                            send_lab_reports.apply_async(
                                (report.integrator_response.object_id, ),
                                countdown=1)
                        except Exception as e:
                            logger.error(str(e))
        except Exception as e:
            logger.error(str(e))