Skip to content

Commit

Permalink
Fix not allowed extension not showing in the error message
Browse files Browse the repository at this point in the history
  • Loading branch information
Th0masCat authored and RealOrangeOne committed Jul 19, 2024
1 parent f82f6fd commit 5cc28ac
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions docs/releases/6.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 5 additions & 1 deletion wagtail/documents/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
"""
Expand Down
6 changes: 5 additions & 1 deletion wagtail/documents/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down

0 comments on commit 5cc28ac

Please sign in to comment.