runserver and tests work without reportlab installed.

This commit is contained in:
Pēteris Caune 2017-12-27 13:14:20 +02:00
parent a174aa0faa
commit 80523787c3
2 changed files with 18 additions and 10 deletions

View File

@ -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)

View File

@ -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"