From 6f9fb06da3f76b438544c056a69eaf85eb4f4e8a Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Sun, 25 Oct 2015 22:08:13 +0100 Subject: [PATCH] Replace Document, still some cache issues, fixes #2855 --- pandora/document/views.py | 17 +++++++++++++---- static/js/documentsPanel.js | 29 ++++++++++++++++++++++------- static/js/uploadDocumentDialog.js | 18 +++++++++++------- 3 files changed, 46 insertions(+), 18 deletions(-) diff --git a/pandora/document/views.py b/pandora/document/views.py index 7faf2343..6ffb2ffa 100644 --- a/pandora/document/views.py +++ b/pandora/document/views.py @@ -1,6 +1,8 @@ # -*- coding: utf-8 -*- # vi:si:et:sw=4:sts=4:ts=4 from __future__ import division +import os +from glob import glob import ox from ox.utils import json @@ -317,11 +319,11 @@ def thumbnail(request, id, size=256, page=None): def upload(request): if 'id' in request.GET: file = models.Document.get(request.GET['id']) + elif 'id' in request.POST: + file = models.Document.get(request.POST['id']) else: file = None - extension = request.POST['filename'].split('.') - name = '.'.join(extension[:-1]) - extension = extension[-1].lower() + name, extension = request.POST['filename'].rsplit('.', 1) response = json_response(status=400, text='this request requires POST') if 'chunk' in request.FILES: if file.editable(request.user): @@ -348,8 +350,15 @@ def upload(request): file.save() else: #replace existing file - file.file.delete() + if file.file: + folder = os.path.dirname(file.file.path) + for f in glob('%s/*' % folder): + if f != file.file.path: + os.unlink(f) + file.file.delete() file.uploading = True + name, extension = request.POST['filename'].rsplit('.', 1) + file.extension = extension file.save() upload_url = request.build_absolute_uri('/api/upload/document?id=%s' % file.get_id()) return render_to_json_response({ diff --git a/static/js/documentsPanel.js b/static/js/documentsPanel.js index a2259f72..8a7baee7 100644 --- a/static/js/documentsPanel.js +++ b/static/js/documentsPanel.js @@ -253,7 +253,7 @@ pandora.ui.documentsPanel = function(options) { {id: 'add', title: ''}, {id: 'embed', title: Ox._('Embed Document...')}, {}, - {id: 'replace', title: Ox._('Replace {0}...', [Ox._('Document')])}, + {id: 'replace', title: Ox._('Replace {0}...', [Ox._('Document')]), file: {width: 192}}, {id: 'delete', title: '', keyboard: 'delete'} ], title: 'set', @@ -276,7 +276,7 @@ pandora.ui.documentsPanel = function(options) { } else if (data.id == 'remove') { removeDocuments(); } else if (data.id == 'replace') { - replaceDocument(data.files); + replaceDocument(data); } else if (data.id == 'upload') { uploadDocuments(data); } @@ -509,8 +509,22 @@ pandora.ui.documentsPanel = function(options) { ).open(); } - function replaceDocument(file) { + function replaceDocument(data) { var id = $list.options('selected')[0]; + pandora.ui.uploadDocumentDialog({ + files: data.files, + id: id + }, function(files) { + if (files) { + Ox.Request.clearCache(); + $list.bindEventOnce({ + load: function() { + $list.options({selected: files.ids}); + } + }) + .reloadList(); + } + }).open(); } function removeDocuments() { @@ -816,7 +830,7 @@ pandora.ui.documentsPanel = function(options) { function renderList() { var options = { items: pandora.api.findDocuments, - keys: ['description', 'dimensions', 'extension', 'id', 'name', 'ratio', 'size', 'user', 'entities'], + keys: ['description', 'dimensions', 'extension', 'id', 'name', 'ratio', 'size', 'user', 'entities', 'modified'], query: { conditions: isItemView ? [{ key: 'item', value: ui.item, operator: '==' }] : [], operator: '&' @@ -845,7 +859,7 @@ pandora.ui.documentsPanel = function(options) { id: data.id, info: info, title: data.name, - url: pandora.getMediaURL('/documents/' + data.id + '/256p.jpg'), + url: pandora.getMediaURL('/documents/' + data.id + '/256p.jpg?' + data.modified), width: Math.round(data.ratio > 1 ? size : size * data.ratio) }; }, @@ -973,7 +987,6 @@ pandora.ui.documentsPanel = function(options) { hasListSelection && ui.listSelection.length > 1 ? 'plural' : 'singular']) ])) - .setItemTitle('replace', Ox._('Replace {0}...', [string])) .setItemTitle('delete', Ox._('Delete {0}...', [string])) [selected.length && (hasItemView || hasListSelection) ? 'enableItem' : 'disableItem']('add') [selected.length ? 'enableItem' : 'disableItem']('embed') @@ -1035,7 +1048,9 @@ pandora.ui.documentsPanel = function(options) { } function uploadDocuments(data) { - pandora.ui.uploadDocumentDialog(data.files, function(files) { + pandora.ui.uploadDocumentDialog({ + files: data.files + }, function(files) { if (files) { Ox.Request.clearCache('findDocuments'); $list.bindEventOnce({ diff --git a/static/js/uploadDocumentDialog.js b/static/js/uploadDocumentDialog.js index 29ffad35..21423418 100644 --- a/static/js/uploadDocumentDialog.js +++ b/static/js/uploadDocumentDialog.js @@ -1,9 +1,9 @@ // vim: et:ts=4:sw=4:sts=4:ft=javascript 'use strict'; -pandora.ui.uploadDocumentDialog = function(files, callback) { - - var extensions = files.map(function(file) { +pandora.ui.uploadDocumentDialog = function(options, callback) { + var files = options.files, + extensions = files.map(function(file) { return file.name.split('.').pop().toLowerCase() }), @@ -137,16 +137,20 @@ pandora.ui.uploadDocumentDialog = function(files, callback) { } function uploadFile(part) { - var file = files[part], + var data = { + }, + file = files[part], extension = file.name.split('.').pop().toLowerCase(), filename = file.name.split('.').slice(0, -1).join('.') + '.' + (extension == 'jpeg' ? 'jpg' : extension); $text.html(Ox._('Uploading {0}', [file.name])); + if (options.id) { + data.id = options.id; + } + data.filename = filename; upload = pandora.chunkupload({ - data: { - filename: filename - }, + data: data, file: file, url: '/api/upload/document/', })