summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFrédéric Péters <fpeters@entrouvert.com>2017-12-25 18:08:26 (GMT)
committerFrédéric Péters <fpeters@entrouvert.com>2017-12-30 10:50:05 (GMT)
commitf41fe4dd1ba8874b416c6a808defd737d1c0f585 (patch)
treeb08d0d6d0dceb54351a8e66ebe383965ad18d546
parent7e7db9366ab28c63b87dfce754be7fa9f600f2cc (diff)
downloadcombo-wip/assets-management.zip
combo-wip/assets-management.tar.gz
combo-wip/assets-management.tar.bz2
manager: add action to overwrite an asset (#11491)wip/assets-management
-rw-r--r--combo/manager/static/css/combo.manager.css11
-rw-r--r--combo/manager/templates/combo/manager_asset_overwrite.html21
-rw-r--r--combo/manager/templates/combo/manager_assets.html2
-rw-r--r--combo/manager/urls.py1
-rw-r--r--combo/manager/views.py34
-rw-r--r--tests/test_manager.py19
6 files changed, 87 insertions, 1 deletions
diff --git a/combo/manager/static/css/combo.manager.css b/combo/manager/static/css/combo.manager.css
index 534532f..6fd8502 100644
--- a/combo/manager/static/css/combo.manager.css
+++ b/combo/manager/static/css/combo.manager.css
@@ -207,9 +207,10 @@ p#redirection {
}
#assets-browser #assets-listing table td.actions {
- width: 3em;
+ width: 80px;
}
+#assets-browser #assets-listing table td.actions a.overwrite,
#assets-browser #assets-listing table td.actions a.delete {
display: inline-block;
border: none;
@@ -227,6 +228,14 @@ p#redirection {
width: 30px;
}
+#assets-browser #assets-listing table td.actions a.overwrite::before {
+ text-align: center;
+ font-family: FontAwesome;
+ content: "\f093"; /* upload */
+ display: inline-block;
+ width: 30px;
+}
+
#assets-browser tr::before {
font-family: FontAwesome;
vertical-align: middle;
diff --git a/combo/manager/templates/combo/manager_asset_overwrite.html b/combo/manager/templates/combo/manager_asset_overwrite.html
new file mode 100644
index 0000000..a7b4422
--- /dev/null
+++ b/combo/manager/templates/combo/manager_asset_overwrite.html
@@ -0,0 +1,21 @@
+{% extends "combo/manager_base.html" %}
+{% load i18n %}
+
+{% block appbar %}
+<h2>{% trans "Overwrite Asset" %}</h2>
+{% endblock %}
+
+{% block content %}
+
+<form method="post" enctype="multipart/form-data">
+ {% csrf_token %}
+ <p>
+ {% trans "This will erase the existing file and replace it with a new one." %}
+ </p>
+ {{ form.as_p }}
+ <div class="buttons">
+ <button class="submit-button">{% trans "Upload" %}</button>
+ <a class="cancel" href="{% url 'combo-manager-assets' %}">{% trans 'Cancel' %}</a>
+ </div>
+</form>
+{% endblock %}
diff --git a/combo/manager/templates/combo/manager_assets.html b/combo/manager/templates/combo/manager_assets.html
index 1093837..21992c9 100644
--- a/combo/manager/templates/combo/manager_assets.html
+++ b/combo/manager/templates/combo/manager_assets.html
@@ -47,6 +47,8 @@
<td>{{ asset.size|filesizeformat }}</td>
<td class="image">{% if asset.is_image %}<img data-href="{{ asset.src }}" src="{{ asset.thumb }}"/>{% endif %}</td>
<td class="actions">
+ <a href="{% url 'combo-manager-asset-overwrite' %}?img={{asset.filepath|iriencode}}"
+ class="overwrite" rel="popup">{% trans 'Overwrite' %}</a>
<a href="{% url 'combo-manager-asset-delete' %}?img={{asset.filepath|iriencode}}"
class="delete" rel="popup">{% trans 'Delete' %}</a>
</td>
diff --git a/combo/manager/urls.py b/combo/manager/urls.py
index 0fee589..84b8807 100644
--- a/combo/manager/urls.py
+++ b/combo/manager/urls.py
@@ -68,6 +68,7 @@ urlpatterns = [
name='combo-manager-page-order'),
url(r'^assets/$', views.assets, name='combo-manager-assets'),
url(r'^assets/delete$', views.asset_delete, name='combo-manager-asset-delete'),
+ url(r'^assets/overwrite/$', views.asset_overwrite, name='combo-manager-asset-overwrite'),
url(r'^assets/upload/$', views.asset_upload, name='combo-manager-asset-upload'),
url(r'^ckeditor/', include('ckeditor.urls')),
]
diff --git a/combo/manager/views.py b/combo/manager/views.py
index efdbff6..ebfc6d6 100644
--- a/combo/manager/views.py
+++ b/combo/manager/views.py
@@ -445,6 +445,40 @@ class AssetUpload(FormView):
asset_upload = AssetUpload.as_view()
+class AssetOverwrite(FormView):
+ form_class = AssetUploadForm
+ template_name = 'combo/manager_asset_overwrite.html'
+ success_url = reverse_lazy('combo-manager-assets')
+
+ def form_valid(self, form):
+ img_orig = self.request.GET['img']
+ if '..' in img_orig:
+ raise PermissionDenied() # better safe than sorry
+ base_path = settings.CKEDITOR_UPLOAD_PATH
+ if getattr(settings, 'CKEDITOR_RESTRICT_BY_USER', False):
+ base_path = os.path.join(base_path, request.user.username)
+ if not img_orig.startswith(base_path):
+ raise PermissionDenied()
+
+ upload = self.request.FILES['upload']
+ default_storage.delete(img_orig)
+ if getattr(settings, 'CKEDITOR_IMAGE_BACKEND', None):
+ thumb = ckeditor.utils.get_thumb_filename(img_orig)
+ default_storage.delete(thumb)
+ saved_path = default_storage.save(img_orig, upload)
+ backend = ckeditor.image_processing.get_backend()
+ try:
+ backend.image_verify(upload)
+ except ckeditor.utils.NotAnImageException:
+ pass
+ else:
+ if backend.should_create_thumbnail(saved_path):
+ backend.create_thumbnail(saved_path)
+ return super(AssetOverwrite, self).form_valid(form)
+
+asset_overwrite = AssetOverwrite.as_view()
+
+
class AssetDelete(TemplateView):
template_name = 'combo/manager_asset_confirm_delete.html'
diff --git a/tests/test_manager.py b/tests/test_manager.py
index c08018e..275a333 100644
--- a/tests/test_manager.py
+++ b/tests/test_manager.py
@@ -584,6 +584,25 @@ def test_asset_management(app, admin_user):
resp = app.get('/manage/assets/')
assert 'have any asset yet.' not in resp.body
+ # check there is also a thumbnail
+ thumbnail_filename = re.findall('src="/media/(.*thumb.*)"', resp.body)[0]
+ thumbnail_path = default_storage.path(thumbnail_filename)
+ assert os.path.exists(thumbnail_path)
+ thumbnail_contents = open(thumbnail_path).read()
+
+ # check overwriting
+ resp = resp.click('Overwrite')
+ resp.form['upload'] = Upload('test.png',
+ base64.decodestring('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAAAAAA6fptVAAAACklEQVQI12NgAgAABAADRWoApgAA\nAABJRU5ErkJggg=='),
+ 'image/png')
+ resp = resp.form.submit().follow()
+
+ resp.click('test.png')
+ assert re.findall('src="/media/(.*thumb.*)"', resp.body)[0] == thumbnail_filename
+ assert os.path.exists(thumbnail_path)
+ thumbnail_contents_new = open(thumbnail_path).read()
+ assert thumbnail_contents_new != thumbnail_contents
+
# test deletion
resp = resp.click('Delete')
assert 'Are you sure you want to delete' in resp.body