Browse Source

DIV-1156: Retrieve images by file key for PDF generation

pull/170/head
ariannedee 5 years ago
parent
commit
66d32fe760
4 changed files with 30 additions and 5 deletions
  1. +4
    -0
      edivorce/apps/core/models.py
  2. +1
    -0
      edivorce/apps/core/urls.py
  3. +24
    -5
      edivorce/apps/core/views/api.py
  4. +1
    -0
      edivorce/apps/poc/views.py

+ 4
- 0
edivorce/apps/core/models.py View File

@ -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):


+ 1
- 0
edivorce/apps/core/urls.py View File

@ -9,6 +9,7 @@ urlpatterns = [
url(r'^api/documents/$', api.DocumentCreateView.as_view(), name='documents'),
path('api/documents/<doc_type>/<int:party_code>/', api.DocumentMetaDataView.as_view(), name='documents-meta'),
path('api/documents/<doc_type>/<int:party_code>/<filename>/<int:size>/', api.DocumentView.as_view(), name='document'),
path('api/documents/<file_key>/', api.get_document_file_by_key, name='document_by_key'),
# url(r'^login/headers$', system.headers),


+ 24
- 5
edivorce/apps/core/views/api.py View File

@ -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')


+ 1
- 0
edivorce/apps/poc/views.py View File

@ -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):


Loading…
Cancel
Save