Added GPG encryption for the PDFs

This commit is contained in:
Daniel Quinn
2016-01-01 16:13:59 +00:00
parent 6956376d71
commit f72c515742
8 changed files with 106 additions and 50 deletions

View File

@@ -1,3 +1,29 @@
from django.shortcuts import render
import gnupg
# Create your views here.
from django.conf import settings
from django.http import HttpResponse
from django.template.defaultfilters import slugify
from django.views.generic.detail import DetailView
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.
"""
gpg = gnupg.GPG(gnupghome=settings.GNUPG_HOME)
response = HttpResponse(gpg.decrypt_file(
self.object.pdf,
passphrase=settings.PASSPHRASE,
).data, content_type="application/pdf")
response["Content-Disposition"] = 'attachment; filename="{}"'.format(
slugify(str(self.object)) + ".pdf")
return response