Still Chipping Away At Formulator/ZPT Interface
Hi; Okay, I *am* going to write a How-To on this when I get it all figured out with your help. This needs to be done. In DTML, we do this to render the fields: <dtml-in "form.get_fields()"> <dtml-let field=sequence-item> <tr> <td><dtml-var "field.get_value('title')"></td> <td><dtml-var "field.render()"></td> etc. How do I do this same thing in Python? Here's the start of the code: mailhost=getattr(context, context.superValues('Mail Host')[0].id) This much works. Now, I need to be able to call the values of the Formulator form (named *email_us_formulator*). An example of a value is *YourEmail*. How would I access this value with Python? TIA, beno
Hi beno,
Hi; Okay, I *am* going to write a How-To on this when I get it all figured out with your help. This needs to be done. In DTML, we do this to render the fields:
<dtml-in "form.get_fields()"> <dtml-let field=sequence-item> <tr> <td><dtml-var "field.get_value('title')"></td> <td><dtml-var "field.render()"></td>
OT: I am not very familiar with DTML, but I remember vaguely it is recommended to use the more explicit syntax: <dtml-var expr="field.render()"> (Well, its only a recommendation.)
etc. How do I do this same thing in Python? Here's the start of the code:
mailhost=getattr(context, context.superValues('Mail Host')[0].id)
This much works. Now, I need to be able to call the values of the Formulator form (named *email_us_formulator*). An example of a value is *YourEmail*. How would I access this value with Python?
Maybe read the fine python tutorial ;-) e.g. the equivalent to "<dtml-in> are "for" loops: form = context.email_us_formulator for fiels in form.fields(): print '<td>' + field.get_value('title') + '</td>' Scott Burton has written an HowTo which may be helpful either: http://www.zopelabs.com/cookbook/1032909599 It is also linked via the Formulator page on zope.org ... looking up the resources in the web may even be faster than asking the list ;)
At 09:43 PM 11/4/2002 +0100, you wrote:
Hi beno,
Hi; Okay, I *am* going to write a How-To on this when I get it all figured out with your help. This needs to be done. In DTML, we do this to render the fields:
<dtml-in "form.get_fields()"> <dtml-let field=sequence-item> <tr> <td><dtml-var "field.get_value('title')"></td> <td><dtml-var "field.render()"></td>
etc. How do I do this same thing in Python? Here's the start of the code:
mailhost=getattr(context, context.superValues('Mail Host')[0].id)
This much works. Now, I need to be able to call the values of the Formulator form (named *email_us_formulator*). An example of a value is *YourEmail*. How would I access this value with Python?
Maybe read the fine python tutorial ;-)
How many times? I imagine it will eventually start to sink in...
e.g. the equivalent to "<dtml-in> are "for" loops:
form = context.email_us_formulator for fiels in form.fields(): print '<td>' + field.get_value('title') + '</td>'
Hmmm. This didn't work. It threw an AttributeError on *fields*. Just to be sure, I changed your line to for x in form.fields(): and it was the form.fields() that was erroring. Why would it work in DTML and not in Python?
Scott Burton has written an HowTo which may be helpful either:
Yep. Read it many times. Before I ever wrote the list. Some of it's pretty obtuse. And it's written specifically for password authentication: not my application.
It is also linked via the Formulator page on zope.org ... looking up the resources in the web may even be faster than asking the list ;)
I try. Really, I do :)) Thanks, beno
beno writes:
Okay, I *am* going to write a How-To on this when I get it all figured out with your help. This needs to be done. In DTML, we do this to render the fields:
<dtml-in "form.get_fields()"> <dtml-let field=sequence-item> <tr> <td><dtml-var "field.get_value('title')"></td> <td><dtml-var "field.render()"></td>
etc. How do I do this same thing in Python? Python, usually, is not there to render a form.
In ZPT, it would be <tr tal:repeat="field here/form/get_fields"> <td tal:content="python: field.get_value('title')" /> <td tal:content="structure field/render" /> </tr> As you see, it's easier than in DTML. Dieter
At 08:33 PM 11/5/2002 +0100, you wrote:
beno writes:
Okay, I *am* going to write a How-To on this when I get it all figured out with your help. This needs to be done. In DTML, we do this to render the fields:
<dtml-in "form.get_fields()"> <dtml-let field=sequence-item> <tr> <td><dtml-var "field.get_value('title')"></td> <td><dtml-var "field.render()"></td>
etc. How do I do this same thing in Python? Python, usually, is not there to render a form.
In ZPT, it would be
<tr tal:repeat="field here/form/get_fields"> <td tal:content="python: field.get_value('title')" /> <td tal:content="structure field/render" /> </tr>
As you see, it's easier than in DTML.
Actually, I need to do this in Python, since I'm calling the MailHost. I figured out how to do that, but discovered that's not what I wanted <:-) What I *really* need is to be able to call the values the visitor enters into the fields in the form. For example, on the page to which POST sends I would write something like this in DTML: <dtml-var the_variable_the_visitor_filled_in_goes_here> and it would render the information accordingly. I need to do that in *Python*, not ZPT. How? TIA, beno
Hi beno, [snip dtml code]
In ZPT, it would be
<tr tal:repeat="field here/form/get_fields"> <td tal:content="python: field.get_value('title')" /> <td tal:content="structure field/render" /> </tr>
As you see, it's easier than in DTML.
Actually, I need to do this in Python, since I'm calling the MailHost. I figured out how to do that, but discovered that's not what I wanted <:-) What I *really* need is to be able to call the values the visitor enters into the fields in the form. For example, on the page to which POST sends I would write something like this in DTML: <dtml-var the_variable_the_visitor_filled_in_goes_here> and it would render the information accordingly. I need to do that in *Python*, not ZPT. How?
Actually You want to verify the submitted form data first, then read out the data to do something, I guess. This looks like: from Products.Formulator.Errors import ValidationError, FormValidationError try: result = context.email_us_formulator.validate_all(context.REQUEST) except FormValidationError, e: # render error message; e.g. see mailing thread at: # http://sourceforge.net/mailarchive/forum.php?thread_id=1272274&forum_id=1702 # if verification succeeded, You now have all filled in values # in the dictionary "result" try: mailhost=getattr(context, context.superValues('Mail Host')[0].id) except: raise AttributeError, "cant find a Mail Host object" mTo = 'beno@thewebsons.com' mFrom = result['YourEmail'] # assuming 'YourEmail' is the id of the corresponding Formulator Field mSubj = 'subject' # assume Field ids beeing 'YourName', 'YourPhone', etc ... message = "Hi, Mike! You've just been sent an email from your Web site!\n\n" message += "Sender's name: " + result['YourName'] + "\n" message += "Sender's phone: " + result['YourPhone'] + "\n" message += "Sender's email: " + result['YourEmail'] + "\n\n" message += "Sender's comments: " + result['YourMessage'] + "\n" mMsg = message mailhost.send(mMsg, mto=mTo, mfrom=mFrom, subject=mSubj) # feedback message: needs a ZPT with id "mail_send" # read name of submitter from "options/name" in this ZPT return context.mail_send(name=result['YourName']) Note 1: I did not rewrite the String concatenation as proposed in a previous posting in this thread. This does not mean I have any objections to it; I have just been to lazy to retype the example. Note 2: I admit I did not test the code this time, again. Sorry for any typos ... Cheers, Clemens
At 10:59 PM 11/5/2002 +0100, you wrote:
Hi beno,
[snip dtml code]
In ZPT, it would be
<tr tal:repeat="field here/form/get_fields"> <td tal:content="python: field.get_value('title')" /> <td tal:content="structure field/render" /> </tr>
As you see, it's easier than in DTML.
Actually, I need to do this in Python, since I'm calling the MailHost. I figured out how to do that, but discovered that's not what I wanted <:-) What I *really* need is to be able to call the values the visitor enters into the fields in the form. For example, on the page to which POST sends I would write something like this in DTML: <dtml-var the_variable_the_visitor_filled_in_goes_here> and it would render the information accordingly. I need to do that in *Python*, not ZPT. How?
Actually You want to verify the submitted form data first, then read out the data to do something, I guess.
This looks like:
from Products.Formulator.Errors import ValidationError, FormValidationError try: result = context.email_us_formulator.validate_all(context.REQUEST) except FormValidationError, e: # render error message; e.g. see mailing thread at: # http://sourceforge.net/mailarchive/forum.php?thread_id=1272274&forum_id=1702
Hmmm. Went there. Was referred to a file called *Errors.py* in Formulator. File doesn't exist. Noticed the thread was recent and the author was the author of the program. What did I miss?!
# if verification succeeded, You now have all filled in values # in the dictionary "result"
try: mailhost=getattr(context, context.superValues('Mail Host')[0].id) except: raise AttributeError, "cant find a Mail Host object"
mTo = 'beno@thewebsons.com' mFrom = result['YourEmail'] # assuming 'YourEmail' is the id of the corresponding Formulator Field mSubj = 'subject'
# assume Field ids beeing 'YourName', 'YourPhone', etc ... message = "Hi, Mike! You've just been sent an email from your Web site!\n\n" message += "Sender's name: " + result['YourName'] + "\n" message += "Sender's phone: " + result['YourPhone'] + "\n" message += "Sender's email: " + result['YourEmail'] + "\n\n" message += "Sender's comments: " + result['YourMessage'] + "\n" mMsg = message
mailhost.send(mMsg, mto=mTo, mfrom=mFrom, subject=mSubj)
Thank you! Yes, finally!
# feedback message: needs a ZPT with id "mail_send" # read name of submitter from "options/name" in this ZPT return context.mail_send(name=result['YourName'])
Okay, now I'm confused. What is this for?
Note 1: I did not rewrite the String concatenation as proposed in a previous posting in this thread. This does not mean I have any objections to it; I have just been to lazy to retype the example.
I did rewrite it. Also, any idea on how to send *two* emails with slightly different content, all generated via Formulator, through the one connection to MailHost? Thanks so much, beno P.S. Perhaps I should have called this thread "Still *Hacking* Away..."
beno writes: [...]
Actually You want to verify the submitted form data first, then read out the data to do something, I guess.
This looks like:
from Products.Formulator.Errors import ValidationError, FormValidationError try: result = context.email_us_formulator.validate_all(context.REQUEST) except FormValidationError, e: # render error message; e.g. see mailing thread at: # http://sourceforge.net/mailarchive/forum.php?thread_id=1272274&forum_id=1702
Hmmm. Went there. Was referred to a file called *Errors.py* in Formulator. File doesn't exist. Noticed the thread was recent and the author was the author of the program. What did I miss?!
guess You missed to look in the "Products/Formulator" directory on the file system level, where You have installed the product. If "Errors.py" would not be there, the script would not be able to do things like "from Products.Formulator.Errors import ..." [snip sending mail part]
# feedback message: needs a ZPT with id "mail_send" # read name of submitter from "options/name" in this ZPT return context.mail_send(name=result['YourName'])
Okay, now I'm confused. What is this for?
Render a page template which displays a message to the submitter that the email has been send. Though it may sound strange, some people like to get feedback if they press a submit button on the web ;-)
>> from Products.Formulator.Errors import ... beno> Hmmm. Went there. Was referred to a file called *Errors.py* in beno> Formulator. File doesn't exist. Noticed the thread was recent and beno> the author was the author of the program. What did I miss?! Depending how your directory tree is set up, you may have to look in more than one place. I have a binary Plone installed (MacOSX). It has the following relevant directories: /Applications/Plone/PloneInstance/Products /Applications/Plone/Zope/lib/python/Products It would appear products can be stored in either directory, probably because when starting Plone twiddles some path variable used by Zope (sys.path comes to mind, but there is probably something Zope-specific as well). In my case Formulator product in is in the first Products directory. In there I find an Errors.py. Perhaps you are also dealing with a multi-headed beast. Assuming you are on a Unix-ish system you should be able to execute something like: cd /Applications/Plone find . -name Formulator -- Skip Montanaro - skip@pobox.com http://www.mojam.com/ http://www.musi-cal.com/
At 06:53 PM 11/5/2002 -0600, you wrote:
>> from Products.Formulator.Errors import ...
beno> Hmmm. Went there. Was referred to a file called *Errors.py* in beno> Formulator. File doesn't exist. Noticed the thread was recent and beno> the author was the author of the program. What did I miss?!
Depending how your directory tree is set up, you may have to look in more than one place. I have a binary Plone installed (MacOSX). It has the following relevant directories:
/Applications/Plone/PloneInstance/Products /Applications/Plone/Zope/lib/python/Products
I ran this command at my Zope root: find . -name "Errors.py" and nothing came back!
Perhaps you are also dealing with a multi-headed beast. Assuming you are on a Unix-ish system you should be able to execute something like:
cd /Applications/Plone find . -name Formulator
Oh, *Formulator* I find with no problem at all! It's in the Zope directory... Thanks anyway, beno
participants (4)
-
beno -
Clemens Robbenhaar -
Dieter Maurer -
Skip Montanaro