Project

General

Profile

0001-assets-use-assets-views-instead-of-ckeditor-browser-.patch

Lauréline Guerin, 24 Apr 2020 10:25 AM

Download (11.6 KB)

View differences:

Subject: [PATCH] assets: use assets views instead of ckeditor browser (#40249)

 .../combo/manager_assets_browse.html          | 76 +++++++++++++++++++
 .../combo/manager_assets_fragment.html        |  8 +-
 combo/apps/assets/views.py                    | 17 ++++-
 combo/manager/static/css/combo.manager.css    |  4 +
 combo/manager/static/js/combo.manager.js      | 23 ++++--
 combo/manager/urls.py                         |  3 +-
 tests/test_manager.py                         | 44 +++++++++++
 7 files changed, 166 insertions(+), 9 deletions(-)
 create mode 100644 combo/apps/assets/templates/combo/manager_assets_browse.html
combo/apps/assets/templates/combo/manager_assets_browse.html
1
{% extends "gadjo/base.html" %}
2
{% load staticfiles i18n %}
3

  
4
{% block css %}
5
<link rel="stylesheet" type="text/css" media="all" href="{{ STATIC_URL }}css/combo.manager.css"/>
6
{% endblock %}
7

  
8
{% block extrascripts %}
9
<script src="{% static "js/combo.manager.js" %}"></script>
10
{% endblock %}
11

  
12
{% block user-links %}{% endblock %}
13
{% block sidepage %}{% endblock %}
14
{% block site-header %}{% endblock %}
15
{% block bodyargs %}class="no-header"{% endblock %}
16
{% block footer %}{% endblock %}
17

  
18
{% block appbar %}
19
<h2>{% trans "Browse for the file you want, then click 'Embed File' to continue..." %}</h2>
20
{% endblock %}
21

  
22
{% block content %}
23

  
24
{% if not object_list and not query %}
25
<div class="big-msg-info">
26
  {% trans "No files found. Upload files using the 'Image Button' or 'Link Button' dialog's 'Upload' tab." %}
27
</div>
28

  
29
{% else %}
30

  
31
<form>
32
  {% for k, v in request.GET.items %}
33
    {% if k != 'q' %}<input type="hidden" name="{{ k }}" value="{{ v }}" />{% endif %}
34
  {% endfor %}
35
  <p><input name="q" type="search" value="{{query}}"> <button>{% trans 'Search' %}</button>
36
  <span class="help_text">{% trans "(case insensitive search over filenames)" %}</span>
37
  </p>
38
</form>
39

  
40
<div id="assets-browser" class="assets-ckeditor">
41
  <div id="assets-listing">
42
    {% with asset_for_ckeditor=True %}
43
    {% include "combo/manager_assets_fragment.html" %}
44
    {% endwith %}
45
  </div>
46
  <div id="asset-preview"></div>
47
</div>
48

  
49
{% include "gadjo/pagination.html" %}
50

  
51
<div class="buttons">
52
  <input href="" id="asset-ckeditor-embed" type="submit" name="_embed" value="{% trans "Embed File" %}" disabled />
53
</div>
54

  
55
{% endif %}
56
<script type="text/javascript">
57
    // helper functions
58
    function getUrlParam(paramName) {
59
        var reParam = new RegExp('(?:[\?&]|&amp;)' + paramName + '=([^&]+)', 'i') ;
60
        var match = window.location.search.match(reParam) ;
61

  
62
        return (match && match.length > 1) ? match[1] : '' ;
63
    }
64

  
65
    // embedder
66
    $(document).on('click', '#asset-ckeditor-embed', function() {
67
        var funcNum = getUrlParam('CKEditorFuncNum');
68
        var fileUrl = $(this).attr('href');
69
        console.log(funcNum)
70
        console.log(fileUrl)
71
        window.opener.CKEDITOR.tools.callFunction(funcNum, fileUrl);
72
        window.close();
73
    });
74
    console.log(window.opener.CKEDITOR)
75
</script>
76
{% endblock %}
combo/apps/assets/templates/combo/manager_assets_fragment.html
5 5
      <th>{% trans "Name" %}</th>
6 6
      <th>{% trans "Size" %}</th>
7 7
      <th></th>
8
      {% if not asset_for_ckeditor %}
8 9
      <th></th>
10
      {% endif %}
9 11
    </tr>
10 12
  </thead>
11 13
  <tbody>
12 14
    {% for asset in object_list %}
13
    <tr class="{{ asset.css_classes }}">
14
      <td><a href="{{ asset.src }}">{{ asset.name }}</a></td>
15
    <tr class="{{ asset.css_classes }}" data-href="{{ asset.src }}">
16
      <td>{% if not asset_for_ckeditor %}<a href="{{ asset.src }}">{{ asset.name }}</a>{% else %}{{ asset.name }}{% endif %}</td>
15 17
      <td>{% if asset.size %}{{ asset.size|filesizeformat }}{% else %}-{% endif %}</td>
16 18
      <td class="image">{% if asset.is_image %}<img data-href="{{ asset.src }}" src="{{ asset.thumb }}"/>{% endif %}</td>
19
      {% if not asset_for_ckeditor %}
17 20
      <td class="actions">
18 21
        {% if asset.key %}{# theme asset #}
19 22
        <a href="{% url 'combo-manager-slot-asset-upload' key=asset.key %}{% if cell_reference %}?cell_reference={{ cell_reference }}{% endif %}"
......
29 32
           class="delete" rel="popup">{% trans 'Delete' %}</a>
30 33
        {% endif %}
31 34
      </td>
35
      {% endif %}
32 36
    </tr>
33 37
    {% endfor %}
34 38
  </tbody>
combo/apps/assets/views.py
119 119
    template_name = 'combo/manager_assets.html'
120 120
    paginate_by = 10
121 121

  
122
    def get_files(self):
123
        return list(SlotAsset.get_assets()) + CkEditorAsset.get_assets(self.request)
124

  
122 125
    def get_queryset(self):
123
        files = list(SlotAsset.get_assets()) + CkEditorAsset.get_assets(self.request)
126
        files = self.get_files()
124 127
        q = self.request.GET.get('q')
125 128
        if q:
126 129
            files = [x for x in files if q.lower() in x.name.lower()]
......
142 145
            return url + '?page=%s' % ((i // self.paginate_by) + 1)
143 146
        return url
144 147

  
148

  
145 149
assets = Assets.as_view()
146 150

  
147 151

  
152
class AssetsBrowse(Assets):
153
    template_name = 'combo/manager_assets_browse.html'
154
    paginate_by = 7
155

  
156
    def get_files(self):
157
        return CkEditorAsset.get_assets(self.request)
158

  
159

  
160
browse = AssetsBrowse.as_view()
161

  
162

  
148 163
class AssetUpload(FormView):
149 164
    form_class = AssetUploadForm
150 165
    template_name = 'combo/manager_asset_upload.html'
combo/manager/static/css/combo.manager.css
1
body.no-header #header {
2
	display: none;
3
}
4

  
1 5
div#meta {
2 6
	margin-bottom: 1em;
3 7
}
combo/manager/static/js/combo.manager.js
284 284
      $(this).addClass('untoggled');
285 285
    }
286 286
  });
