Browse Source

Merge pull request #115 from bcgov/DIV-1120

restoring upload state #1
pull/170/head
Michael Olund 5 years ago
committed by GitHub
parent
commit
9797a59768
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 79 additions and 42 deletions
  1. +21
    -0
      edivorce/apps/core/models.py
  2. +15
    -18
      edivorce/apps/core/tests/test_logic.py
  3. +3
    -20
      edivorce/apps/core/views/api.py
  4. +40
    -4
      vue/src/components/Uploader/Uploader.vue

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

@ -1,3 +1,5 @@
import re
from django.contrib import admin from django.contrib import admin
from django.db import models from django.db import models
from django.db.models import F from django.db.models import F
@ -173,6 +175,9 @@ class Document(models.Model):
def get_file_url(self): 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}) 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): 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 = 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) q.update(sort_order=F('sort_order') - 1)
@ -181,6 +186,22 @@ class Document(models.Model):
def get_file(file_key): def get_file(file_key):
return redis.RedisStorage().open(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: class DontLog:
def log_addition(self, *args): def log_addition(self, *args):


+ 15
- 18
edivorce/apps/core/tests/test_logic.py View File

@ -5,7 +5,7 @@ from django.test import TestCase
from edivorce.apps.core.models import BceidUser, UserResponse 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.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.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): class ConditionalLogicTestCase(TestCase):
@ -56,20 +56,17 @@ class ConditionalLogicTestCase(TestCase):
class ViewLogic(TestCase): class ViewLogic(TestCase):
def test_content_type_from_filename(self): 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')

+ 3
- 20
edivorce/apps/core/views/api.py View File

@ -1,5 +1,3 @@
import re
import graphene import graphene
import graphene_django import graphene_django
from django.http import Http404, HttpResponse, HttpResponseGone from django.http import Http404, HttpResponse, HttpResponseGone
@ -82,7 +80,7 @@ class DocumentView(RetrieveUpdateDestroyAPIView):
def retrieve(self, request, *args, **kwargs): def retrieve(self, request, *args, **kwargs):
""" Return the file instead of meta data """ """ Return the file instead of meta data """
document = self.get_object() 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 # If file doesn't exist anymore, delete it
try: try:
@ -95,31 +93,16 @@ class DocumentView(RetrieveUpdateDestroyAPIView):
def get_document_file_by_key(request, file_key): def get_document_file_by_key(request, file_key):
file = Document.get_file(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: try:
return HttpResponse(file, content_type=content_type) return HttpResponse(file, content_type=content_type)
except TypeError: except TypeError:
raise Http404("File not found") 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): class DocumentType(graphene_django.DjangoObjectType):
file_url = graphene.String(source='get_file_url') file_url = graphene.String(source='get_file_url')
content_type = graphene.String(source='get_content_type')
class Meta: class Meta:
model = Document model = Document


+ 40
- 4
vue/src/components/Uploader/Uploader.vue View File

@ -248,8 +248,8 @@ export default {
newFile.rotation = 0; newFile.rotation = 0;
const img = new Image(); const img = new Image();
img.onload = function() { 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; img.src = newFile.objectURL;
} }
@ -261,7 +261,10 @@ export default {
const url = `${urlbase}/${this.docType}/${this.party}/${file.size}/${encFilename}`; const url = `${urlbase}/${this.docType}/${this.party}/${file.size}/${encFilename}`;
axios.delete(url) axios.delete(url)
.then(response => { .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) => { .catch((error) => {
this.showError('Error deleting document from the server: ' + file.name); this.showError('Error deleting document from the server: ' + file.name);
@ -320,7 +323,7 @@ export default {
query: ` query: `
mutation updateMetadata { mutation updateMetadata {
updateMetadata(input:${graphQLData}){ updateMetadata(input:${graphQLData}){
documents{filename size width height rotation}
documents{filename size width height rotation contentType}
} }
} }
`}) `})
@ -334,6 +337,39 @@ export default {
} }
}, },
created() { 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 // call the API to update the metadata every second, but only if
// the data has changed (throttling requests because rotating and // the data has changed (throttling requests because rotating and
// re-ordering images can cause a lot of traffic and possibly // re-ordering images can cause a lot of traffic and possibly


Loading…
Cancel
Save