Direkt zum Inhalt | Direkt zur Navigation

Benutzerspezifische Werkzeuge

Sie sind hier: Startseite / Tech-Blog / Plone: Multiple NameChooser for same Container

Plone: Multiple NameChooser for same Container

To have nice id's, one can register NameChooser adapter for a container in Plone. Here we show how one can have multiple NameChooser in the same container, like for different objekt types in a folder.

Here we register one NameChooser for a container named Library and use the chooserName method to dispatch to the right nameChooser for our current obj.

@implementer(INameChooser)
class LibraryNameChooser(object):
    """A name chooser for the Library"""

    def __init__(self, context):
        self.context = context

    def chooseName(self, name, obj):
        """ NameChooserFactory which dispatch to different nameChooser methods
        """
        if name:
            return name
        if IOutline.providedBy(obj):
            name = self.chooseOutlineName(name, obj)
        elif ISignature.providedBy(obj):
            name = self.chooseSignatureName(name, obj)
        else:
            name = self.chooseDefaultName(name, obj)
        return name

    def chooseOutlineName(self, name, obj):
        """ NameChooser based on sigel attribute
        """
        container = aq_inner(self.context)
        normalizer = queryUtility(IURLNormalizer)
        base_name = normalizer.normalize(obj.sigel)
        name = base_name
        i = 1
        while name in container.objectIds():
            name = base_name + '-' + str(i)
            i += 1
        return name

    def chooseSignatureName(self, name, obj):
        """ NameChooser based on signature attribute
        """
        container = aq_inner(self.context)
        normalizer = queryUtility(IURLNormalizer)
        base_name = normalizer.normalize(obj.signature)
        name = base_name
        i = 1
        while name in container.objectIds():
            name = base_name + '-' + str(i)
            i += 1
        return name

    def chooseDefaultName(self, name, obj):
        """ NameChooser based on title attribute
        """
        container = aq_inner(self.context)
        normalizer = queryUtility(IURLNormalizer)
        base_name = normalizer.normalize(obj.title)
        name = base_name
        i = 1
        while name in container.objectIds():
            name = base_name + '-' + str(i)
            i += 1
        return name

To activate this, we need to register our nameChooser:

<!-- Register a name chooser which handles objects inside library. -->
<adapter
    for=".library.ILibrary"
    factory=".library.LibraryNameChooser"
    provides="zope.container.interfaces.INameChooser"
    />
abgelegt unter: , ,