SVN: r25346 - trunk/quixote/demo
Neil Schemenauer <nascheme-fVcApmY9cLvQ3/1i3zOLAti2O/[email protected]> Thu, 14 Oct 2004 13:34:21 -0400
| Newsgroups | gmane.comp.web.quixote.cvs |
|---|---|
| Message-ID | <[email protected]> |
Author: nascheme
Date: 2004-10-14 13:30:50 -0400 (Thu, 14 Oct 2004)
New Revision: 25346
Removed:
trunk/quixote/demo/widgets.ptl
Modified:
trunk/quixote/demo/__init__.py
trunk/quixote/demo/forms.ptl
trunk/quixote/demo/pages.ptl
Log:
Use form2 library for demo.
Modified: trunk/quixote/demo/__init__.py
===================================================================
--- trunk/quixote/demo/__init__.py 2004-10-14 17:29:05 UTC (rev 25345)
+++ trunk/quixote/demo/__init__.py 2004-10-14 17:30:50 UTC (rev 25346)
@@ -1,11 +1,10 @@
-_q_exports = ["simple", "error", "publish_error", "widgets",
+_q_exports = ["simple", "error", "publish_error",
"form_demo", "dumpreq", "srcdir",
("favicon.ico", "q_ico")]
import sys
from quixote.demo.pages import _q_index, _q_exception_handler, dumpreq
-from quixote.demo.widgets import widgets
from quixote.demo.integer_ui import IntegerUI
from quixote.errors import PublishError
from quixote.util import StaticDirectory, StaticFile
Modified: trunk/quixote/demo/forms.ptl
===================================================================
--- trunk/quixote/demo/forms.ptl 2004-10-14 17:29:05 UTC (rev 25345)
+++ trunk/quixote/demo/forms.ptl 2004-10-14 17:30:50 UTC (rev 25346)
@@ -6,7 +6,10 @@
import time
-from quixote.form import Form
+from quixote.form2 import Form, StringWidget, PasswordWidget, \
+ RadiobuttonsWidget, SingleSelectWidget, MultipleSelectWidget, \
+ CheckboxWidget
+from quixote.form2.css import BASIC_FORM_CSS
class Topping:
def __init__(self, name, cost):
@@ -29,61 +32,57 @@
Topping('anchovies', 30),
Topping('onions', 25)]
-class FormDemo(Form):
- def __init__(self):
- # build form
- Form.__init__(self)
- self.add_widget("string", "name", title="Your Name",
- size=20, required=True)
- self.add_widget("password", "password", title="Password",
- size=20, maxlength=20, required=True)
- self.add_widget("checkbox", "confirm",
- title="Are you sure?")
- self.add_widget("radiobuttons", "color", title="Eye color",
- allowed_values=['green', 'blue', 'brown', 'other'])
- self.add_widget("single_select", "size", title="Size of pizza",
- value='medium',
- allowed_values=['tiny', 'small', 'medium', 'large',
- 'enormous'],
- descriptions=['Tiny (4")', 'Small (6")', 'Medium (10")',
- 'Large (14")', 'Enormous (18")'],
- size=1)
- # select widgets can use any type of object, no just strings
- self.add_widget("multiple_select", "toppings", title="Pizza Toppings",
- value=TOPPINGS[0],
- allowed_values=TOPPINGS,
- size=5)
- self.add_widget('hidden', 'time', value=time.time())
- self.add_submit_button("go", "Go!")
+def form_demo(request):
+ # build form
+ form = Form()
+ form.add(StringWidget, "name", title="Your Name",
+ size=20, required=True)
+ form.add(PasswordWidget, "password", title="Password",
+ size=20, maxlength=20, required=True)
+ form.add(CheckboxWidget, "confirm",
+ title="Are you sure?")
+ form.add(CheckboxWidget, "color", title="Eye color",
+ options=['green', 'blue', 'brown', 'other'])
+ form.add(SingleSelectWidget, "size", title="Size of pizza",
+ value='medium',
+ options=[('tiny', 'Tiny (4")'),
+ ('small', 'Small (6")'),
+ ('medium', 'Medium (10")'),
+ ('large', 'Large (14")'),
+ ('enormous', 'Enormous (18")')],
+ size=1)
+ # select widgets can use any type of object, no just strings
+ form.add(MultipleSelectWidget, "toppings", title="Pizza Toppings",
+ value=[TOPPINGS[0]],
+ options=TOPPINGS,
+ size=5)
+ form.add_hidden('time', value=time.time())
+ form.add_submit("go", "Go!")
-
- def render [html] (self, request, action_url):
+ def render [html] ():
"""
<html>
<head><title>Quixote Form Demo</title></head>
+ <style type="text/css">
+ %s
+ </style>
<body>
<h1>Quixote Form Demo</h1>
+ """ % BASIC_FORM_CSS
+ form.render()
"""
- Form.render(self, request, action_url)
- """
</body>
</html>
"""
+ if not form.is_submitted() or form.has_errors():
+ return render()
- def process(self, request):
- # check data
- form_data = Form.process(self, request)
- if not form_data["name"]:
- self.error["name"] = "You must provide your name."
- if not form_data["password"]:
- self.error["password"] = "You must provide a password."
- return form_data
+ # Could to more error checking, set errors and return render().
-
- def action [html] (self, request, submit_button, form_data):
- # The data has been submitted and verified. Do something interesting
- # with it (save it in DB, send email, etc.). We'll just display it.
+ # The data has been submitted and verified. Do something interesting
+ # with it (save it in DB, send email, etc.). We'll just display it.
+ def success [html] ():
"""
<html>
<head><title>Quixote Form Demo</title></head>
@@ -96,12 +95,13 @@
<th align=left>Value</th>
</tr>
"""
- for name, value in form_data.items():
+ for widget in form.get_all_widgets():
+ value = widget.parse()
'<tr>'
- ' <td>%s</td>' % name
+ ' <td>%s</td>' % widget.get_name()
' <td>%s</td>' % type(value).__name__
if value is None:
- value = "<i>no value</i>"
+ value = "<i>None</i>"
' <td>%s</td>' % value
'</tr>'
"""
@@ -110,5 +110,4 @@
</html>
"""
-def form_demo(request):
- return FormDemo().handle(request)
+ return success()
Modified: trunk/quixote/demo/pages.ptl
===================================================================
--- trunk/quixote/demo/pages.ptl 2004-10-14 17:29:05 UTC (rev 25345)
+++ trunk/quixote/demo/pages.ptl 2004-10-14 17:30:50 UTC (rev 25346)
@@ -46,8 +46,6 @@
A method on a published Python object.
<li><a href="dumpreq">dumpreq</a>:
Print out the contents of the HTTPRequest object.
- <li><a href="widgets">widgets</a>:
- Try out the Quixote widget classes.
<li><a href="form_demo">form demo</a>:
A Quixote form in action.
<li><a href="srcdir/">srcdir</a>:
Deleted: trunk/quixote/demo/widgets.ptl
===================================================================
--- trunk/quixote/demo/widgets.ptl 2004-10-14 17:29:05 UTC (rev 25345)
+++ trunk/quixote/demo/widgets.ptl 2004-10-14 17:30:50 UTC (rev 25346)
@@ -1,129 +0,0 @@
-# quixote.demo.widgets
-#
-# Demonstrate the Quixote widget classes.
-
-__revision__ = "$Id$"
-
-
-import time
-from quixote.form import widget
-
-
-def widgets(request):
-
- # Whether we are generating or processing the form with these
- # widgets, we need all the widget objects -- so create them now.
- widgets = {}
- widgets['name'] = widget.StringWidget('name', size=20)
- widgets['password'] = widget.PasswordWidget(
- 'password', size=20, maxlength=20)
- widgets['confirm'] = widget.CheckboxWidget('confirm')
- widgets['colour'] = widget.RadiobuttonsWidget(
- 'colour', allowed_values=['green', 'blue', 'brown', 'other'])
- widgets['size'] = widget.SingleSelectWidget(
- 'size', value='medium',
- allowed_values=['tiny', 'small', 'medium', 'large', 'enormous'],
- descriptions=['Tiny (4")', 'Small (6")', 'Medium (10")',
- 'Large (14")', 'Enormous (18")'])
- widgets['toppings'] = widget.MultipleSelectWidget(
- 'toppings', value=['cheese'],
- allowed_values=['cheese', 'pepperoni', 'green peppers', 'mushrooms',
- 'sausage', 'anchovies', 'onions'],
- size=5)
- widgets['time'] = widget.HiddenWidget('time', value=time.time())
-
- if request.form:
- # If we have some form data, then we're being invoked to process
- # the form; call process_widgets() to do the real work. We only
- # handle it in this page to conserve urls: the "widget" url both
- # generates the form and processes it, and behaves very
- # differently depending on whether there are form variables
- # present when it is invoked.
- return process_widgets(request, widgets)
- else:
- # No form data, so generate the form from scratch. When the
- # user submits it, we'll come back to this page, but
- # request.form won't be empty that time -- so we'll call
- # process_widgets() instead.
- return render_widgets(request, widgets)
-
-
-def render_widgets [html] (request, widgets):
- """\
-<html>
-<head><title>Quixote Widget Demo</title></head>
-<body>
-<h1>Quixote Widget Demo</h1>
-"""
-
- """\
-<form method="POST" action="widgets">
-<table>
-"""
- row_fmt = '''\
- <tr>
- <th align="left">%s</th>
- <td colspan=2>%s</td>
- </tr>
-'''
- row_fmt % ("Your name", widgets['name'].render(request))
- row_fmt % ("Password", widgets['password'].render(request))
- row_fmt % ("Are you sure?", widgets['confirm'].render(request))
- row_fmt % ("Eye colour", widgets['colour'].render(request))
-
- '''\
- <tr>
- <th align="left" valign="top">Select a<br>size of pizza</th>
- <td valign="top">%s</td>
- <th align="left" valign="top">And some<br>pizza toppings</th>
- <td valign="top">%s</td>
- </tr>
-''' % (widgets['size'].render(request),
- widgets['toppings'].render(request))
-
- widgets['time'].render(request)
-
- '</table>\n'
- widget.SubmitButtonWidget(value="Submit").render(request)
- '''\
-</form>
-</body>
-</html>
-'''
-
-def process_widgets [html] (request, widgets):
- """\
-<html>
-<head><title>Quixote Widget Demo</title></head>
-<body>
-<h2>You entered the following values:</h2>
-<table>
-"""
-
- row_fmt = ' <tr><th align="left">%s</th><td>%s</td></tr>\n'
- fallback = '<i>nothing</i>'
- row_fmt % ("name",
- widgets['name'].parse(request) or fallback)
- row_fmt % ("password",
- widgets['password'].parse(request) or fallback)
- row_fmt % ("confirmation",
- widgets['confirm'].parse(request))
- row_fmt % ("eye colour",
- widgets['colour'].parse(request) or fallback)
- row_fmt % ("pizza size",
- widgets['size'].parse(request) or fallback)
- toppings = widgets['toppings'].parse(request)
- row_fmt % ("pizza toppings",
- toppings and (", ".join(toppings)) or fallback)
-
- '</table>\n'
-
- form_time = float(widgets['time'].parse(request))
- now = time.time()
- ("<p>It took you %.1f sec to fill out and submit the form</p>\n"
- % (now - form_time))
-
- """\
-</body>
-</html>
-"""