From 9227578a9b5381e6447808f0899050b08540186f Mon Sep 17 00:00:00 2001 From: Michael Olund Date: Mon, 28 Sep 2020 15:28:40 -0700 Subject: [PATCH 1/2] restoring upload state #1 --- edivorce/apps/core/models.py | 21 +++++++++++ edivorce/apps/core/views/api.py | 23 ++----------- vue/src/components/Uploader/Uploader.vue | 44 +++++++++++++++++++++--- 3 files changed, 64 insertions(+), 24 deletions(-) diff --git a/edivorce/apps/core/models.py b/edivorce/apps/core/models.py index 39d186f1..0b1dddfa 100644 --- a/edivorce/apps/core/models.py +++ b/edivorce/apps/core/models.py @@ -1,3 +1,5 @@ +import re + from django.contrib import admin from django.db import models from django.db.models import F @@ -173,6 +175,9 @@ class Document(models.Model): def get_file_url(self): return reverse('document', kwargs={'filename': self.filename, 'doc_type': self.doc_type, 'party_code': self.party_code, 'size': self.size}) + def get_content_type(self): + return Document.content_type_from_filename(self.filename) + def update_sort_orders(self): 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) @@ -181,6 +186,22 @@ class Document(models.Model): def get_file(file_key): return redis.RedisStorage().open(file_key) + @staticmethod + def content_type_from_filename(filename): + content_types = { + "pdf": "application/pdf", + "gif": "image/gif", + "png": "image/png", + "jpe": "image/jpeg", + "jpg": "image/jpeg", + "jpeg": "image/jpeg" + } + extension = re.split(r'[\._]', filename.lower())[-1] + content_type = content_types.get(extension) + if not content_type: + return "application/unknown" + return content_type + class DontLog: def log_addition(self, *args): diff --git a/edivorce/apps/core/views/api.py b/edivorce/apps/core/views/api.py index 819e2c11..bde64568 100644 --- a/edivorce/apps/core/views/api.py +++ b/edivorce/apps/core/views/api.py @@ -1,5 +1,3 @@ -import re - import graphene import graphene_django from django.http import Http404, HttpResponse, HttpResponseGone @@ -82,7 +80,7 @@ class DocumentView(RetrieveUpdateDestroyAPIView): def retrieve(self, request, *args, **kwargs): """ Return the file instead of meta data """ document = self.get_object() - content_type = _content_type_from_filename(document.filename) + content_type = Document.content_type_from_filename(document.filename) # If file doesn't exist anymore, delete it try: @@ -95,31 +93,16 @@ class DocumentView(RetrieveUpdateDestroyAPIView): def get_document_file_by_key(request, file_key): file = Document.get_file(file_key) - content_type = _content_type_from_filename(file.name) + content_type = Document.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): - content_types = { - "pdf": "application/pdf", - "gif": "image/gif", - "png": "image/png", - "jpe": "image/jpeg", - "jpg": "image/jpeg", - "jpeg": "image/jpeg" - } - extension = re.split(r'[\._]', filename.lower())[-1] - content_type = content_types.get(extension) - if not content_type: - raise TypeError(f'Filetype "{extension}" not supported') - return content_type - - class DocumentType(graphene_django.DjangoObjectType): file_url = graphene.String(source='get_file_url') + content_type = graphene.String(source='get_content_type') class Meta: model = Document diff --git a/vue/src/components/Uploader/Uploader.vue b/vue/src/components/Uploader/Uploader.vue index 5b3b6a00..d811f297 100644 --- a/vue/src/components/Uploader/Uploader.vue +++ b/vue/src/components/Uploader/Uploader.vue @@ -248,8 +248,8 @@ export default { newFile.rotation = 0; const img = new Image(); img.onload = function() { - newFile.width = this.width; - newFile.height = this.height; + newFile.width = this.width || 0; + newFile.height = this.height || 0; } img.src = newFile.objectURL; } @@ -261,7 +261,10 @@ export default { const url = `${urlbase}/${this.docType}/${this.party}/${file.size}/${encFilename}`; axios.delete(url) .then(response => { - this.$refs.upload.remove(file) + var pos = this.files.findIndex(f => f.docType === file.docType && f.size === file.size); + if (pos > -1) { + this.files.splice(pos, 1); + } }) .catch((error) => { this.showError('Error deleting document from the server: ' + file.name); @@ -320,7 +323,7 @@ export default { query: ` mutation updateMetadata { updateMetadata(input:${graphQLData}){ - documents{filename size width height rotation} + documents{filename size width height rotation contentType} } } `}) @@ -334,6 +337,39 @@ export default { } }, created() { + // get saved state from the server + const url = `${this.$parent.proxyRootPath}api/graphql/`; + axios.post(url, { + query: ` + query getMetadata { + documents(docType:"${this.docType}",partyCode:${this.party}) { + filename size width height rotation contentType + } + } + `, + variables: null}) + .then(response => { + console.log('response', response); + response.data.data.documents.forEach((doc) => { + this.files.push({ + name: doc.filename, + size: doc.size, + width: doc.width, + height: doc.height, + rotation: doc.rotation, + type: doc.contentType, + error: false, + success: true, + progress: '100.00', + objectURL: `${this.$parent.proxyRootPath}api/documents/${this.docType}/${this.party}/${doc.size}/${doc.filename}` + }); + }); + }) + .catch((error) => { + this.showError('Error getting metadata'); + console.log('error', error); + }); + // call the API to update the metadata every second, but only if // the data has changed (throttling requests because rotating and // re-ordering images can cause a lot of traffic and possibly From 77aab85a426326d726e285690208d80079a08e36 Mon Sep 17 00:00:00 2001 From: Michael Olund Date: Mon, 28 Sep 2020 18:10:29 -0700 Subject: [PATCH 2/2] Fixed broken test --- edivorce/apps/core/tests/test_logic.py | 33 ++++++++++++-------------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/edivorce/apps/core/tests/test_logic.py b/edivorce/apps/core/tests/test_logic.py index f40c7186..94dc2b37 100644 --- a/edivorce/apps/core/tests/test_logic.py +++ b/edivorce/apps/core/tests/test_logic.py @@ -5,7 +5,7 @@ from django.test import TestCase from edivorce.apps.core.models import BceidUser, UserResponse from edivorce.apps.core.utils.conditional_logic import get_cleaned_response_value, get_num_children_living_with from edivorce.apps.core.utils.user_response import get_data_for_user -from edivorce.apps.core.views.api import _content_type_from_filename +from edivorce.apps.core.models import Document class ConditionalLogicTestCase(TestCase): @@ -56,20 +56,17 @@ class ConditionalLogicTestCase(TestCase): class ViewLogic(TestCase): def test_content_type_from_filename(self): - self.assertEqual(_content_type_from_filename('test_file1.pdf'), 'application/pdf') - self.assertEqual(_content_type_from_filename('redis_key_test_file1_pdf'), 'application/pdf') - self.assertEqual(_content_type_from_filename('test_file2.png'), 'image/png') - self.assertEqual(_content_type_from_filename('redis_key_test_file2_png'), 'image/png') - self.assertEqual(_content_type_from_filename('Test File 3.GIF'), 'image/gif') - self.assertEqual(_content_type_from_filename('redis_key_test_file_3_GIF'), 'image/gif') - self.assertEqual(_content_type_from_filename('Test_File--4.JPEG'), 'image/jpeg') - self.assertEqual(_content_type_from_filename('redis_key_test_file_4_jpeg'), 'image/jpeg') - self.assertEqual(_content_type_from_filename('TestFile5.jpe'), 'image/jpeg') - self.assertEqual(_content_type_from_filename('redis_key_test_file_5_jpe'), 'image/jpeg') - self.assertEqual(_content_type_from_filename('testFile6.jpeg'), 'image/jpeg') - self.assertEqual(_content_type_from_filename('redis_key_testfile_6_jpeg'), 'image/jpeg') - - with self.assertRaises(TypeError): - _content_type_from_filename('test_file7.HEIC') - with self.assertRaises(TypeError): - _content_type_from_filename('redis_key_testfile_7_svg') + self.assertEqual(Document.content_type_from_filename('test_file1.pdf'), 'application/pdf') + self.assertEqual(Document.content_type_from_filename('redis_key_test_file1_pdf'), 'application/pdf') + self.assertEqual(Document.content_type_from_filename('test_file2.png'), 'image/png') + self.assertEqual(Document.content_type_from_filename('redis_key_test_file2_png'), 'image/png') + self.assertEqual(Document.content_type_from_filename('Test File 3.GIF'), 'image/gif') + self.assertEqual(Document.content_type_from_filename('redis_key_test_file_3_GIF'), 'image/gif') + self.assertEqual(Document.content_type_from_filename('Test_File--4.JPEG'), 'image/jpeg') + self.assertEqual(Document.content_type_from_filename('redis_key_test_file_4_jpeg'), 'image/jpeg') + self.assertEqual(Document.content_type_from_filename('TestFile5.jpe'), 'image/jpeg') + self.assertEqual(Document.content_type_from_filename('redis_key_test_file_5_jpe'), 'image/jpeg') + self.assertEqual(Document.content_type_from_filename('testFile6.jpeg'), 'image/jpeg') + self.assertEqual(Document.content_type_from_filename('redis_key_testfile_6_jpeg'), 'image/jpeg') + self.assertEqual(Document.content_type_from_filename('test_file7.HEIC'), 'application/unknown') + self.assertEqual(Document.content_type_from_filename('redis_key_testfile_7_svgg'), 'application/unknown')