[Product-Developers] Remove dashes from shortname - override choosename in namechooser.py?

Laurence Rowe l at lrowe.co.uk
Thu Jul 28 14:37:49 UTC 2011


Giacomo Spettoli wrote:
> 
> Il 27/07/2011 20:34, imran.azad ha scritto:
>> Hi,
>>
>> I want to remove the dashes from the shortname of the object. In other
>> words
>> I want to remove all dashes in the id of an object. I have noticed there
>> is
>> a function in namechooser.py called chooseName which does the following:
>>
>> name = getUtility(IURLNormalizer).normalize(name)
>>
>> if I set the variable to:
>>
>> name = name
>>
>> It works! And the dashes aren't added. However this approach is modifying
>> the Plone Core :-(
>> I want to avoid doing this, I would rather like to override the
>> chooseName
>> function.
>>
>> How can I do this from my class(custom content type using Dexterity)
>>
>> Thank You
>>
>> --
>> View this message in context:
>> http://plone.293351.n2.nabble.com/Remove-dashes-from-shortname-override-choosename-in-namechooser-py-tp6627262p6627262.html
>> Sent from the Product Developers mailing list archive at Nabble.com.
>> _______________________________________________
>> Product-Developers mailing list
>> Product-Developers at lists.plone.org
>> https://lists.plone.org/mailman/listinfo/plone-product-developers
> 
> 
> Hi,
>     you can customize the behaviour of name normalization by registering 
> a local utility that implements IURLNormalizer interface.
> 
> Read more here:
> http://plone.org/documentation/manual/developer-manual/generic-setup/reference/component-registry
> 

The IURLNormalizer utility is designed for normalizing non-ascii and unsafe
characters into something sensible (which is dependent on your language,
e.g. in German ü normalized to ue.) It is looked up on the request rather
than the context, so you probably want a custom INameFromTitle adapter
instead. See
http://dev.plone.org/plone/browser/plone.app.dexterity/trunk/plone/app/dexterity/behaviors/filename.py
for an example dexterity behavior.

That example looks slightly complex because it uses __new__ to ensure that
INameFromTitle(object, None) returns None in the case that it can't
determine the primary field for an object, yours should probably just look
something like:

class CustomTitleAdapter(object):
    implements(INameFromTitle)
    adapts(ICustomTitleBehavior)
    def __init__(self, context):
        self.title = context.title.replace(' ', '')

Laurence

--
View this message in context: http://plone.293351.n2.nabble.com/Remove-dashes-from-shortname-override-choosename-in-namechooser-py-tp6627262p6630087.html
Sent from the Product Developers mailing list archive at Nabble.com.


More information about the Product-Developers mailing list