Replace Document, still some cache issues, fixes #2855

This commit is contained in:
j 2015-10-25 22:08:13 +01:00
parent 21a6007f8c
commit 6f9fb06da3
3 changed files with 46 additions and 18 deletions

View file

@ -1,6 +1,8 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4 # vi:si:et:sw=4:sts=4:ts=4
from __future__ import division from __future__ import division
import os
from glob import glob
import ox import ox
from ox.utils import json from ox.utils import json
@ -317,11 +319,11 @@ def thumbnail(request, id, size=256, page=None):
def upload(request): def upload(request):
if 'id' in request.GET: if 'id' in request.GET:
file = models.Document.get(request.GET['id']) file = models.Document.get(request.GET['id'])
elif 'id' in request.POST:
file = models.Document.get(request.POST['id'])
else: else:
file = None file = None
extension = request.POST['filename'].split('.') name, extension = request.POST['filename'].rsplit('.', 1)
name = '.'.join(extension[:-1])
extension = extension[-1].lower()
response = json_response(status=400, text='this request requires POST') response = json_response(status=400, text='this request requires POST')
if 'chunk' in request.FILES: if 'chunk' in request.FILES:
if file.editable(request.user): if file.editable(request.user):
@ -348,8 +350,15 @@ def upload(request):
file.save() file.save()
else: else:
#replace existing file #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 file.uploading = True
name, extension = request.POST['filename'].rsplit('.', 1)
file.extension = extension
file.save() file.save()
upload_url = request.build_absolute_uri('/api/upload/document?id=%s' % file.get_id()) upload_url = request.build_absolute_uri('/api/upload/document?id=%s' % file.get_id())
return render_to_json_response({ return render_to_json_response({

View file

@ -253,7 +253,7 @@ pandora.ui.documentsPanel = function(options) {
{id: 'add', title: ''}, {id: 'add', title: ''},
{id: 'embed', title: Ox._('Embed Document...')}, {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'} {id: 'delete', title: '', keyboard: 'delete'}
], ],
title: 'set', title: 'set',
@ -276,7 +276,7 @@ pandora.ui.documentsPanel = function(options) {
} else if (data.id == 'remove') { } else if (data.id == 'remove') {
removeDocuments(); removeDocuments();
} else if (data.id == 'replace') { } else if (data.id == 'replace') {
replaceDocument(data.files); replaceDocument(data);
} else if (data.id == 'upload') { } else if (data.id == 'upload') {
uploadDocuments(data); uploadDocuments(data);
} }
@ -509,8 +509,22 @@ pandora.ui.documentsPanel = function(options) {
).open(); ).open();
} }
function replaceDocument(file) { function replaceDocument(data) {
var id = $list.options('selected')[0]; 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() { function removeDocuments() {
@ -816,7 +830,7 @@ pandora.ui.documentsPanel = function(options) {
function renderList() { function renderList() {
var options = { var options = {
items: pandora.api.findDocuments, 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: { query: {
conditions: isItemView ? [{ key: 'item', value: ui.item, operator: '==' }] : [], conditions: isItemView ? [{ key: 'item', value: ui.item, operator: '==' }] : [],
operator: '&' operator: '&'
@ -845,7 +859,7 @@ pandora.ui.documentsPanel = function(options) {
id: data.id, id: data.id,
info: info, info: info,
title: data.name, 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) width: Math.round(data.ratio > 1 ? size : size * data.ratio)
}; };
}, },
@ -973,7 +987,6 @@ pandora.ui.documentsPanel = function(options) {
hasListSelection && ui.listSelection.length > 1 hasListSelection && ui.listSelection.length > 1
? 'plural' : 'singular']) ? 'plural' : 'singular'])
])) ]))
.setItemTitle('replace', Ox._('Replace {0}...', [string]))
.setItemTitle('delete', Ox._('Delete {0}...', [string])) .setItemTitle('delete', Ox._('Delete {0}...', [string]))
[selected.length && (hasItemView || hasListSelection) ? 'enableItem' : 'disableItem']('add') [selected.length && (hasItemView || hasListSelection) ? 'enableItem' : 'disableItem']('add')
[selected.length ? 'enableItem' : 'disableItem']('embed') [selected.length ? 'enableItem' : 'disableItem']('embed')
@ -1035,7 +1048,9 @@ pandora.ui.documentsPanel = function(options) {
} }
function uploadDocuments(data) { function uploadDocuments(data) {
pandora.ui.uploadDocumentDialog(data.files, function(files) { pandora.ui.uploadDocumentDialog({
files: data.files
}, function(files) {
if (files) { if (files) {
Ox.Request.clearCache('findDocuments'); Ox.Request.clearCache('findDocuments');
$list.bindEventOnce({ $list.bindEventOnce({

View file

@ -1,9 +1,9 @@
// vim: et:ts=4:sw=4:sts=4:ft=javascript // vim: et:ts=4:sw=4:sts=4:ft=javascript
'use strict'; 'use strict';
pandora.ui.uploadDocumentDialog = function(files, callback) { pandora.ui.uploadDocumentDialog = function(options, callback) {
var files = options.files,
var extensions = files.map(function(file) { extensions = files.map(function(file) {
return file.name.split('.').pop().toLowerCase() return file.name.split('.').pop().toLowerCase()
}), }),
@ -137,16 +137,20 @@ pandora.ui.uploadDocumentDialog = function(files, callback) {
} }
function uploadFile(part) { function uploadFile(part) {
var file = files[part], var data = {
},
file = files[part],
extension = file.name.split('.').pop().toLowerCase(), extension = file.name.split('.').pop().toLowerCase(),
filename = file.name.split('.').slice(0, -1).join('.') + '.' filename = file.name.split('.').slice(0, -1).join('.') + '.'
+ (extension == 'jpeg' ? 'jpg' : extension); + (extension == 'jpeg' ? 'jpg' : extension);
$text.html(Ox._('Uploading {0}', [file.name])); $text.html(Ox._('Uploading {0}', [file.name]));
if (options.id) {
data.id = options.id;
}
data.filename = filename;
upload = pandora.chunkupload({ upload = pandora.chunkupload({
data: { data: data,
filename: filename
},
file: file, file: file,
url: '/api/upload/document/', url: '/api/upload/document/',
}) })