X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/e24e657ebf536f55c1bde66bd41563fae30a98b0..ab9b52b00d9c995e2b2036f32270841bfebb11b2:/src/club/models.py diff --git a/src/club/models.py b/src/club/models.py index b79a7a838..c40428def 100644 --- a/src/club/models.py +++ b/src/club/models.py @@ -1,5 +1,5 @@ -# This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. -# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. +# This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later. +# Copyright © Fundacja Wolne Lektury. See NOTICE for more information. # from datetime import datetime, timedelta from decimal import Decimal @@ -417,7 +417,8 @@ class PayUOrder(payu_models.Order): ) @classmethod - def send_receipt(cls, email, year, resend=False): + def generate_receipt(cls, email, year): + # TODO: abstract out Contact = apps.get_model('messaging', 'Contact') Funding = apps.get_model('funding', 'Funding') BillingAgreement = apps.get_model('paypal', 'BillingAgreement') @@ -485,11 +486,8 @@ class PayUOrder(payu_models.Order): ctx = { "email": email, "year": year, - "next_year": year + 1, "total": sum(x['amount'] for x in payments), "payments": payments, - "optout": optout, - "resend": resend, } temp = tempfile.NamedTemporaryFile(prefix='receipt-', suffix='.pdf', delete=False) temp.close() @@ -497,15 +495,32 @@ class PayUOrder(payu_models.Order): "wl.eps": os.path.join(settings.STATIC_ROOT, "img/wl.eps"), }) + with open(temp.name, 'rb') as f: + content = f.read() + os.unlink(f.name) + return content, optout, payments + + @classmethod + def send_receipt(cls, email, year, resend=False): + receipt = cls.generate_receipt(email, year) + if receipt: + content, optout, payments = receipt + ctx = { + "email": email, + "year": year, + "next_year": year + 1, + "total": sum(x['amount'] for x in payments), + "payments": payments, + "optout": optout, + "resend": resend, + } message = EmailMessage( 'Odlicz darowiznę na Wolne Lektury od podatku', template.loader.render_to_string('club/receipt_email.txt', ctx), settings.CLUB_CONTACT_EMAIL, [email] ) - with open(temp.name, 'rb') as f: - message.attach('wolnelektury-darowizny.pdf', f.read(), 'application/pdf') + message.attach('wolnelektury-darowizny.pdf', content, 'application/pdf') message.send() - os.unlink(f.name) class PayUCardToken(payu_models.CardToken):