Exemple #1
0
 def test_real_file_response(self):
     Channel("test").send({"reply_channel": "test", "http_version": "1.1", "method": "GET", "path": b"/test/"})
     current_dir = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
     response = FileResponse(open(os.path.join(current_dir, "a_file"), "rb"))
     handler = FakeAsgiHandler(response)
     reply_messages = list(handler(self.get_next_message("test", require=True)))
     self.assertEqual(len(reply_messages), 2)
     self.assertEqual(response.getvalue(), b"")
Exemple #2
0
    def perform_create(self, serializer):
        measurement = self.get_object()
        # Create a file-like buffer to receive PDF data.
        buffer = io.BytesIO()

        # Create the PDF object, using the buffer as its "file."
        p = canvas.Canvas(buffer)

        p.setFont("Helvetica-Bold", 25)
        p.drawString(90, 720, "    Axpo's Measurements data")
        p.setFont("Helvetica", 15)
        p.drawString(90, 620, f'Id: {measurement.id}')
        p.drawString(90, 600, f'Customer: {measurement.customer}')
        p.drawString(90, 580, f'Site: {measurement.site}')
        p.drawString(90, 560,
                     f'Used power line: {measurement.used_power_line}')
        p.drawString(90, 540, f'Date: {measurement.date}')
        p.drawString(
            90, 470,
            f'Touch voltage allowed: {measurement.touch_voltage_allowed}')
        p.drawString(90, 440, f'Scaling factor: {measurement.scaling_factor}')
        p.drawString(
            90, 410,
            f'Scaled touch voltage allowed: {measurement.scaled_touch_voltage_allowed}'
        )
        p.drawString(90, 380, f'Comment: {measurement.kommentar}')

        p.drawString(300, 250, 'Name: ...............................')
        p.drawString(300, 210, 'Date: .................................')
        p.drawString(300, 160, 'Signature: ..........................')
        p.setFont("Helvetica-Oblique", 7)
        p.drawString(90, 20, f'Confidential')
        p.drawImage('visualisations/axpologo.png', 500, 750, 0.7 * inch,
                    0.4 * inch)

        p.showPage()
        p.save()

        # FileResponse sets the Content-Disposition header so that browsers
        # present the option to save the file.
        buffer.seek(0)

        pdf_file = FileResponse(buffer,
                                as_attachment=False,
                                filename='measurements_data_' +
                                str(datetime.now()) + '.pdf')

        email = self.request.data["email"]

        message = EmailMessage(
            "Measurements data",
            "Please find requested data attached. \nHave a nice day!",
            os.environ.get('DEFAULT_FROM_EMAIL'), [email])
        message.attach('measurements_data_' + str(datetime.now()) + '.pdf',
                       pdf_file.getvalue(), 'application/pdf')
        message.send()
        return Response(status=200)
Exemple #3
0
def email_file_response_as_attachment(
    subject: str,
    body: str,
    recipients: List[str],
    attachment: FileResponse
) -> None:
    attachment_bytes = attachment.getvalue()

    for recipient in recipients:
        msg = EmailMessage(subject=subject, body=body, to=[recipient])
        msg.attach(attachment.filename, attachment_bytes)
        msg.send()
Exemple #4
0
 def test_real_file_response(self):
     Channel("test").send({
         "reply_channel": "test",
         "http_version": "1.1",
         "method": "GET",
         "path": b"/test/",
     })
     current_dir = os.path.realpath(
         os.path.join(os.getcwd(), os.path.dirname(__file__)))
     response = FileResponse(open(os.path.join(current_dir, 'a_file'),
                                  'rb'))
     handler = FakeAsgiHandler(response)
     reply_messages = list(
         handler(self.get_next_message("test", require=True)))
     self.assertEqual(len(reply_messages), 2)
     self.assertEqual(response.getvalue(), b'')
Exemple #5
0
def email_file_response_as_attachment(
    subject: str,
    body: str,
    recipients: List[str],
    attachment: FileResponse,
    html_body: Optional[str] = None,
    headers: Optional[Dict[str, str]] = None,
) -> None:
    attachment_bytes = attachment.getvalue()

    for recipient in recipients:
        msg = EmailMultiAlternatives(subject=subject,
                                     body=body,
                                     to=[recipient],
                                     headers=headers)
        headers = headers or {}
        if html_body:
            msg.attach_alternative(html_body, "text/html")
        msg.attach(attachment.filename, attachment_bytes)
        msg.send()