diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 426db1cf0075..3fd146f452d4 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -46,6 +46,7 @@ Changelog * Fix: Handle StreamField migrations where the field value is null (Joshua Munn) * Fix: Prevent incorrect menu ordering when order value is 0 (Ben Dickinson) * Fix: Fix dynamic image serve view with certain backends (Sébastien Corbin) + * Fix: Show not allowed extension in error message (Sahil Jangra) * Docs: Remove duplicate section on frontend caching proxies from performance page (Jake Howard) * Docs: Document `restriction_type` field on PageViewRestriction (Shlomo Markowitz) * Docs: Document Wagtail's bug bounty policy (Jake Howard) diff --git a/docs/releases/6.2.md b/docs/releases/6.2.md index d38f4c89ffb0..bfeeaff6a99c 100644 --- a/docs/releases/6.2.md +++ b/docs/releases/6.2.md @@ -69,6 +69,7 @@ This feature was developed by Albina Starykova and sponsored by The Motley Fool. * Handle StreamField migrations where the field value is null (Joshua Munn) * Prevent incorrect menu ordering when order value is 0 (Ben Dickinson) * Fix dynamic image serve view with certain backends (Sébastien Corbin) + * Show not allowed extension in error message (Sahil Jangra) ### Documentation diff --git a/wagtail/documents/models.py b/wagtail/documents/models.py index 445eb6c848de..e84936f37163 100644 --- a/wagtail/documents/models.py +++ b/wagtail/documents/models.py @@ -4,6 +4,7 @@ from mimetypes import guess_type from django.conf import settings +from django.core.exceptions import ValidationError from django.core.validators import FileExtensionValidator from django.db import models from django.dispatch import Signal @@ -71,7 +72,10 @@ def clean(self): allowed_extensions = getattr(settings, "WAGTAILDOCS_EXTENSIONS", None) if allowed_extensions: validate = FileExtensionValidator(allowed_extensions) - validate(self.file) + try: + validate(self.file) + except ValidationError as e: + raise ValidationError({"file": e.messages[0]}) def is_stored_locally(self): """ diff --git a/wagtail/documents/tests/test_models.py b/wagtail/documents/tests/test_models.py index e7a88ab8d91a..1dcaecf8ace4 100644 --- a/wagtail/documents/tests/test_models.py +++ b/wagtail/documents/tests/test_models.py @@ -217,8 +217,12 @@ def test_create_doc_invalid_extension(self): creation when called full_clean. This specific testcase invalid file extension is passed """ - with self.assertRaises(ValidationError): + with self.assertRaises(ValidationError) as e: self.document_invalid.full_clean() + expected_message = ( + "File extension “doc” is not allowed. Allowed extensions are: pdf." + ) + self.assertEqual(e.exception.message_dict["file"][0], expected_message) def test_create_doc_valid_extension(self): """