manager: add action to overwrite an asset (#11491)
This commit is contained in:
parent
8f6e102720
commit
f23083b9b6
|
@ -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;
|
||||
|
|
|
@ -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 %}
|
|
@ -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>
|
||||
|
|
|
@ -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')),
|
||||
]
|
||||
|
|
|
@ -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'
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue