How to set validation for document upload only to .pdf in openERP
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
- CRM
- e-Commerce
- Contabilità
- Magazzino
- PoS
- Project
- MRP
La domanda è stata contrassegnata
Hello @rumaan Try This:
from odoo.exceptions import UserError
import mimetypes
from odoo.tools.mimetypes import guess_mimetype
file_name = fields.Char('File Name')
mimetype = None
if mimetype is None and self.file_name:
mimetype = mimetypes.guess_type(res.file_name)[0]
if not mimetype == 'pdf':
raise UserError('Allowed Format Pdf')
I can't answer directly to Jaiswal Shivam, but their answer is dangerous from a security standpoint. The attacker controls the file name and thus the validation can be bypassed by setting the expected extension (malicious_content.xlsx) but the content can be anything (e.g. php script).
The accepted answer using mimetype guessing is much better from a security standpoint.
Or you can also try this code::
# create a char field
file_data = fields.Binary('File')
file_name = fields.Char('File Name')
# Go to xml and
def check_file_type(self):
if not self.file_name.endswith('.xlsx'): //Here you can define your file format like .pdf, .word etc.//
raise UserError('Please insert a valid file')
Thanks!!
Were you able to solve it?
Yes..
Ti stai godendo la conversazione? Non leggere soltanto, partecipa anche tu!
Crea un account oggi per scoprire funzionalità esclusive ed entrare a far parte della nostra fantastica community!
Registrati