Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract common module for EPC QR generation code #1294

Merged
merged 3 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions apps/common/epc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from io import BytesIO

from markupsafe import Markup
from segno import QRCode, helpers

from models.payment import BankPayment


def make_epc_qrfile(payment: BankPayment, **kwargs) -> BytesIO:
qrfile = BytesIO()
# TODO: this isn't currently used. Need to fetch IBAN from payment.recommended_destination
# and name from somewhere - maybe config rather than hard-coding.
qr: QRCode = helpers.make_epc_qr(
name="EMF Festivals Ltd",
iban=payment.recommended_destination.iban,
amount=payment.amount,
reference=payment.bankref,
encoding=1,
)
qr.save(qrfile, **kwargs)
qrfile.seek(0)
return qrfile


def qrfile_to_svg(qrfile: BytesIO) -> str:
return Markup(qrfile.getvalue().decode("utf-8"))


def format_inline_epc_qr(payment: BankPayment) -> str:
qrfile = make_epc_qrfile(
payment,
kind="svg",
svgclass=None,
omitsize=True,
xmldecl=False,
svgns=False,
nl=False,
)
return qrfile_to_svg(qrfile)
37 changes: 1 addition & 36 deletions apps/payments/invoice.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from decimal import Decimal
from io import BytesIO
import logging
import shutil
import os.path
Expand All @@ -15,55 +14,21 @@
send_file,
)
from flask_login import login_required, current_user
from markupsafe import Markup
from segno import helpers
from sqlalchemy.sql.functions import func
from wtforms import TextAreaField, SubmitField

from main import external_url, db
from ..common.receipt import render_pdf
from models.product import Product, PriceTier
from models.purchase import Purchase
from ..common.epc import format_inline_epc_qr
from ..common.forms import Form
from . import get_user_payment_or_abort
from . import payments

logger = logging.getLogger(__name__)


def make_epc_qrfile(payment, **kwargs):
qrfile = BytesIO()
# TODO: this isn't currently used. Need to fetch IBAN from payment.recommended_destination
# and name from somewhere - maybe config rather than hard-coding.
qr = helpers.make_epc_qr(
name="EMF Festivals Ltd",
iban=payment.recommended_destination.iban,
amount=payment.amount,
reference=payment.bankref,
encoding=1,
)
qr.save(qrfile, **kwargs)
qrfile.seek(0)
return qrfile


def qrfile_to_svg(qrfile):
return Markup(qrfile.getvalue().decode("utf-8"))


def format_inline_epc_qr(payment):
qrfile = make_epc_qrfile(
payment,
kind="svg",
svgclass=None,
omitsize=True,
xmldecl=False,
svgns=False,
nl=False,
)
return qrfile_to_svg(qrfile)


class InvoiceForm(Form):
company = TextAreaField("Company name")
update = SubmitField("Update")
Expand Down
2 changes: 1 addition & 1 deletion tests/test_invoice.py → tests/test_epc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pyzbar.pyzbar import decode

from apps.payments.invoice import format_inline_epc_qr
from apps.common.epc import format_inline_epc_qr
from models.payment import BankPayment

from tests._utils import render_svg
Expand Down
Loading