Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for: Not allowed extension not showing in the error message #11802 #11879

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 cm:
self.document_invalid.full_clean()
expected_message = (
"File extension “doc” is not allowed. Allowed extensions are: pdf."
)
self.assertEqual(cm.exception.message_dict["file"][0], expected_message)

def test_create_doc_valid_extension(self):
"""
Expand Down