JourneyJoker/payment/voucher/views.py
Klaus-Uwe Mitterer a3d31c5cc3 Implement PDF generator
Improve mail view
Implement voucher generation
2021-05-31 07:20:27 +02:00

38 lines
No EOL
1.1 KiB
Python

from django.views.generic import CreateView, DetailView
from django.shortcuts import redirect
from django.utils import timezone
from django.http import Http404, HttpResponse
from localauth.mixins import SuperUserRequiredMixin
from pdf.views import PDFView
from .models import VoucherDownloadURL
class AdminGenerateVoucherView(SuperUserRequiredMixin, CreateView):
pass
class VoucherPDFView(PDFView):
template_name = "payment/voucher/pdfvoucher.html"
voucher = None
def get_context_data(self, **kwargs):
return super().get_context_data(voucher=self.voucher, **kwargs)
class DownloadVoucherView(DetailView):
model = VoucherDownloadURL
def get(self, request, *args, **kwargs):
obj = self.get_object()
if (not obj) or obj.expiry < timezone.now():
raise Http404()
pdfview = VoucherPDFView()
pdfview.voucher = obj.voucher
pdf = pdfview.render()
response = HttpResponse(pdf, content_type="application/pdf")
response['Content-Disposition'] = 'inline; filename=voucher.pdf'
return response