plone / plone.api

The Plone API
https://6.docs.plone.org/plone.api
Other
85 stars 53 forks source link

Cannot create an object with a custom naming behavior #362

Open ale-rt opened 7 years ago

ale-rt commented 7 years ago

Because of these lines:

        derived_id = id or title
        new_id = chooser.chooseName(derived_id, content)

in https://github.com/plone/plone.api/blob/d65c81e6eeb69f0a581fa50e9f0500372efe45b5/src/plone/api/content.py#L115

I will never reach line 34 (nameFromTitle = INameFromTitle(obj, None)) here https://github.com/plone/plone.app.content/blob/9c220b2ceace9c74a464529d89e4a66c16c0fc17/plone/app/content/namechooser.py#L34

For this reason it is not possible to create with plone.api an object that authomatically gets the id correctly from a custom INameFromTitle behavior that is not based on the title.

I noticed this with a custom INameFromTitle adapter that gives an id based on the date, but I think this is an issue also for the File content type (which uses this behavior: https://github.com/plone/plone.app.dexterity/blob/184184150ab6b7743769fe92bf8ec78e5fb52873/plone/app/dexterity/behaviors/filename.py#L16)

rafaelbco commented 5 years ago

I can confirm that the issue occurs with any content type that implements a custom adapter to INameFromTitle.

This adapter provides a title property, which the name chooser utility then uses as an input to generate the actual ID. This way one can customize the generated IDs for the content items.

I think plone.api.content.create() should be modified in the following way (before the line mentioned in the preceding comment):

chooser = INameChooser(container)
name_from_title = INameFromTitle(content, None)
title = name_from_title.title if name_from_title else title
derived_id = id or title
new_id = chooser.chooseName(derived_id, content)

If the content type does not implement INameFromTitle or has the default implementation then nothing will change.