[albatross-users] Form state problem

Michael Neel neel at mediapulse.com
Wed Oct 30 13:51:31 EST 2002


I feel ashamed to not have thought of that.  It's been several years
since I've worked in an OO language, and the years of that "other p"
have taken their toll I see.  I'll have to do some playing around to get
the OrderInput class callable from the template, but that shouldn't be
too much work.  The reason I set it from the template is I use a custom
tag I wrote, alx-sql, to run SQL statements inside the template.  It
works something like such...

<alx-sql stmt="SELECT field1, field2 FROM table1 WHERE field3 = %s"
	   args="(1,)">
<!-- alx-sql sets the result in db_cursor and places it in ctx.locals
space -->
<al-for iter="row" expr="db_cursor.fetchall()">

Or also handy with <al-select name="choices"
optionexpr="db_cursor.fetchall()" />

Mike

-----Original Message-----
From: djc at ferret.object-craft.com.au
[mailto:djc at ferret.object-craft.com.au] On Behalf Of Dave Cole
Sent: Tuesday, October 29, 2002 7:13 PM
To: Michael C. Neel
Cc: albatross-users at object-craft.com.au
Subject: Re: [albatross-users] Form state problem


>>>>> "Michael" == Michael C Neel <neel at mediapulse.com> writes:

Michael> Ah, young grasshopper is schooled...  Yes, that's exactly what 
Michael> I did, even though Learning Python p.64 warns of this. 
Michael> Everything works fine now.

Have you considered using a small class to group values instead of using
parallel arrays?

  class OrderInput:
      def __init__(self, cfid, name):
          self.cfid = cfid
          self.name = name
          self.checked = 'on'
          self.order = ''

      def albatross_alias(self):
          return 'order%s' % self.cfid

  ctx.locals.orders = [OrderInput(1, 'One'),
                       OrderInput(2, 'Two')]
  ctx.add_session_vars('orders')


  <al-form method="POST">
  <al-input type="checkbox" name="custom_form" value="yes"> Enable
Custom Form
  <table>
   <tr>
    <td>Displayed</td>
    <td>Field Name</td>
    <td>Ordering</td>
   <tr>
   <al-for iter="r" expr="orders">
   <tr>
    <td><al-input type="checkbox" alias="r.value().checked"></td>
    <td><al-value expr="r.value().name"></td>
    <td><al-input type="text" size="2" alias="r.value().order"></td>
   </tr>
   </al-for>
   <tr>
    <td colspan="2"><al-input type="submit" name="submit" value="<->
Step 4"></td>
    <td align="right"><al-input type="submit" name="submit" value="Step
6 ->"></td>
   </tr>
  </table>
  </al-form>

Then your code which processes the input can simply do something like
this:

  for order in ctx.local.orders:
      if order.checked == 'on':
          # do something

For added security (since 1.01) you might even want to do this:

  ctx.locals._orders

This would prevent the browser from directly accessing any of the
internal values except via the alias.

- Dave

-- 
http://www.object-craft.com.au





More information about the Albatross-users mailing list