From: Radek Czajka Date: Mon, 3 Mar 2025 13:01:58 +0000 (+0100) Subject: downloading receipts X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/e16f5c5d7985b8e501c6810abf5e540326e392f1 downloading receipts --- diff --git a/src/club/models.py b/src/club/models.py index c09769a95..c40428def 100644 --- a/src/club/models.py +++ b/src/club/models.py @@ -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): diff --git a/src/club/templates/admin/club/schedule/change_list.html b/src/club/templates/admin/club/schedule/change_list.html index 1b6de0814..e47cd7436 100644 --- a/src/club/templates/admin/club/schedule/change_list.html +++ b/src/club/templates/admin/club/schedule/change_list.html @@ -2,6 +2,7 @@ {% load club %} {% block content %} +
@@ -19,5 +20,15 @@
Aktywne miesięczne wpłaty cykliczne:{% club_active_30day_sum %} zł.
+
+
+ {% csrf_token %} + Pobierz zestawienie roczne + + + +
+
+
{{ block.super }} {% endblock content %} diff --git a/src/club/urls.py b/src/club/urls.py index 026758070..87bfa5a90 100644 --- a/src/club/urls.py +++ b/src/club/urls.py @@ -28,4 +28,6 @@ urlpatterns = [ path('notify//', views.PayUNotifyView.as_view(), name='club_payu_notify'), path('weryfikacja/', views.member_verify, name='club_member_verify'), + + path('potwierdzenie/', views.receipt, name='club_receipt'), ] diff --git a/src/club/views.py b/src/club/views.py index c6136b67f..1ae16ce11 100644 --- a/src/club/views.py +++ b/src/club/views.py @@ -4,7 +4,7 @@ from django.conf import settings from django.contrib.auth.decorators import login_required, permission_required from django.db.models import Sum -from django.http import HttpResponseRedirect +from django.http import HttpResponse, HttpResponseRedirect from django.shortcuts import get_object_or_404, render from django.urls import reverse from django.utils.decorators import method_decorator @@ -249,3 +249,23 @@ def member_verify(request): 'result': rows } ) + + +@permission_required('club.schedule_view') +def receipt(request): + email = request.POST.get('email') + year = int(request.POST.get('year')) + + receipt = models.PayUOrder.generate_receipt(email, year) + if receipt: + content, optout, payments = receipt + if not content: + return HttpResponse('no content') + return HttpResponse( + content, + headers={ + "Content-Type": "application/pdf", + "Content-Disposition": f'attachment; filename="wolnelektury-{year}-{email}.pdf"', + } + ) +