[Zope] Newbe question concerning using python classes and external methods

Richard Taylor r.taylor@eris.dera.gov.uk
06 Jul 2000 18:23:51 +0100


"Gaspard, Bradley S" <BRADLEY.S.GASPARD@saic.com> writes:

> All,
> 
> I am a new Zope user and I guess I'm missing something fundemental that I
> haven't been able to glean from the documentation and various "How-to's".
> As a learning tool I am trying to implement one of Brian Lloyd's examples I
> found on the Web implementing a GuestBook.  
> 
> I suspect what I don't understand is how to write the external method that
> would make use of this class (The one I have been playing with follows the
> class definitions).  Anyhow, I've created an an external method and call
> (e.g. <dtml-var guest>) after which the guestBookForm is displayed.  After
> filling out the form and submitting it Zope does not seem able to find the
> signGuestBook method.  What am I doing wrong??
>

I not to sure why you have gone down the route of doing all of this as
an external method? I would have thought that the form should be
generated from an DTMLDocument and a signal function in the External
method could do the file-stuff and return the success page, no real
need for the class at all. But, I guess you are just using this as a
learning exercise, so what do I know.


Zope can't find you signGuestBook function because it knows nothing
about your  GuestBook class. The only thing you have told it about is
the ExternalMethod AddGuest. You could add another ExternalMethod
something like (code not tested):

 def SignGuestBook(name,email):
   myGuestBook=guestbook.GuestBook('My GuestBook', 'guestbookdata.txt')
   return myGuestBook.signGuestBook(name,email)

then create a DTMLmethod call signGuestBook:

	<dtml-var "SignGuestBook(name,email)">

you will need to add the SignGuestBook function as an external method
in addition to the one you already have.

You can see from this that the creation of an instance of the
GuestBook class is pretty pointless because it only exists within the
scope of the function in which it is created.

Another thing to bear in mind (it caught me out recently) is that
ExternalMethods are load once when Zope is started (or they are first
called, I am not sure which) not every time they are called. This
means that any global variables you create are common to every
call. As Zope is multithreaded this may catch you out if you do not
consider it.

Hope this helps.

Richard

> Other than mostly trivial examples, I haven't been able to find many
> complete (working) examples of using external methods.
> 
> Thanks in advance for any help.
> 
> """Module guestbook: a simple guestbook application"""
> 
> class GuestBook:
>   """A guestbook object that provides both the forms
>      and the handling of submitted form data."""
> 
>   def __init__(self, title, filename):
>     self.title=title
>     self.filename=filename
> 
>   def guestbookForm(self):
>     """Return the guestbook from to the user"""
>     return """<HTML>
> 	      <HEAD><TITLE>%s</TITLE></HEAD>
> 	      <BODY>
> 	      <H2>%s</H2>
> 	      Please sign our guestbook!
> 	      <P>
> 	      <FORM ACTION="signGuestBook" METHOD="POST">
> 	      Name: <INPUT TYPE="TEXT" NAME="name"><BR>
> 	      Email: <INPUT TYPE="TEXT" NAME="email"><BR>
> 	      <INPUT TYPE="SUBMIT" VALUE="Sign Guestbook">
> 	      </FORM>
> 	      </BODY>
> 	      </HTML>""" % (self.title, self.title)
> 
>   def successPage(self):
>     """Return a page to thank the user on success"""
>     return """<HTML>
> 	      <HEAD><TITLE>%s</TITLE></HEAD>
> 	      <BODY>
> 	      <H2>Thank You!</H2>
> 	      Thank you for signing %s!
> 	      </BODY>
> 	      </HTML>""" % (self.title, self.title)
> 
>   def signGuestBook(self, name, email='not specified'):
>     """Handle a submitted guestbook form"""
> 
>     # Open a file to save the guestbook entry
>     try: 
>       file=open(self.filename, 'a')
>     except IOError:
>       file=open(self.filename, 'w')
>     entry='Guestbook entry: %s %s\n' % (name, email)
>     file.write(entry)
>     file.close()
>     return self.successPage()
> 
> External method
> 
> import sys
> sys.path.append('/usr/local/src/Zope-2.1.6-linux2-x86/lib/python/Shard/jscap
> e/winsat')
> 
> import guestbook
> 
> def AddGuest(self):
>   myGuestBook=guestbook.GuestBook('My GuestBook', 'guestbookdata.txt')
>   return myGuestBook.guestbookForm()
> 
> Brad
> 
> _______________________________________________
> Zope maillist  -  Zope@zope.org
> http://lists.zope.org/mailman/listinfo/zope
> **   No cross posts or HTML encoding!  **
> (Related lists - 
>  http://lists.zope.org/mailman/listinfo/zope-announce
>  http://lists.zope.org/mailman/listinfo/zope-dev )