ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> zope-List
zope-List
[Zope] Howto use getattr() without acquisition?
by other posts by this author
Dec 31 2002 4:08PM messages near this date
Re: [Zope] Python scripts & DA objects | Re: [Zope] Howto use getattr() without acquisition?
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
Attachments:
unknown1

Thread:

Gilles Lenfant
Dieter Maurer
Chris Withers

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved