[Zope] Why is it so hard to do simple things?

John Adams jadams@inktomi.com
Tue, 7 May 2002 17:12:00 -0700 (PDT)


I'm new to Zope, and new to Python, but certainly not new to software
engineering, with most of my background in C, Perl and Java.

I'm trying to do something quite simple. I'd like to see if a user has
checked off a radio box.  If it's their first time to the page, then the
attribute request.perm_or_temp hasn't been set, and one of the boxes
should be checked by default. If it's their 2nd time to the page (perhaps
an error, and they're POSTing again) then I'd like to retain the state of
the checkbox.

If I was doing this in perl it'd be quite easy; I could check the query
variable, and act on it -- but now I have to deal with things like not
being able to even examine the variable without throwing an exception, and
the fact that Python doesn't short-circuit blows.

i.e. You can't examine something like:

if (hasattr(request,'perm_or_temp') and request.perm_or_temp == 'P')
  ...


In Zope, I assume that I should be using DTML here, and perhaps dtml-in
over the items in the radio list.

What's the proper way to do this zope? My current version is quite poor,
takes up too many lines, and is disgustingly inefficient:

(this is a dtml-method)

# Default block -- this is a really shitty way to do this sort
# of thing. I hate python.

if not hasattr(request,'perm_or_temp'):
  print "<INPUT TYPE='RADIO' NAME=perm_or_temp VALUE='P' CHECKED>
Permanent"
  print "<INPUT TYPE='RADIO' NAME=perm_or_temp VALUE='T'> Temporary"
  return printed

# else we have it, process the request
print "<INPUT TYPE='RADIO' NAME=perm_or_temp VALUE='P'>"

if request.perm_or_temp == 'P'):
  print "CHECKED"

print "> Permanent"

print "<INPUT TYPE='RADIO' NAME=perm_or_temp VALUE='T'>"

if request.perm_or_temp == 'T':
  print "CHECKED"

print "> Temporary"

return printed



----

Thanks.

--john