Custom Id, from specific field (person name)

Hi,
I have an issue that I cant fix. I want an custom Id, generated from

'{0}-{1}'.format(first_name, last_name)

But it doesn't work. I tried it with an behavior, here is my code so far

Behavior

<plone:behavior
    title="Name from Patient"
    name="ps.vita.namefrompatientname"
  description="Automatically generate short URL name for content based on patient name"
    provides=".id.INameFromPatientName"
    factory=".id.NameFromPatientName"
    for="plone.dexterity.interfaces.IDexterityContent"
    />
<adapter factory=".id.NameFromPatientName" />



class INameFromPatientName(Interface):
    """Marker interface to enable name from filename behavior"""
    print 'INameFromPatientName ...'


@implementer(INameFromTitle)
@adapter(INameFromPatientName)
class NameFromPatientName(object):

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

    def __new__(cls, context):
        instance = super(NameFromPatientName, cls).__new__(cls)
        print '..........'
        try:
            title = context.first_name + ' ' + context.last_name
        except AttributeError:
            return u'new person'

        if context.prefix:
            title = '%s %s' % (context.prefix, title)
        if context.suffix:
            title = '%s, %s' % (title, context.suffix)

        instance.title = '%s-%s' % (context.first_name, context.last_name)
        context.setTitle(title)

        return instance

I already copied and paste the code from https://github.com/plone/plone.app.dexterity/blob/master/plone/app/dexterity/behaviors/filename.py
but it does nothing...

I hope someone could give me a hint

I'm sorry, but it works now - I have no idea why, but ... :slight_smile:

As Behavior
configure.zcml

<plone:behavior
    title="Short Name for Person"
    description="Use prefix, title and suffix (if availiable) for setting the name (id) of an object1"
    provides=".id.INameFromPerson"
    />
<adapter factory=".id.NameFromPerson" />

id.py

# -*- coding: utf-8 -*-
from plone.app.content.interfaces import INameFromTitle
from zope.interface import Interface
from zope.component import adapter
from zope.interface import implementer

import logging

logger = logging.getLogger(__name__)


class INameFromPerson(Interface):
    """ Interface to adapt to INameFromTitle """
    pass


@implementer(INameFromTitle)
@adapter(INameFromPerson)
class NameFromPerson(object):
    """ Adapter to INameFromTitle """

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

    def __new__(cls, context):
        instance = super(NameFromPerson, cls).__new__(cls)
        try:
            title = '{} {}'.format(context.first_name, context.last_name)
        except AttributeError:
            return u'Sorry, could not create person'

        instance.title = title
        context.setTitle(title)

        return instance
1 Like