diff --git a/combo/data/models.py b/combo/data/models.py index 298dca46..968cd567 100644 --- a/combo/data/models.py +++ b/combo/data/models.py @@ -162,6 +162,11 @@ class Page(models.Model): def natural_key(self): return (self.get_online_url().strip('/'), ) + def picture_extension(self): + if not self.picture: + return None + return os.path.splitext(self.picture.name)[-1] + def save(self, *args, **kwargs): if not self.id: self.related_cells = {'cell_types': []} diff --git a/combo/manager/fields.py b/combo/manager/fields.py new file mode 100644 index 00000000..384bd721 --- /dev/null +++ b/combo/manager/fields.py @@ -0,0 +1,38 @@ +# -*- coding: utf-8 -*- +# +# combo - content management system +# Copyright (C) 2015-2019 Entr'ouvert +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + + +from django.core import validators +from django import forms + + +def get_available_image_extensions(): + return validators.get_available_image_extensions() + ['svg'] + +validate_image_file_extension = validators.FileExtensionValidator( + allowed_extensions=get_available_image_extensions(), +) + +class ImageIncludingSvgField(forms.ImageField): + default_validators = [validate_image_file_extension] + + def to_python(self, data): + if data.name and data.name.endswith('.svg'): + # bypass image field Pillow check + return super(forms.ImageField, self).to_python(data) + return super(ImageIncludingSvgField, self).to_python(data) diff --git a/combo/manager/forms.py b/combo/manager/forms.py index bbfd4820..1767e659 100644 --- a/combo/manager/forms.py +++ b/combo/manager/forms.py @@ -23,6 +23,8 @@ from django.utils.translation import ugettext_lazy as _ from combo.data.models import Page +from .fields import ImageIncludingSvgField + class PageEditTitleForm(forms.ModelForm): class Meta: @@ -53,6 +55,9 @@ class PageEditPictureForm(forms.ModelForm): class Meta: model = Page fields = ('picture',) + field_classes = { + 'picture': ImageIncludingSvgField + } class PageVisibilityForm(forms.ModelForm): diff --git a/combo/manager/static/css/combo.manager.css b/combo/manager/static/css/combo.manager.css index e54884a5..99d49e7d 100644 --- a/combo/manager/static/css/combo.manager.css +++ b/combo/manager/static/css/combo.manager.css @@ -374,6 +374,7 @@ span.extra-info { img.page-picture { max-width: 100%; + max-height: 250px; } span.error { diff --git a/combo/manager/templates/combo/page_view.html b/combo/manager/templates/combo/page_view.html index ec3585fe..83646bce 100644 --- a/combo/manager/templates/combo/page_view.html +++ b/combo/manager/templates/combo/page_view.html @@ -70,10 +70,14 @@

{% if object.picture %} -{% thumbnail object.picture "320x240" crop="50% 25%" as im %} - + {% if object.picture_extension != '.svg' %} + {% thumbnail object.picture "320x240" crop="50% 25%" as im %} + + {% endthumbnail %} + {% else %} + + {% endif %} ({% trans 'remove' %}) -{% endthumbnail %} {% else %}{% trans 'none' %}{% endif %} ({% trans 'change' %})

diff --git a/tests/test_manager.py b/tests/test_manager.py index 8aafd611..e9748e63 100644 --- a/tests/test_manager.py +++ b/tests/test_manager.py @@ -170,11 +170,22 @@ def test_page_edit_picture(app, admin_user): resp = resp.follow() assert '

Page - One

' in resp.text assert '', 'image/svg+xml') + resp = resp.form.submit() + assert resp.location.endswith('/manage/pages/%s/' % page.id) + resp = resp.follow() + assert '

Page - One

' in resp.text + assert '