from django.http import HttpResponse from django.template.defaultfilters import slugify from django.views.generic.detail import DetailView from paperless.db import GnuPG from .models import Document class PdfView(DetailView): model = Document def render_to_response(self, context, **response_kwargs): """ Override the default to return the unencrypted PDF as raw data. """ content_types = { Document.TYPE_PDF: "application/pdf", Document.TYPE_PNG: "image/png", Document.TYPE_JPG: "image/jpeg", Document.TYPE_GIF: "image/gif", Document.TYPE_TIF: "image/tiff", } response = HttpResponse( GnuPG.decrypted(self.object.source_file), content_type=content_types[self.object.file_type] ) response["Content-Disposition"] = 'attachment; filename="{}"'.format( slugify(str(self.object)) + "." + self.object.file_type) return response