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

Richard Jones rjones@ekit-inc.com
Wed, 8 May 2002 10:30:12 +1000


On Wed, 8 May 2002 10:12, John Adams wrote:
> 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.

That's nice.


> 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.

Number one suggestion: have the page call a python script method that checks 
the environment for a form submission as the first thing it does. If it 
doesn't find a form submission, have it set the environment to your sensible 
form defaults. Your presentation/view code (DTML or ZPT) now doesn't have to 
have lots of yucky code in it to handle the various with/without cases.


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

Number two suggestion: take your attitude and shove it up your arse.


> 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

If you don't want to split the model and view code as I suggested above, try 
this python script code for generating radio boxes.

perm_or_temp = getattr(request, 'perm_or_temp', 'P')
for value, label in (('P', 'Permanent'), ('T', 'Temporary')):
  if perm_or_temp == value: checked = 'CHECKED'
  else: checked = ''
  print '<INPUT TYPE="RADIO" NAME="perm_or_temp" VALUE="%s" %s>%s'%(value,
    checked, label)


    Richard