def get_email2(self, principal):
        """Returns the second email address of a `principal`.
        """

        # inbox does not have a email
        if isinstance(principal, types.StringTypes) and \
                self.is_inbox(principal):
            return None

        # principal may be a contact brain
        elif ICatalogBrain.providedBy(principal) and \
                brain_is_contact(principal):
            return principal.email2

        # principal may be a user object
        elif IUser.providedBy(principal) or isinstance(principal, UserDict):
            return principal.email2

        # principal may ba a string contact principal
        elif self.is_contact(principal):
            return self.get_contact(principal).email2

        # principal may be a string user principal
        elif self.is_user(principal):
            return self.get_user(principal).email2

        else:
            raise ValueError('Unknown principal type: %s' %
                             str(principal))
    def describe(self, principal, with_email=False, with_email2=False,
                 with_principal=True):
        """Represent a user / contact / inbox / ... as string. This usually
        returns the fullname or another label / title.
        `principal` could also be a user object or a contact brain.
        """

        if not principal:
            return ''

        is_string = isinstance(principal, types.StringTypes)
        brain = None
        contact = None
        user = None

        # is principal a brain?
        if not is_string and ICatalogBrain.providedBy(principal):
            brain = principal

        # ok, lets check what we have...

        # string inbox
        if is_string and self.is_inbox(principal):
            # just do it
            client = self.get_client_of_inbox(principal)
            # we need to instantly translate, because otherwise
            # stuff like the autocomplete widget will not work
            # properly.
            label = _(u'inbox_label',
                      default=u'Inbox: ${client}',
                      mapping=dict(client=client.title))

            return translate(label, context=getRequest())

        # string contact
        elif is_string and self.is_contact(principal):
            contact = self.get_contact(principal)

        # string user
        elif is_string and self.is_user(principal):
            user = self.get_user(principal)

        # contact brain
        elif brain and brain_is_contact(brain):
            contact = brain
            principal = contact.contactid

        # user object
        elif IUser.providedBy(principal) or isinstance(principal, UserDict):
            user = principal
            principal = user.userid

        # ok, now lookup the stuff
        if contact:
            if not contact:
                return principal
            elif contact.lastname and contact.firstname:
                name = ' '.join((contact.lastname, contact.firstname))
            elif contact.lastname:
                name = contact.lastname
            elif contact.firstname:
                name = contact.firstname
            elif 'userid' in contact:
                name = contact.userid
            else:
                name = contact.id

            if with_email2 and contact.email2:
                return '%s (%s)' % (name, contact.email2)
            elif with_principal and contact.email:
                return '%s (%s)' % (name, contact.email)
            else:
                return name

        elif user:
            if user.lastname and user.firstname:
                name = ' '.join((user.lastname, user.firstname))
            elif user.lastname:
                name = user.lastname
            elif user.firstname:
                name = user.firstname
            else:
                name = user.userid

            infos = []
            if with_principal:
                infos.append(user.userid)

            if with_email and user.email:
                infos.append(user.email)

            elif with_email2 and user.email2:
                infos.append(user.email2)

            if infos:
                return '%s (%s)' % (name, ', '.join(infos))
            else:
                return name

        elif is_string:
            # fallback for acl_users
            portal = getSite()
            portal_membership = getToolByName(portal, 'portal_membership')
            member = portal_membership.getMemberById(principal)
            if not member:
                if isinstance(principal, str):
                    return principal.decode('utf-8')
                else:
                    return principal
            name = member.getProperty('fullname', principal)
            email = member.getProperty('email', None)

            infos = []
            if with_principal:
                infos.append(principal)

            if with_email and email:
                infos.append(email)

            if infos:
                return '%s (%s)' % (name, ', '.join(infos))
            else:
                return name

        else:
            raise ValueError('Unknown principal type: %s' % str(principal))
Beispiel #3
0
    def describe(self,
                 principal,
                 with_email=False,
                 with_email2=False,
                 with_principal=True):
        """Represent a user / contact / inbox / ... as string. This usually
        returns the fullname or another label / title.
        `principal` could also be a user object or a contact brain.
        """

        if not principal:
            return ''

        is_string = isinstance(principal, types.StringTypes)
        brain = None
        contact = None
        user = None

        # is principal a brain?
        if not is_string and ICatalogBrain.providedBy(principal):
            brain = principal

        # ok, lets check what we have...

        # string inbox
        if is_string and self.is_inbox(principal):
            # just do it
            client = self.get_client_of_inbox(principal)
            # we need to instantly translate, because otherwise
            # stuff like the autocomplete widget will not work
            # properly.
            label = _(u'inbox_label',
                      default=u'Inbox: ${client}',
                      mapping=dict(client=client.title))

            return translate(label, context=getRequest())

        # string contact
        elif is_string and self.is_contact(principal):
            contact = self.get_contact(principal)

        # string user
        elif is_string and self.is_user(principal):
            user = self.get_user(principal)

        # contact brain
        elif brain and brain_is_contact(brain):
            contact = brain
            principal = contact.contactid

        # user object
        elif IUser.providedBy(principal) or isinstance(principal, UserDict):
            user = principal
            principal = user.userid

        # ok, now lookup the stuff
        if contact:
            if not contact:
                return principal
            elif contact.lastname and contact.firstname:
                name = ' '.join((contact.lastname, contact.firstname))
            elif contact.lastname:
                name = contact.lastname
            elif contact.firstname:
                name = contact.firstname
            elif 'userid' in contact:
                name = contact.userid
            else:
                name = contact.id

            if with_email2 and contact.email2:
                return '%s (%s)' % (name, contact.email2)
            elif with_principal and contact.email:
                return '%s (%s)' % (name, contact.email)
            else:
                return name

        elif user:
            if user.lastname and user.firstname:
                name = ' '.join((user.lastname, user.firstname))
            elif user.lastname:
                name = user.lastname
            elif user.firstname:
                name = user.firstname
            else:
                name = user.userid

            infos = []
            if with_principal:
                infos.append(user.userid)

            if with_email and user.email:
                infos.append(user.email)

            elif with_email2 and user.email2:
                infos.append(user.email2)

            if infos:
                return '%s (%s)' % (name, ', '.join(infos))
            else:
                return name

        elif is_string:
            # fallback for acl_users
            portal = getSite()
            portal_membership = getToolByName(portal, 'portal_membership')
            member = portal_membership.getMemberById(principal)
            if not member:
                if isinstance(principal, str):
                    return principal.decode('utf-8')
                else:
                    return principal
            name = member.getProperty('fullname', principal)
            email = member.getProperty('email', None)

            infos = []
            if with_principal:
                infos.append(principal)

            if with_email and email:
                infos.append(email)

            if infos:
                return '%s (%s)' % (name, ', '.join(infos))
            else:
                return name

        else:
            raise ValueError('Unknown principal type: %s' % str(principal))