wagtail / wagtail

A Django content management system focused on flexibility and user experience

Home Page:https://wagtail.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Not allowed extension not showing in the error message.

Hlamallama opened this issue · comments

Issue Summary

Not allowed extension showing as blank when using the WAGTAILDOCS_EXTENSIONS setting.

Steps to Reproduce

  1. Start a new project with wagtail start myproject
  2. Add WAGTAILDOCS_EXTENSIONS = ["doc", "pdf"]
  3. Log onto Wagtail, navigate to Documents on the side menu
  4. Add a new document with txt extension.
  5. Error thrown File extension “” is not allowed. Allowed extensions are: doc, docx, pdf. Instead of File extension “txt” is not allowed. Allowed extensions are: doc, docx, pdf

image (1)

Technical details

  • Python version: 3.11.5
  • Django version: 5.0.3
  • Wagtail version: 6.0
  • Browser version: Chrome 122 and Firefox 123

Working on this

Anyone can contribute to this. View our contributing guidelines, add a comment to the issue once you’re ready to start.

It looks like there's a deeper issue here, the user shouldn't be presented with a successful upload message and then shown an error when they hit Update because the upload has actually failed already.

For example, with: WAGTAILDOCS_EXTENSIONS = ["doc", "pdf"]:
image

The .webp file hasn't actually successfully uploaded, if we navigate to the list of Documents, it's not there. Hitting Update then shows the validation error:
image

I think this is happening because the FileExtensionValidator adds the error to __all__ but when checking if the form is valid, we check the "file" key. If we add the error to the "file" key then the validation error is caught before the file is uploaded:
image

The below is a potential solution to get the above result but I'm not sure it's the best solution!
In the model clean method:

if allowed_extensions:
    validate = FileExtensionValidator(allowed_extensions)
    try:
        validate(self.file)
    except ValidationError as e:
        raise ValidationError(
            { "file": e.messages[0] }
        )

Fixed in 5cc28ac