Add file-extension field to ir.attachment


We can add files to anything in Tryton which is great. We have a machine to which we have added multiple files with different file extensions using the ir.attachment module.
Now we want to create a report and add some images to it. These images are already added to the machine with the ir.attachment module. So we want to make a selection out of the list of files attached to that machine. We made an many2many field for that which holds the images which should go on the report.
But, we cannot filter (add a domain ('extension', 'in', ['png', 'jpg', 'jpeg']) on file extension because there is no field on ir.attachement which holds the extension. Before I’m creating a new module for that, is this something which can be added to the core?

(albert) #2

I think adding the MIME Type would make sense. And there’s the core mimetypes [2] library for helping with that.

Even more, I think it would be interesting to have supertypes such as “Spreadsheet” or “Image” which includes all image mime types (PNG, SVG, JPG, GIF, etc).


(Cédric Krier) #3

If the filtering is only based on the file extension, I do not see the point to store any mimetype or extension.

This can be wrote: ['OR', ('name', 'ilike', '%.png'), ('name', 'ilike', '%.jpg'), ('name', 'ilike', '%.jpeg')]
Of course the list of extension could be deducted from guess_all_extensions and constructed by the searcher of a function field which compute the mimetype. I guess such function field could be in standard.


Ok, that’s a possibility. But now the user says " hey, that name is not readable". So the user changes the name to something readable, but did not add the extension or a complete wrong one.
You can try it yourself. Add an image (png, jpg whatever) to an item (sale, purchase, party whatever). And use the manage dialog to change the name to e.g. test.docx and click on “OK” to store the change and close the dialog. Now click on the paperclip and open your docx image … it opens LibreOffice to show the image.
So IMO we cannot rely on that field.

(Cédric Krier) #5

Neither on any field if you consider user breaking stuffs.
Most of users know (thanks to windows) what is an extension and know to not play with them.


You can have a database field which is controlled by Tryton and not displayed (or only readonly) to the user. So they cannot change it or they have to deliberately change is manually in the database. So when you upload the file to Tryton, it determines it’s file extension and stores that in the database field.

(Cédric Krier) #7

Please explain how you are doing that? If the information does not come from the user or file name.



I meant, that the user choose which file is uploaded. Just after uploading, Tryton takes that file extension and stores that in the database. When the user then changes the name field in the manage dialog, it doesn’t matter anymore because we know the file extension.

So users can upload an image (e.g. png) and after uploading change the name field in the manage dialog to for example test.docx. When the user then opens the file, it starts (in my case) LibreOffice instead of the image-viewer.


But if the user changes the file extension before uploading to Tryton, you cannot do anything about that.

(albert) #10

In fact, AFAIK, Windows hides extensions by default.

(albert) #11

There would probably be the possibility of using the “file” command line tool to guess the filetype / mimetype and try to guess an extension for it. Probably it would work for most common file types but not so well for others.


I think you can create lots of test to try to guess the filetype, but I think it makes things more complex and the benefit of having such is not that big. Some workflows I can imagine:

  • user creates a document, stores it to the disk with a filename with the name of the party in it and closes the document
  • user opens the document an other day and modifies it, stores it again
  • user is confident that the document is complete and add it to Tryton
  • user sees the name in the manage dialog and changes the name to remove the name of the party (not needed anymore because it’s in the list / form)

Another one:

  • user wants to create a new product
  • user goes to the website of the supplier and downloads the specification sheet in PDF
  • user creates the product in Tryton and adds the PDF
  • user sees the name and doesn’t care about is and leaves it ‘as is’

So I’m even start to think that the name field and the actual filename should be disconnected. With the upload of the file to Tryton, the name field is filled by Tryton with the filename and that’s it. When the user later wants to download that file, the original upload filename is used.