From 80523787c3c9bd809a79d8213a9ffaa8a1732e67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Wed, 27 Dec 2017 13:14:20 +0200 Subject: [PATCH] runserver and tests work without reportlab installed. --- hc/payments/invoices.py | 19 ++++++++++--------- hc/payments/tests/test_pdf_invoice.py | 9 ++++++++- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/hc/payments/invoices.py b/hc/payments/invoices.py index 9acc5f3a..f85a3a75 100644 --- a/hc/payments/invoices.py +++ b/hc/payments/invoices.py @@ -1,20 +1,22 @@ # coding: utf-8 -from reportlab.lib.pagesizes import A4 -from reportlab.lib.units import inch -from reportlab.pdfgen import canvas - - -W, H = A4 +try: + from reportlab.lib.pagesizes import A4 + from reportlab.lib.units import inch + from reportlab.pdfgen.canvas import Canvas + W, H = A4 +except ImportError: + # Don't crash if reportlab is not installed. + Canvas = object def f(dt): return dt.strftime("%b. %-d, %Y") -class PdfInvoice(canvas.Canvas): +class PdfInvoice(Canvas): def __init__(self, fileobj): - canvas.Canvas.__init__(self, fileobj, pagesize=A4, pageCompression=0) + Canvas.__init__(self, fileobj, pagesize=A4, pageCompression=0) self.head_y = H - inch * 0.5 def linefeed(self): @@ -52,7 +54,6 @@ class PdfInvoice(canvas.Canvas): self.head_y -= inch / 8 def render(self, tx, bill_to): - width, height = A4 invoice_id = "MS-HC-%s" % tx.id.upper() self.setTitle(invoice_id) diff --git a/hc/payments/tests/test_pdf_invoice.py b/hc/payments/tests/test_pdf_invoice.py index 24d5a29a..866f4996 100644 --- a/hc/payments/tests/test_pdf_invoice.py +++ b/hc/payments/tests/test_pdf_invoice.py @@ -1,10 +1,16 @@ from mock import Mock, patch +from unittest import skipIf from django.utils.timezone import now from hc.payments.models import Subscription from hc.test import BaseTestCase import six +try: + import reportlab +except ImportError: + reportlab = None + class PdfInvoiceTestCase(BaseTestCase): @@ -24,9 +30,9 @@ class PdfInvoiceTestCase(BaseTestCase): self.tx.subscription_details.billing_period_start_date = now() self.tx.subscription_details.billing_period_end_date = now() + @skipIf(reportlab is None, "reportlab not installed") @patch("hc.payments.views.braintree") def test_it_works(self, mock_braintree): - mock_braintree.Transaction.find.return_value = self.tx self.client.login(username="alice@example.org", password="password") @@ -47,6 +53,7 @@ class PdfInvoiceTestCase(BaseTestCase): r = self.client.get("/invoice/pdf/abc123/") self.assertEqual(r.status_code, 403) + @skipIf(reportlab is None, "reportlab not installed") @patch("hc.payments.views.braintree") def test_it_shows_company_data(self, mock_braintree): self.profile.bill_to = "Alice and Partners"