[Zope] Howto use getattr() without acquisition?

dave@loewen.com dave@loewen.com
Tue, 31 Dec 2002 10:08:01 -0600


This is a multipart message in MIME format.
--=_alternative 00589A2386256CA0_=
Content-Type: text/plain; charset="us-ascii"

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

--=_alternative 00589A2386256CA0_=
Content-Type: text/html; charset="us-ascii"


<br><font size=2 face="sans-serif">I'm building a python script to generate breadcrumb navigation links on each page of my website.</font>
<br>
<br><font size=2 face="sans-serif">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 &quot;nickname&quot; property, it will use this for the breadcrumb hyperlink instead of the title.</font>
<br>
<br><font size=2 face="sans-serif">I tried using &quot;hasattr()&quot; 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 &quot;true&quot; 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...</font>
<br>
<br><font size=2 face="sans-serif">(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().)</font>
<br>
<br><font size=2 face="sans-serif">--dave</font>
<br>
<br>
<br><font size=2 face="sans-serif">&quot;&quot;&quot;</font>
<br><font size=2 face="sans-serif">This script creates breadcrumb style navigation links.</font>
<br>
<br><font size=2 face="sans-serif">It walks the REQUEST.PARENTS list of parents and creates</font>
<br><font size=2 face="sans-serif">a hyperlink for each parent. It stops at the Examples folder.</font>
<br><font size=2 face="sans-serif">&quot;&quot;&quot;</font>
<br><font size=2 face="sans-serif">links=[]</font>
<br><font size=2 face="sans-serif">for parent in context.REQUEST.PARENTS[1:]:</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; id = parent.getId()</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; if hasattr(parent, 'nickname'):</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; label = getattr(parent, 'nickname')</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; elif parent.title != &quot;&quot;:</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; label = parent.title</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; else:</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; label = parent.getId()</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; links.insert(0, &quot;&quot;&quot;&lt;a href=&quot;%s&quot; class=&quot;breadcrumb&quot;&gt;%s&lt;/a&gt;&quot;&quot;&quot; % (parent.absolute_url(), label))</font>
<br>
<br><font size=2 face="sans-serif"># Now get label for current container</font>
<br><font size=2 face="sans-serif">if context.hasProperty('nickname'):</font>
<br><font size=2 face="sans-serif">&nbsp; label = context.nickname</font>
<br><font size=2 face="sans-serif">elif context.title != &quot;&quot;:</font>
<br><font size=2 face="sans-serif">&nbsp; label = context.title</font>
<br><font size=2 face="sans-serif">else:</font>
<br><font size=2 face="sans-serif">&nbsp; label = context.getId()</font>
<br>
<br><font size=2 face="sans-serif">return &quot; : &quot;.join(links) + &quot; : &quot; + label</font>
<br>
--=_alternative 00589A2386256CA0_=--