Projet

Général

Profil

0001-assets-extends-assets-stable-URI-with-sorl.thumbnail.patch

Lauréline Guérin, 15 octobre 2019 15:01

Télécharger (4,92 ko)

Voir les différences:

Subject: [PATCH] assets: extends assets stable URI with sorl.thumbnail options
 (#25039)

 combo/apps/assets/views.py | 26 ++++++++++++++++---
 tests/test_manager.py      | 53 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 75 insertions(+), 4 deletions(-)
combo/apps/assets/views.py
23 23
from django.core.files.storage import default_storage
24 24
from django.core.urlresolvers import reverse, reverse_lazy
25 25
from django.http import Http404, HttpResponse
26
from django.shortcuts import get_object_or_404
26 27
from django.shortcuts import redirect
27 28
from django.utils.six import BytesIO
28 29
from django.utils.translation import ugettext_lazy as _
......
285 286

  
286 287

  
287 288
def serve_asset(request, key):
288
    try:
289
        asset = Asset.objects.get(key=key)
290
        return redirect(asset.asset.url)
291
    except (Asset.DoesNotExist, AttributeError):
289
    asset = get_object_or_404(Asset, key=key)
290

  
291
    if not os.path.exists(asset.asset.path):
292 292
        raise Http404()
293

  
294
    # get options for thumbnail
295
    thumb_options = request.GET.dict()
296
    width = thumb_options.pop('width', None)
297
    height = thumb_options.pop('height', None)
298

  
299
    geometry_string = ''
300
    if width:
301
        geometry_string += width
302
    if height:
303
        geometry_string += 'x%s' % height
304

  
305
    # no thumbnail whithout geometry_string or for a svg file
306
    if not geometry_string or asset.asset.name.endswith('svg'):
307
        return redirect(asset.asset.url)
308

  
309
    # get or create thumbnail and return url
310
    return redirect(get_thumbnail(asset.asset, geometry_string, **thumb_options).url)
tests/test_manager.py
904 904
        assert '>CGU<' in resp.text
905 905

  
906 906

  
907
def test_serve_asset(settings, app, admin_user):
908
    settings.COMBO_ASSET_SLOTS = {'collectivity:banner': {'label': 'Banner'}}
909
    app = login(app)
910

  
911
    resp = app.get('/manage/assets/')
912
    assert '>Banner<' in resp.text
913
    assert '>Delete<' not in resp.text
914
    resp = resp.click('Overwrite')
915
    resp.form['upload'] = Upload(
916
        'test.png',
917
        base64.decodestring(b'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAAAAAA6fptVAAAACklEQVQI12NgAgAABAADRWoApgAA\nAABJRU5ErkJggg=='),
918
        'image/png')
919
    resp = resp.form.submit().follow()
920
    assert Asset.objects.filter(key='collectivity:banner').count() == 1
921
    asset = Asset.objects.latest('pk')
922

  
923
    assert app.get('/assets/collectivity:banner', status=302)['location'].endswith('test.png')
924
    assert app.get('/assets/collectivity:banner?width=200', status=302)['location'].startswith('/media/cache/')
925
    assert app.get('/assets/collectivity:banner?height=200', status=302)['location'].startswith('/media/cache/')
926
    assert app.get('/assets/collectivity:banner?crop=center', status=302)['location'].endswith('test.png')
927
    assert app.get('/assets/collectivity:banner?width=200&crop=center', status=302)['location'].startswith('/media/cache/')
928

  
929
    # file is missing
930
    os.remove(asset.asset.path)
931
    app.get('/assets/collectivity:banner', status=404)
932

  
933
    # upload a SVG
934
    resp = resp.click('Delete')
935
    resp = resp.form.submit()
936
    assert Asset.objects.filter(key='collectivity:banner').count() == 0
937

  
938
    resp = app.get('/manage/assets/')
939
    assert '>Banner<' in resp.text
940
    assert '>Delete<' not in resp.text
941
    resp = resp.click('Overwrite')
942
    resp.form['upload'] = Upload(
943
        'test.svg',
944
        base64.decodestring(b'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAAAAAA6fptVAAAACklEQVQI12NgAgAABAADRWoApgAA\nAABJRU5ErkJggg=='),
945
        'image/svg+xml')
946
    resp = resp.form.submit()
947
    assert Asset.objects.filter(key='collectivity:banner').count() == 1
948

  
949
    # SVG: no thumbnail !
950
    assert app.get('/assets/collectivity:banner', status=302)['location'].endswith('test.svg')
951
    assert app.get('/assets/collectivity:banner?width=200', status=302)['location'].endswith('test.svg')
952
    assert app.get('/assets/collectivity:banner?height=200', status=302)['location'].endswith('test.svg')
953
    assert app.get('/assets/collectivity:banner?crop=center', status=302)['location'].endswith('test.svg')
954
    assert app.get('/assets/collectivity:banner?width=200&crop=center', status=302)['location'].endswith('test.svg')
955

  
956
    # unknown Asset key
957
    app.get('/assets/foo:bar', status=404)
958

  
959

  
907 960
def test_asset_export_import(app, admin_user):
908 961
    for path in ('uploads', 'assets', 'cache'):
909 962
        if os.path.exists(default_storage.path(path)):
910
-