Browse Source

restoring upload state #1

pull/170/head
Michael Olund 5 years ago
parent
commit
9227578a9b
3 changed files with 64 additions and 24 deletions
  1. +21
    -0
      edivorce/apps/core/models.py
  2. +3
    -20
      edivorce/apps/core/views/api.py
  3. +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.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):


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

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


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

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


Loading…
Cancel
Save