Browse Source

runserver and tests work without reportlab installed.

pull/149/head
Pēteris Caune 7 years ago
parent
commit
80523787c3
2 changed files with 18 additions and 10 deletions
  1. +10
    -9
      hc/payments/invoices.py
  2. +8
    -1
      hc/payments/tests/test_pdf_invoice.py

+ 10
- 9
hc/payments/invoices.py View File

@ -1,20 +1,22 @@
# coding: utf-8 # 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): def f(dt):
return dt.strftime("%b. %-d, %Y") return dt.strftime("%b. %-d, %Y")
class PdfInvoice(canvas.Canvas):
class PdfInvoice(Canvas):
def __init__(self, fileobj): 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 self.head_y = H - inch * 0.5
def linefeed(self): def linefeed(self):
@ -52,7 +54,6 @@ class PdfInvoice(canvas.Canvas):
self.head_y -= inch / 8 self.head_y -= inch / 8
def render(self, tx, bill_to): def render(self, tx, bill_to):
width, height = A4
invoice_id = "MS-HC-%s" % tx.id.upper() invoice_id = "MS-HC-%s" % tx.id.upper()
self.setTitle(invoice_id) self.setTitle(invoice_id)


+ 8
- 1
hc/payments/tests/test_pdf_invoice.py View File

@ -1,10 +1,16 @@
from mock import Mock, patch from mock import Mock, patch
from unittest import skipIf
from django.utils.timezone import now from django.utils.timezone import now
from hc.payments.models import Subscription from hc.payments.models import Subscription
from hc.test import BaseTestCase from hc.test import BaseTestCase
import six import six
try:
import reportlab
except ImportError:
reportlab = None
class PdfInvoiceTestCase(BaseTestCase): 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_start_date = now()
self.tx.subscription_details.billing_period_end_date = now() self.tx.subscription_details.billing_period_end_date = now()
@skipIf(reportlab is None, "reportlab not installed")
@patch("hc.payments.views.braintree") @patch("hc.payments.views.braintree")
def test_it_works(self, mock_braintree): def test_it_works(self, mock_braintree):
mock_braintree.Transaction.find.return_value = self.tx mock_braintree.Transaction.find.return_value = self.tx
self.client.login(username="[email protected]", password="password") self.client.login(username="[email protected]", password="password")
@ -47,6 +53,7 @@ class PdfInvoiceTestCase(BaseTestCase):
r = self.client.get("/invoice/pdf/abc123/") r = self.client.get("/invoice/pdf/abc123/")
self.assertEqual(r.status_code, 403) self.assertEqual(r.status_code, 403)
@skipIf(reportlab is None, "reportlab not installed")
@patch("hc.payments.views.braintree") @patch("hc.payments.views.braintree")
def test_it_shows_company_data(self, mock_braintree): def test_it_shows_company_data(self, mock_braintree):
self.profile.bill_to = "Alice and Partners" self.profile.bill_to = "Alice and Partners"


Loading…
Cancel
Save