287
  $('#assets-browser table tr').on('hover mouseenter', function() {
288
    var $img = $(this).find('img');
287

  
288
  function show_asset($elem) {
289
    var $img = $elem.find('img');
289 290
    if ($img.data('href')) {
290 291
      $('#asset-preview').empty().append($('<img src="' + $img.data('href') + '"/>'));
291 292
    } else {
292 293
      $('#asset-preview').empty();
293 294
    }
294 295
    return true;
295
  }).on('mouseleave', function() {
296
    $('#asset-preview').empty();
297
  });
296
  }
297
  if ($('#assets-browser.assets-ckeditor').length) {
298
    $('#assets-browser table tr').on('click', function() {
299
      $('#assets-browser table tr').removeClass('active');
300
      $(this).addClass('active');
301
      show_asset($(this));
302
      $('#asset-ckeditor-embed').attr('href', $(this).data('href')).prop('disabled', false);
303
    });
304
  } else {
305
    $('#assets-browser table tr').on('hover mouseenter', function() {
306
      show_asset($(this));
307
    }).on('mouseleave', function() {
308
      $('#asset-preview').empty();
309
    });
310
  }
298 311
  $('.manager-add-new-cell a').on('click', function() {
299 312
    $(this).next().toggle();
300 313
    return false;
combo/manager/urls.py
20 20

  
21 21
import ckeditor.views as ckeditor_views
22 22

  
23
from combo.apps.assets import views as assets_views
23 24
from .. import plugins
24 25
from . import views
25 26

  
......
94 95
            name='combo-manager-page-order'),
95 96
        url(r'^ckeditor/upload/', staff_member_required(ckeditor_views.upload),
96 97
            name='ckeditor_upload'),
97
        url(r'^ckeditor/browse/', never_cache(staff_member_required(ckeditor_views.browse)),
98
        url(r'^ckeditor/browse/', never_cache(staff_member_required(assets_views.browse)),
98 99
            name='ckeditor_browse'),
99 100
]
100 101

  
tests/test_manager.py
1041 1041
    app.get('/logout/')
1042 1042
    assert app.get('/manage/', status=302).location.endswith('/login/?next=/manage/')
1043 1043

  
1044

  
1044 1045
def test_asset_management(app, admin_user):
1045 1046
    app = login(app)
1046 1047
    resp = app.get('/manage/assets/')
......
1099 1100
    resp = resp.form.submit().follow()
1100 1101
    assert 'have any asset yet.' in resp.text
1101 1102

  
1103

  
1102 1104
def test_asset_management_anchor(app, admin_user):
1103 1105
    app = login(app)
1104 1106
    resp = app.get('/manage/assets/')
......
1134 1136
    resp = resp.follow()
1135 1137
    assert 'test.png' not in resp.text
1136 1138

  
1139

  
1137 1140
def test_asset_management_search(app, admin_user):
1138 1141
    app = login(app)
1139 1142

  
......
1156 1159
    assert resp.text.count('<tr class="asset') == 2
1157 1160

  
1158 1161

  
1162
def test_assets_browse(app, admin_user):
1163
    app = login(app)
1164

  
1165
    resp = app.get('/manage/ckeditor/browse/?CKEditor=foo&CKEditorFuncNum=0')
1166
    assert 'No files found' in resp.text
1167

  
1168
    filepath = os.path.join(settings.CKEDITOR_UPLOAD_PATH, 'aa.png')
1169
    pix = b'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQAAAAA3bvkkAAAACklEQVQI12NoAAAAggCB3UNq9AAAAABJRU5ErkJggg=='
1170
    default_storage.save(filepath, BytesIO(base64.decodebytes(pix)))
1171
    filepath = os.path.join(settings.CKEDITOR_UPLOAD_PATH, 'bb.pdf')
1172
    pix = b'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQAAAAA3bvkkAAAACklEQVQI12NoAAAAggCB3UNq9AAAAABJRU5ErkJggg=='
1173
    default_storage.save(filepath, BytesIO(base64.decodebytes(pix)))
1174
    filepath = os.path.join(settings.CKEDITOR_UPLOAD_PATH, 'cc.png')
1175
    pix = b'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQAAAAA3bvkkAAAACklEQVQI12NoAAAAggCB3UNq9AAAAABJRU5ErkJggg=='
1176
    default_storage.save(filepath, BytesIO(base64.decodebytes(pix)))
1177

  
1178
    resp = app.get('/manage/ckeditor/browse/?CKEditor=foo&CKEditorFuncNum=0')
1179
    assert 'No files found' not in resp.text
1180

  
1181
    resp.form['q'] = 'test'
1182
    resp = resp.form.submit()
1183
    assert 'CKEditor=foo' in resp.request.url
1184
    assert 'CKEditorFuncNum=0' in resp.request.url
1185
    assert resp.text.count('<tr class="asset') == 0
1186

  
1187
    resp.form['q'] = 'png'
1188
    resp = resp.form.submit()
1189
    assert 'CKEditor=foo' in resp.request.url
1190
    assert 'CKEditorFuncNum=0' in resp.request.url
1191
    assert resp.text.count('<tr class="asset') == 2
1192

  
1193
    for i in range(25):
1194
        filepath = os.path.join(settings.CKEDITOR_UPLOAD_PATH, 'cc.png')
1195
        pix = b'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQAAAAA3bvkkAAAACklEQVQI12NoAAAAggCB3UNq9AAAAABJRU5ErkJggg=='
1196
        default_storage.save(filepath, BytesIO(base64.decodebytes(pix)))
1197
    resp = app.get('/manage/ckeditor/browse/?CKEditor=foo&CKEditorFuncNum=0')
1198
    resp = resp.click(href='.*page=2.*')
1199
    assert 'CKEditor=foo' in resp.request.url
1200
    assert 'CKEditorFuncNum=0' in resp.request.url
1201

  
1202

  
1159 1203
def test_asset_slots_management(app, admin_user):
1160 1204
    app = login(app)
1161 1205
    assert Asset.objects.count() == 0
1162
-