38 lines
1.1 KiB
Python
38 lines
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
|