From 0ef3b472aae2ed693b16d3da95b1562fefa1e6cd Mon Sep 17 00:00:00 2001 From: Magnus Hagander Date: Tue, 5 Nov 2019 19:59:35 +0100 Subject: Make loading of qrencode and cairosvg optional In particular, cairosvg is only used for the PNG format twitter cards, so most functionality exists without it. Make sure we import those modules conditionally only when needed and handle the import exception. Also add an explicit attempt to load them during system startup and write to the log if it fails, so it doesn't do so silently. --- postgresqleu/util/misc/baseinvoice.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'postgresqleu/util/misc') diff --git a/postgresqleu/util/misc/baseinvoice.py b/postgresqleu/util/misc/baseinvoice.py index 8d79492e..834f37bc 100755 --- a/postgresqleu/util/misc/baseinvoice.py +++ b/postgresqleu/util/misc/baseinvoice.py @@ -13,7 +13,6 @@ from reportlab.platypus.tables import Table, TableStyle from reportlab.platypus.flowables import Image from reportlab.pdfbase.pdfmetrics import registerFont, getFont from reportlab.pdfbase.ttfonts import TTFont -import qrencode from io import BytesIO from django.conf import settings @@ -324,11 +323,17 @@ class BaseInvoice(PDFBase): p.wrapOn(self.canvas, cm(12), cm(2)) p.drawOn(self.canvas, cm(2), cm(3.5)) - (ver, size, qrimage) = qrencode.encode(self.paymentlink) - qrimage = qrimage.resize((size * 4, size * 4)) - self.canvas.drawImage(ImageReader(qrimage), - cm(2), cm(1.8), - cm(1.5), cm(1.5)) + try: + import qrencode + (ver, size, qrimage) = qrencode.encode(self.paymentlink) + qrimage = qrimage.resize((size * 4, size * 4)) + self.canvas.drawImage(ImageReader(qrimage), + cm(2), cm(1.8), + cm(1.5), cm(1.5)) + except ImportError: + # If we don't have the qrcode module, we just don't bother drawing the + # QR code for the link + pass if self.reverse_vat: t = self.canvas.beginText() -- cgit v1.2.3