[Zope] Howto use getattr() without acquisition?
Chris Withers
chrisw@nipltd.com
Tue, 31 Dec 2002 16:57:03 +0000
getattr(context.aq_explicit,'nickname',None)
cheers,
Chris
dave@loewen.com wrote:
>
> I'm building a python script to generate breadcrumb navigation links on
> each page of my website.
>
> It creates hyperlinks and uses the title or id for the link. I'd like to
> add a feature so that if the folder has a "nickname" property, it will
> use this for the breadcrumb hyperlink instead of the title.
>
> I tried using "hasattr()" to see if the current folder has a nickname
> property. It seems to work if the folder has a nickname property.
> However, if the folder doesn't have that properly, but the parent does,
> it still returns "true" and then getattr() acquires it from the parent
> instead of using the title. Is there a way to tell hasattr() to *not*
> use acquisition? Or perhaps another method which would do what I want?
> I'm sure there is a much more elegant way to do this...
>
> (These question feels like a FAQ, and I did do my best to search the
> group archives, but couldn't find anything appropriate-- most breadcrumb
> conversations seem to revolve around DTML and getProperty().)
>
> --dave
>
>
> """
> This script creates breadcrumb style navigation links.
>
> It walks the REQUEST.PARENTS list of parents and creates
> a hyperlink for each parent. It stops at the Examples folder.
> """
> links=[]
> for parent in context.REQUEST.PARENTS[1:]:
> id = parent.getId()
> if hasattr(parent, 'nickname'):
> label = getattr(parent, 'nickname')
> elif parent.title != "":
> label = parent.title
> else:
> label = parent.getId()
> links.insert(0, """<a href="%s" class="breadcrumb">%s</a>""" %
> (parent.absolute_url(), label))
>
> # Now get label for current container
> if context.hasProperty('nickname'):
> label = context.nickname
> elif context.title != "":
> label = context.title
> else:
> label = context.getId()
>
> return " : ".join(links) + " : " + label