From 66d32fe7604fda5186b192fe9ba4d2cca3b21275 Mon Sep 17 00:00:00 2001 From: ariannedee Date: Mon, 28 Sep 2020 11:40:19 -0700 Subject: [PATCH] DIV-1156: Retrieve images by file key for PDF generation --- edivorce/apps/core/models.py | 4 ++++ edivorce/apps/core/urls.py | 1 + edivorce/apps/core/views/api.py | 29 ++++++++++++++++++++++++----- edivorce/apps/poc/views.py | 1 + 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/edivorce/apps/core/models.py b/edivorce/apps/core/models.py index a593bc60..9b569ee3 100644 --- a/edivorce/apps/core/models.py +++ b/edivorce/apps/core/models.py @@ -171,6 +171,10 @@ class Document(models.Model): q = Document.objects.filter(bceid_user=self.bceid_user, doc_type=self.doc_type, party_code=self.party_code, sort_order__gt=self.sort_order) q.update(sort_order=F('sort_order') - 1) + @staticmethod + def get_file(file_key): + return redis.RedisStorage().open(file_key) + class DontLog: def log_addition(self, *args): diff --git a/edivorce/apps/core/urls.py b/edivorce/apps/core/urls.py index bc77a4b9..cfa0f35d 100644 --- a/edivorce/apps/core/urls.py +++ b/edivorce/apps/core/urls.py @@ -9,6 +9,7 @@ urlpatterns = [ url(r'^api/documents/$', api.DocumentCreateView.as_view(), name='documents'), path('api/documents///', api.DocumentMetaDataView.as_view(), name='documents-meta'), path('api/documents/////', api.DocumentView.as_view(), name='document'), + path('api/documents//', api.get_document_file_by_key, name='document_by_key'), # url(r'^login/headers$', system.headers), diff --git a/edivorce/apps/core/views/api.py b/edivorce/apps/core/views/api.py index 5ee8e326..584fbcac 100644 --- a/edivorce/apps/core/views/api.py +++ b/edivorce/apps/core/views/api.py @@ -1,6 +1,6 @@ import graphene import graphene_django -from django.http import HttpResponse, HttpResponseGone +from django.http import Http404, HttpResponse, HttpResponseGone from graphql import GraphQLError from rest_framework import permissions, status from rest_framework.generics import CreateAPIView, ListAPIView, RetrieveUpdateDestroyAPIView @@ -79,18 +79,37 @@ class DocumentView(RetrieveUpdateDestroyAPIView): def retrieve(self, request, *args, **kwargs): """ Return the file instead of meta data """ - doc = self.get_object() - content_type = 'application/pdf' if 'pdf' in doc.filename else 'image/jpeg' + document = self.get_object() + content_type = _content_type_from_filename(document.filename) # If file doesn't exist anymore, delete it try: - file_contents = doc.file.read() + file_contents = document.file.read() except TypeError: - doc.delete() + document.delete() return HttpResponseGone('File no longer exists') return HttpResponse(file_contents, content_type=content_type) +def get_document_file_by_key(request, file_key): + file = Document.get_file(file_key) + content_type = _content_type_from_filename(file.name) + try: + return HttpResponse(file, content_type=content_type) + except TypeError: + raise Http404("File not found") + + +def _content_type_from_filename(filename): + if filename.endswith('pdf'): + content_type = 'application/pdf' + elif filename.endswith('png'): + content_type = 'image/png' + else: + content_type = 'image/jpeg' + return content_type + + class DocumentType(graphene_django.DjangoObjectType): file_url = graphene.String(source='get_file_url') diff --git a/edivorce/apps/poc/views.py b/edivorce/apps/poc/views.py index b6ac114a..69c8c566 100644 --- a/edivorce/apps/poc/views.py +++ b/edivorce/apps/poc/views.py @@ -60,6 +60,7 @@ class UploadStorage(CreateView): class UploadStorageDelete(DeleteView): model = Document success_url = settings.FORCE_SCRIPT_NAME + 'poc/storage' + template_name = 'poc/document_confirm_delete.html' def view_document_file(request, document_id):