assets: fix asset_url templatetag when file does not exist (#33959)
This commit is contained in:
parent
baf081c2c2
commit
08a739e0d8
|
@ -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
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue