[Zope-DB] Hello!

Jim Penny jpenny@universal-fasteners.com
Fri, 25 Jul 2003 12:57:50 -0400


On Fri, 25 Jul 2003 17:30:20 +0100
Philip Kilner <phil@xfr.co.uk> wrote:

> Hi Cahrlie (& List)
> 
> Charlie Clark wrote:
> > You call an ZSQL-method within Zope like this:
> > r = context.selectBook()
> > 
> 
> My next query would be what I make the "action" of my form? We started
> 
> off at "insertBook", which was my ZSQL method and which I now know is 
> the wrong thing to point it at. Would I be right in thinking that the 
> "action" should be a ZPT which calls the script and then the ZSQL
> method?

Well, pretty much.  It is probably NOT a ZPT.  Again, ZPT's are for
presentation of HTML or XML data to a browser which will deliver it to a
user.  Since an insert does not really have any meaningful result to the
user, use the "silence is golden" rule.  In the scheme I have outlined,
index_html will handle the details of what to call when a submit button
that triggers an insert is pushed, and what to return to the browser
after the insert is done.  I don't have the code in front of me, but it
typically looks something like:

if ns=='Insert':
  err=container.check_insert_data_ps()
  if err:
     return container.insert_form_pt(context, request, error=err)
  else:
    # insert the data
    container.insert_isbn_sql(arguments)
    return container.whatever_you_want_the_user_to_see_next_pt()

Some comments:

  check_insert_data_ps typically has two functions.  Generating an error
message if one is needed, and making all the variable canonical -
certainly stripping, maybe taking to upper or lower case, etc.  It might
look like:

check_insert_data_ps()
request=context.REQUEST
err=''
# the ISBN NUMBER must be two sequences of numeric separated by a dash
isbn_number=request['isbn_number'].strip()
request.set('isbn_number', isbn_number)
if isbn_number.find('-')<0:
  err+='<p>There must be a dash in the ISBN number.</p>'
else:
  try:
    (n1, n2)=isbn_number.split('-')
    try:
      i=int(n1)
    except:
      err+="""<p>The part of the ISBN number in front of the dash must
consist only of digits</p>"""
    try:
      i=int(n2)
    except:
      err+="""<p>The part of the ISBN number after the dash must
consist only of digits</p>"""
  except:
    err+='<p>There can be only one dash in an ISBN number</p>'
# process the other arguments
...
return err

Second comment.  A ZSQL method can accept either a dictionary or a list
of named parameters, or both as its arguments.  That means that you can
call it as container.insertBook(book_dict), as below, 
container.insertBook(request), 
container.insertBook(isbnNumber=isbn_number, ...) or even
container.insertBook(request, isbnNumber=isbn_number, ...)

Jim

> 
> > I generally loop over the REQUEST.form assigning values to a
> > dictionary which gets passed to the ZSQL-Method after things have
> > been checked.
> > 
> > book_dict = {}
> > for item, value in request.form.items():
> > 	book_dict[item] = value
> > 
> > context.insertBook(book_dict)
> > return context.thank_you(context, request)
> > 
> 
> This looks like script which would be called by a PT and which in turn
> 
> calls the ZSQL. Do I have that right?
> 
> > Hope that helps.
> > 
> 
> Well, I feel as though I am getting there in microsocopic increments -
> I appreciate your help here, because I'm feeling deeply stupid.
> There's something about the "big picture" I'm just not "getting"!
> 
> -- 
> 
> Regards,
> 
> PhilK
> 
> Email: phil@xfr.co.uk / Voicemail & Facsimile: 07092 070518
> 
> "the symbols of the divine show up in our world initially at the trash
> 
> stratum." Philip K Dick
> 
>