[Twisted-web] nevow.wolf
Peter Hunt
floydophone at gmail.com
Thu Nov 18 07:50:43 MST 2004
Hi everyone,
I've been discussing this with a few people on #twisted.web, and after
reviewing the code, I've decided it's time to show it off to a larger
audience. I've committed a new package called wolf (flow backwards).
It's available at http://www.divmod.org/svn/Nevow/sandbox/phunt/wolf/.
Download that and drop it into your site-packages.
Essentially, it lets you write web applications in a linear style
instead of writing a bunch of ugly FSMs. For example, let's say you
wanted to write a user registration system...
from nevow import wolf
# assume RegisterPage, ProfilePage, and SuccessPage are nevow Pages
# RegisterPage asks for username and desired password
# ProfilePage asks for name and age
# SuccessPage is success
regpage = wolf.callable_page(RegisterPage) # simplifies usage within wolf
profpage = wolf.callable_page(ProfilePage)
successpage = wolf.callable_page(SuccessPage)
class Example(wolf.IndexedContainer):
def _validate_reg(self, req):
# TODO: make sure everything is ok with the form
return True
def _validate_prof(self, req):
# todo
return True
def _regform(self, req):
# underscore-prefixed methods cannot be called from outside
while True
yield regpage(req)
if self._validate_reg(req):
break
def _profform(self, req):
while True:
yield profpage(req)
if self._validate_prof(req):
break
def index(self, req):
# this is the whole register routine
yield self._regform(req)
yield self._profform(req)
yield successpage(req)
You can connect this to a Nevow site by using:
root.addChild("childname", wolf.WolfResource(Example))
Then, you can request it by going to:
http://host/childname/
or
http://host/childname/index
Download the code, try it out. If you run Stackless, you get to use
the back button, otherwise you're stuck with not being able to go
back. There is an example.py file which is a fully working example of
a guess-the-number game.
More information about the Twisted-web
mailing list