Browse Source

Remove WAGTAILIMAGES_ALLOW_SVG setting for WAGTAILIMAGES_EXTENSIONS

PR #9860 introduced the WAGTAILIMAGES_EXTENSIONS setting, which
conflicts with the use of
WAGTAILIMAGES_ALLOW_SVG. WAGTAILIMAGES_EXTENSIONS can be used to
opt-in for SVG use instead.
Joshua Munn 2 years ago
parent
commit
2424df3d68

+ 0 - 8
docs/reference/settings.md

@@ -367,14 +367,6 @@ If this isn't supplied, all of GIF, JPG, JPEG, PNG, WEBP are allowed.
 Warning: this doesn't always ensure that the uploaded file is valid as files can
 be renamed to have an extension no matter what data they contain.
 
-### `WAGTAILIMAGES_ALLOW_SVG`
-
-```python
-WAGTAILIMAGES_ALLOW_SVG = True
-```
-
-If `True`, this setting allows editors to upload and use SVG images alongside the default image formats (`False` by default).
-
 ## Documents
 
 ### `WAGTAILDOCS_DOCUMENT_MODEL`

+ 5 - 1
docs/topics/images.md

@@ -425,7 +425,11 @@ See [](image_renditions).
 
 ## SVG images
 
-Wagtail supports the use of Scalable Vector Graphics alongside raster images. To allow Wagtail users to upload and use SVG images, set `WAGTAILIMAGES_ALLOW_SVG` to `True`.
+Wagtail supports the use of Scalable Vector Graphics alongside raster images. To allow Wagtail users to upload and use SVG images, add "svg" to the list of allowed image extensions by configuring `WAGTAILIMAGES_EXTENSIONS`:
+
+```python
+WAGTAILIMAGES_EXTENSIONS = ["gif", "jpg", "jpeg", "png", "webp", "svg"]
+```
 
 SVG images can be included in templates via the `image` template tag, as with raster images. However, operations that require SVG images to be rasterised are not currently supported. This includes direct format conversion, e.g. `format-webp`, and `bgcolor` directives. Crop and resize operations do not require rasterisation, so may be used freely (see [](available_resizing_methods)).
 

+ 17 - 11
wagtail/images/fields.py

@@ -9,17 +9,25 @@ from django.forms.fields import FileField, ImageField
 from django.template.defaultfilters import filesizeformat
 from django.utils.translation import gettext_lazy as _
 
-ALLOW_SVG = getattr(settings, "WAGTAILIMAGES_ALLOW_SVG", False)
-if ALLOW_SVG:
-    ALLOWED_EXTENSIONS = ["gif", "jpg", "jpeg", "png", "webp", "svg"]
-    SUPPORTED_FORMATS_TEXT = _("GIF, JPEG, PNG, WEBP, SVG")
-else:
-    ALLOWED_EXTENSIONS = ["gif", "jpg", "jpeg", "png", "webp"]
-    SUPPORTED_FORMATS_TEXT = _("GIF, JPEG, PNG, WEBP")
+
+def get_allowed_image_extensions():
+    return getattr(
+        settings, "WAGTAILIMAGES_EXTENSIONS", ["gif", "jpg", "jpeg", "png", "webp"]
+    )
+
+
+def ImageFileExtensionValidator(value):
+    # This makes testing different values of WAGTAILIMAGES_EXTENSIONS easier:
+    # if WagtailImageField.default_validators
+    #      = FileExtensionValidator(get_allowed_image_extensions())
+    # then the formats that will pass validation are fixed at the time the class
+    # is created, so changes to WAGTAILIMAGES_EXTENSIONS via override_settings
+    # has no effect.
+    return FileExtensionValidator(get_allowed_image_extensions())(value)
 
 
 class WagtailImageField(ImageField):
-    default_validators = [FileExtensionValidator(ALLOWED_EXTENSIONS)]
+    default_validators = [ImageFileExtensionValidator]
 
     def __init__(self, *args, **kwargs):
         super().__init__(*args, **kwargs)
@@ -33,9 +41,7 @@ class WagtailImageField(ImageField):
         )
         self.max_upload_size_text = filesizeformat(self.max_upload_size)
 
-        self.allowed_image_extensions = getattr(
-            settings, "WAGTAILIMAGES_EXTENSIONS", ALLOWED_EXTENSIONS
-        )
+        self.allowed_image_extensions = get_allowed_image_extensions()
 
         self.supported_formats_text = ", ".join(self.allowed_image_extensions).upper()
 

+ 24 - 0
wagtail/images/tests/test_admin_views.py

@@ -524,6 +524,30 @@ class TestImageAddView(WagtailTestUtils, TestCase):
         root_collection = Collection.get_first_root_node()
         self.assertEqual(image.collection, root_collection)
 
+    def test_add_svg_denied(self):
+        """
+        SVGs should be disallowed by default
+        """
+        response = self.post(
+            {
+                "title": "Test image",
+                "file": SimpleUploadedFile(
+                    "test.svg",
+                    get_test_image_file_svg().file.getvalue(),
+                    content_type="text/html",
+                ),
+            }
+        )
+
+        self.assertEqual(response.status_code, 200)
+        self.assertFormError(
+            response,
+            "form",
+            "file",
+            "Not a supported image format. Supported formats: GIF, JPG, JPEG, PNG, WEBP.",
+        )
+
+    @override_settings(WAGTAILIMAGES_EXTENSIONS=["svg"])
     def test_add_svg(self):
         response = self.post(
             {

+ 2 - 2
wagtail/images/views/multiple.py

@@ -13,7 +13,7 @@ from wagtail.admin.views.generic.multiple_upload import (
 from wagtail.admin.views.generic.multiple_upload import DeleteView as BaseDeleteView
 from wagtail.admin.views.generic.multiple_upload import EditView as BaseEditView
 from wagtail.images import get_image_model
-from wagtail.images.fields import ALLOWED_EXTENSIONS
+from wagtail.images.fields import get_allowed_image_extensions
 from wagtail.images.forms import get_image_form, get_image_multi_form
 from wagtail.images.models import UploadedImage
 from wagtail.images.permissions import ImagesPermissionPolicyGetter, permission_policy
@@ -90,7 +90,7 @@ class AddView(BaseAddView):
             {
                 "max_filesize": self.form.fields["file"].max_upload_size,
                 "max_title_length": self.form.fields["title"].max_length,
-                "allowed_extensions": ALLOWED_EXTENSIONS,
+                "allowed_extensions": get_allowed_image_extensions(),
                 "error_max_file_size": self.form.fields["file"].error_messages[
                     "file_too_large_unknown_size"
                 ],

+ 0 - 2
wagtail/test/settings.py

@@ -257,5 +257,3 @@ MESSAGE_TAGS = {
     message_constants.WARNING: "my-custom-tag",
     message_constants.ERROR: "my-custom-tag",
 }
-
-WAGTAILIMAGES_ALLOW_SVG = True