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

doc docx content types fail #18

Open
mooru opened this issue Oct 4, 2018 · 7 comments
Open

doc docx content types fail #18

mooru opened this issue Oct 4, 2018 · 7 comments

Comments

@mooru
Copy link

mooru commented Oct 4, 2018

I am trying to implement this plugin and have the following content types in my model

 file = ConstrainedFileField(upload_to='cvs/', blank=False, null=True, help_text='format: doc, docx',
                                verbose_name='cv', content_types=['application/msword', 'application/pdf',
                                'application/vnd.openxmlformats-officedocument.wordprocessingml.document'],
                                max_upload_size=1621440, js_checker=True)

but it only seems to save when i add a pdf to field. it fails when i add a doc or docx file. Any way to work aorund this

@mbourqui
Copy link
Owner

mbourqui commented Oct 7, 2018

Hi @mooru ,
Thanks for your interest in my fork.

Do you have any validation errors?
According to the MS docs, you have set the correct mime types. Have you tried running magic on your files, to check that they have the correct mime type? For example, .doc files created with OpenOffice can have a application/vnd.oasis.opendocument.text mime type.

@mbourqui
Copy link
Owner

mbourqui commented Oct 7, 2018

I just updated the tests for .odt and .doc files. Both seem to work fine. But I had an issue testing a .docx file saved from LibreOffice, as magic found a application/zip mime type instead of
application/vnd.openxmlformats-officedocument.wordprocessingml.document.

@mooru
Copy link
Author

mooru commented Oct 8, 2018

Thanks for your response. I changed the mimetype to application/zip but still not working. I will also try and run magic and see what happens

@mooru
Copy link
Author

mooru commented Oct 15, 2018

Hello @mbourqui
the plugin works as designed. I guess for some reason crispy forms was somehow not allowing me to see the error messages. How does one customize the error message? it currently says
unsupported format application/DCVPF or application/pdf to something like "file format can only be a .doc or .docx" without having to edit the source code

@arnav13081994
Copy link

arnav13081994 commented Sep 19, 2019

@mooru That's a problem with the crispy form library. It is not showing errors for django fields "ImageField" and "FileField". The easiest workaround for now is to add the following CSS code to the page you are showing these inline form errors.

.invalid-feedback { display: inherit; }

Another workaround is to override form_valid view and then use messages to send the custom error message in the context dictionary.

@poolpoolpool
Copy link

I found a different work around with CrispyForms. Since Crispy automatically forces users to fix errors in the form, if an error passes through, it must be because of a file type error. So you can simply add this to your view:

if form.is_valid():
    form.save()
else:
    messages.error(request, "Error, upload .doc file")
    form = form()

This has the benefit of not giving the MIME type error code for security reasons, and user experience.

@m-weintraub
Copy link

Hi. First, thanks for this package. I'm having a related issue. With some MSWord formats, MIMEMagic is identifying the MIME type incorrectly: see mimemagicrb/mimemagic#50. In my case, .doc files are being recognized as application/x-ole-storage instead of application/msword.

This app uses both the file type and file size constraints. My client wanted both messages presented if both constraints fail. So I did my own clean on the field.

Back to the issue... in my django form when I clean the constrainedfilefield, I can see the content-type is application-msword. But the application is still throwing an exception. So I'm guessing following the pattern for a model based form's method form_valid with a return super().form_value(form) is triggering the constrainedFileField check, which calls MIMEMagic?

Is there a recommended way to work around this issue? The obvious, but obviously wrong way is to add this wrong type to the list of ok file types. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants