def get(self, request, *args, **kwargs): try: self.invoice = Invoice.objects.get(event=self.request.event, id=self.kwargs['invoice']) except Invoice.DoesNotExist: raise Http404(_('This invoice has not been found')) if not self.invoice.file: invoice_pdf(self.invoice.pk) self.invoice = Invoice.objects.get(pk=self.invoice.pk) if self.invoice.shredded: messages.error( request, _('The invoice file is no longer stored on the server.')) return redirect(self.get_order_url()) if not self.invoice.file: # This happens if we have celery installed and the file will be generated in the background messages.warning( request, _('The invoice file has not yet been generated, we will generate it for you ' 'now. Please try again in a few seconds.')) return redirect(self.get_order_url()) try: resp = FileResponse(self.invoice.file.file, content_type='application/pdf') except FileNotFoundError: invoice_pdf_task.apply(args=(self.invoice.pk, )) return self.get(request, *args, **kwargs) resp['Content-Disposition'] = 'attachment; filename="{}.pdf"'.format( self.invoice.number) return resp
def get(self, request, *args, **kwargs): if not self.order: raise Http404(_('Unknown order code or not authorized to access this order.')) try: invoice = Invoice.objects.get( event=self.request.event, order=self.order, id=self.kwargs['invoice'] ) except Invoice.DoesNotExist: raise Http404(_('This invoice has not been found')) if not invoice.file: invoice_pdf(invoice.pk) invoice = Invoice.objects.get(pk=invoice.pk) if invoice.shredded: messages.error(request, _('The invoice file is no longer stored on the server.')) return redirect(self.get_order_url()) if not invoice.file: # This happens if we have celery installed and the file will be generated in the background messages.warning(request, _('The invoice file has not yet been generated, we will generate it for you ' 'now. Please try again in a few seconds.')) return redirect(self.get_order_url()) try: resp = FileResponse(invoice.file.file, content_type='application/pdf') except FileNotFoundError: invoice_pdf_task.apply(args=(invoice.pk,)) return self.get(request, *args, **kwargs) resp['Content-Disposition'] = 'inline; filename="{}.pdf"'.format(invoice.number) resp._csp_ignore = True # Some browser's PDF readers do not work with CSP return resp
def get(self, request, *args, **kwargs): if not self.order: raise Http404(_('Unknown order code or not authorized to access this order.')) try: invoice = Invoice.objects.get( event=self.request.event, order=self.order, id=self.kwargs['invoice'] ) except Invoice.DoesNotExist: raise Http404(_('This invoice has not been found')) if not invoice.file: invoice_pdf(invoice.pk) invoice = Invoice.objects.get(pk=invoice.pk) if invoice.shredded: messages.error(request, _('The invoice file is no longer stored on the server.')) return redirect(self.get_order_url()) if not invoice.file: # This happens if we have celery installed and the file will be generated in the background messages.warning(request, _('The invoice file has not yet been generated, we will generate it for you ' 'now. Please try again in a few seconds.')) return redirect(self.get_order_url()) try: resp = FileResponse(invoice.file.file, content_type='application/pdf') except FileNotFoundError: invoice_pdf_task.apply(args=(invoice.pk,)) return self.get(request, *args, **kwargs) resp['Content-Disposition'] = 'attachment; filename="{}.pdf"'.format(invoice.number) return resp
def generate_files(self) -> List[Tuple[str, str, str]]: for i in self.event.invoices.filter(shredded=False): if not i.file: invoice_pdf_task.apply(args=(i.pk,)) i.refresh_from_db() i.file.open('rb') yield 'invoices/{}.pdf'.format(i.number), 'application/pdf', i.file.read() i.file.close()
def generate_files(self) -> List[Tuple[str, str, str]]: for i in self.event.invoices.filter(shredded=False): if not i.file: invoice_pdf_task.apply(args=(i.pk,)) i.refresh_from_db() i.file.open('rb') yield 'invoices/{}.pdf'.format(i.number), 'application/pdf', i.file.read() i.file.close()
def test_invoice_shredder(event, order): InvoiceAddress.objects.create(company='Acme Company', street='221B Baker Street', zipcode='12345', city='London', country='UK', order=order) inv = generate_invoice(order) invoice_pdf_task.apply(args=(inv.pk,)) inv.refresh_from_db() assert inv.invoice_to == "Acme Company\n\n221B Baker Street\n12345 London" assert inv.file fname = inv.file.path assert os.path.exists(fname) s = InvoiceShredder(event) f = list(s.generate_files()) assert len(f) == 1 s.shred_data() inv.refresh_from_db() assert "Acme" not in inv.invoice_to assert "icket" not in inv.lines.first().description assert not inv.file assert not os.path.exists(fname)
def test_invoice_shredder(event, order): InvoiceAddress.objects.create(company='Acme Company', street='221B Baker Street', zipcode='12345', city='London', country='UK', order=order) inv = generate_invoice(order) invoice_pdf_task.apply(args=(inv.pk,)) inv.refresh_from_db() assert inv.invoice_to == "Acme Company\n\n221B Baker Street\n12345 London" assert inv.file fname = inv.file.path assert os.path.exists(fname) s = InvoiceShredder(event) f = list(s.generate_files()) assert len(f) == 1 s.shred_data() inv.refresh_from_db() assert "Acme" not in inv.invoice_to assert "icket" not in inv.lines.first().description assert not inv.file assert not os.path.exists(fname)