assets: fix asset_url templatetag when file does not exist (#33959)

This commit is contained in:
Lauréline Guérin 2019-10-14 11:54:33 +02:00
parent baf081c2c2
commit 08a739e0d8
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
2 changed files with 11 additions and 0 deletions

View File

@ -14,6 +14,8 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
from django import template
from django.db.models.fields.files import ImageFieldFile
from django.utils import six
@ -51,6 +53,9 @@ def asset_url(*args, **kwargs):
if not asset:
return ''
if not os.path.exists(asset.path):
return asset.url
geometry_string = kwargs.pop('size', None)
if not geometry_string or asset.file.name.endswith('svg'):
return asset.url

View File

@ -181,6 +181,12 @@ def test_asset_template_tags():
t = Template('''{% load assets %}{% asset_url page.picture "collectivity:banner" size="200x200" %}''')
assert t.render(Context()).startswith('/media/cache/')
# unless file is missing
os.remove(page.picture.path)
del page.picture.file
t = Template('''{% load assets %}{% asset_url page.picture "collectivity:banner" size="200x200" %}''')
assert t.render(Context({'page': page})) == '/media/page-pictures/test2.png'
# unless it's in SVG
page.picture = File(StringIO('test'), 'test2.svg')
page.save()