[Twisted-Python] twisted web, documentation
Hello, Being new to twisted, I didn't find in the documentation an example (in DOM Template, widgets, woven or resource templates ), for implementing a form. Someone has a link? Maarten This message and any attachments (the "message") is intended solely for the addressees and is confidential. If you receive this message in error, please delete it and immediately notify the sender. Any use not in accord with its purpose, any dissemination or disclosure, either whole or partial, is prohibited except formal approval. Neither FININFO (nor any of its subsidiaries or affiliates) shall be liable for the message if modified, altered, falsified, edited or diffused without authorization.
On Fri, Apr 25, 2003 at 08:54:03AM +0200, VAN DER HEIJDEN Maarten wrote:
Hello,
Being new to twisted, I didn't find in the documentation an example (in DOM Template, widgets, woven or resource templates ), for implementing a form.
Someone has a link?
There will be a whole bunch of new, up-to-date docs on Woven in CVS within a week. I believe this includes a general tutorial and stuff about using forms. If you can, wait until we've got those docs ready. Otherwise, you could try asking for advice on #twisted on irc.freenode.net, which is currently the best place for Woven docs (until the new docs get checked in...) -Andrew.
Hi, Andrew Bennetts schrub am Fri, 25 Apr 2003 07:16:25 +0000:
There will be a whole bunch of new, up-to-date docs on Woven in CVS within a week. I believe this includes a general tutorial and stuff about using forms.
Excellent!
If you can, wait until we've got those docs ready. Otherwise, you could try asking for advice on #twisted on irc.freenode.net, which is currently the best place for Woven docs (until the new docs get checked in...)
How about a link to the not-quite-ready docs? -- Matthias
On Sat, 10 May 2003, "Matthias Urlichs" <smurf@noris.de> wrote:
Andrew Bennetts schrub am Fri, 25 Apr 2003 07:16:25 +0000:
Note the 15 days of lag. Also, this is an English-language mailing list. Kindly configure your software to produce English verbiage, so that people are not expected to understand random language.
There will be a whole bunch of new, up-to-date docs on Woven in CVS within a week.
So, in April 25th, Andrew claimed there will be docs in CVS within a week. Had you bothered checking, you would have seen that not only have the docs made it to CVS, but that they are actually in the 1.0.5 book, in a chapter named, in a surprise move, "Woven". See <URL:http://www.twistedmatrix.com/documents/howto/> Thanks a lot to Donovan and Allen for the hard work in writing those documents, and thanks to Andrew for converting them into Lore and integrating them into the book! -- Moshe Zadka -- http://moshez.org/ Buffy: I don't like you hanging out with someone that... short. Riley: Yeah, a lot of young people nowadays are experimenting with shortness. Agile Programming Language -- http://www.python.org/
Hi, Moshe Zadka:
Note the 15 days of lag. Also, this is an English-language mailing list.
Sorry about that.
So, in April 25th, Andrew claimed there will be docs in CVS within a week. Had you bothered checking, you would have seen that not only have the docs made it to CVS, but that they are actually in the 1.0.5 book, in a chapter named, in a surprise move, "Woven".
I saw the missing "forms/guard in depth" link and seem to have concluded erroneously that this has in fact not happened. -- Matthias
On Sat, 10 May 2003, "Matthias Urlichs" <smurf@noris.de> wrote:
I saw the missing "forms/guard in depth" link and seem to have concluded erroneously that this has in fact not happened.
Documentation for form and guard have not, to the best of my knowledge, been written yet. -- Moshe Zadka -- http://moshez.org/ Buffy: I don't like you hanging out with someone that... short. Riley: Yeah, a lot of young people nowadays are experimenting with shortness. Agile Programming Language -- http://www.python.org/
VAN DER HEIJDEN Maarten <mvanderheijden@fininfo.fr> writes:
Hello,
Being new to twisted, I didn't find in the documentation an example (in DOM Template, widgets, woven or resource templates ), for implementing a form.
Someone has a link?
this is an extended ReportRequest with form : import twisted.web.resource import cgi from twisted.protocols import http from twisted.web import resource,error class ReportRequest(twisted.web.resource.Resource): """ ReportRequest with forms and cookies """ def isLeaf(self): return true def render(self, request): user = request.getUser() password = request.getPassword() if not user: request.setResponseCode(401, 'Authentication needed') request.setHeader('WWW-authenticate', 'Basic realm="enter new user and password"') errpage = error.ErrorPage(http.UNAUTHORIZED,"Unauthorized","401 Authentication required") return errpage.render(request) session = request.getSession() try: if request.args["text"]!="": session.text = request.args["text"] session.touch() except: pass # no text arg session_text = getattr(session,"text","init") # create and increment cookie counter, try: counter = int(request.getCookie("counter")) counter += 1 except: counter = 0 request.addCookie("counter",counter) cookies = request.received_cookies args = request.args path = request.path _, host, port = request.getHost() url = request.prePathURL() uri = request.uri secure = (request.isSecure() and "securely") or "insecurely" return ("""\ <HTML> <HEAD><TITLE>Welcome To Twisted Python Reporting</title></head> <BODY><H1>Welcome To Twisted Python Reporting</H1> <UL> <LI>User : %(user)s <LI>Password : %(password)s <LI>The path to me is %(path)s <LI>The host I'm on is %(host)s <LI>The port I'm on is %(port)s <LI>I was accessed %(secure)s <LI>A URL to me is %(url)s <LI>My URI to me is %(uri)s <LI>Session text : %(session_text)s <LI>Cookies (incremented in each reload): <pre>%(cookies)s</pre> <LI>Form args : <pre>%(args)s</pre> </UL> <hr> <h2>Testing args</h2> Test args : <a href='?a=1&a=2&a=3&b=4'>?a=1&a=3&a=3&b=4</a><br> Test forms with method=post enctype='multipart/form-data' <form method=post enctype='multipart/form-data'> <table> <tr><td> text : </td><td> <input name=text type=text> </td></tr> <tr><td> file : </td><td> <input name=file type=file> </td></tr> <tr><td> select (multiple) : </td><td> <select name=select multiple><option>one</option><option>two</option><option>three</option></select> </td></tr></table> <input type=submit></form> </body> </html>""" % vars()) -- William Dodé - http://flibuste.net
William Dode wrote:
class ReportRequest(twisted.web.resource.Resource): """ ReportRequest with forms and cookies """ def render(self, request): session = request.getSession()
try: if request.args["text"]!="": session.text = request.args["text"]
Please correct me if I'm wrong, or out of date, but doesn't request.args contain *lists* of things, always, and not the strings directly? That was what I observed on 1.0.3 anyway, I thought. If that's so, then I think the above test will always pass, as request.args['text'] would contain [''] or [] or something, but never just "". -Peter
On Friday, April 25, 2003, at 12:16 PM, Peter Hansen wrote:
William Dode wrote:
class ReportRequest(twisted.web.resource.Resource): """ ReportRequest with forms and cookies """ def render(self, request): session = request.getSession()
try: if request.args["text"]!="": session.text = request.args["text"]
Please correct me if I'm wrong, or out of date, but doesn't request.args contain *lists* of things, always, and not the strings directly? That was what I observed on 1.0.3 anyway, I thought.
If that's so, then I think the above test will always pass, as request.args['text'] would contain [''] or [] or something, but never just "".
That is correct. request.args always contains lists. Donovan
[William Dode]
[VAN DER HEIJDEN Maarten]
Being new to twisted, I didn't find in the documentation an example (in DOM Template, widgets, woven or resource templates ), for implementing a form. Someone has a link?
this is an extended ReportRequest with form :
Thanks a lot, William. Your example (quoted in full below, for reference) has been both useful and instructive. I have a question, though. What is the purpose of `isLeaf'? It does not seem to be called ever, when I run your example. ----------------------------------------------------------------------> import twisted.web.resource import cgi from twisted.protocols import http from twisted.web import resource,error class ReportRequest(twisted.web.resource.Resource): """ ReportRequest with forms and cookies """ def isLeaf(self): return true def render(self, request): user = request.getUser() password = request.getPassword() if not user: request.setResponseCode(401, 'Authentication needed') request.setHeader('WWW-authenticate', 'Basic realm="enter new user and password"') errpage = error.ErrorPage(http.UNAUTHORIZED,"Unauthorized","401 Authentication required") return errpage.render(request) session = request.getSession() try: if request.args["text"]!="": session.text = request.args["text"] session.touch() except: pass # no text arg session_text = getattr(session,"text","init") # create and increment cookie counter, try: counter = int(request.getCookie("counter")) counter += 1 except: counter = 0 request.addCookie("counter",counter) cookies = request.received_cookies args = request.args path = request.path _, host, port = request.getHost() url = request.prePathURL() uri = request.uri secure = (request.isSecure() and "securely") or "insecurely" return ("""\ <HTML> <HEAD><TITLE>Welcome To Twisted Python Reporting</title></head> <BODY><H1>Welcome To Twisted Python Reporting</H1> <UL> <LI>User : %(user)s <LI>Password : %(password)s <LI>The path to me is %(path)s <LI>The host I'm on is %(host)s <LI>The port I'm on is %(port)s <LI>I was accessed %(secure)s <LI>A URL to me is %(url)s <LI>My URI to me is %(uri)s <LI>Session text : %(session_text)s <LI>Cookies (incremented in each reload): <pre>%(cookies)s</pre> <LI>Form args : <pre>%(args)s</pre> </UL> <hr> <h2>Testing args</h2> Test args : <a href='?a=1&a=2&a=3&b=4'>?a=1&a=3&a=3&b=4</a><br> Test forms with method=post enctype='multipart/form-data' <form method=post enctype='multipart/form-data'> <table> <tr><td> text : </td><td> <input name=text type=text> </td></tr> <tr><td> file : </td><td> <input name=file type=file> </td></tr> <tr><td> select (multiple) : </td><td> <select name=select multiple><option>one</option><option>two</option><option>three</option></select> </td></tr></table> <input type=submit></form> </body> </html>""" % vars()) ----------------------------------------------------------------------< -- François Pinard http://www.iro.umontreal.ca/~pinard
On 2003.04.29 14:51, François Pinard wrote:
Your example (quoted in full below, for reference) has been both useful and instructive. I have a question, though. What is the purpose of `isLeaf'? It does not seem to be called ever, when I run your example.
class ReportRequest(twisted.web.resource.Resource): """ ReportRequest with forms and cookies """ def isLeaf(self): return true
It looks like he meant to just say "isLeaf = True". It's expected to be an attribute, not a method. It worked accidentally because all function objects are True. What isLeaf *does* is to say "Stop looking for more children beyond me". i.e., this resource will not have `getChild' called on it; it will be immediately render()ed. You can handle the rest of the path segments by accessing them through the request object passed to the render method. -- Twisted | Christopher Armstrong: International Man of Twistery Radix | Release Manager, Twisted Project ---------+ http://twistedmatrix.com/users/radix.twistd/
pinard@iro.umontreal.ca (François Pinard) writes:
Your example (quoted in full below, for reference) has been both useful and instructive. I have a question, though. What is the purpose of `isLeaf'? It does not seem to be called ever, when I run your example.
...
class ReportRequest(twisted.web.resource.Resource): def isLeaf(self): return true
Oops. 'isLeaf' is a boolean attribute, not a function. It is usually set at the class level, and overrides t.w.r.Resource's default of '0'. So this class should really be written as follows: class ReportRequest(twisted.web.resource.Resource): isLeaf = 1 (the ReportRequest example worked because a bound method happens to be "true" when evaluated in a conditional. However, it would never have been able to return "false"). 'isLeaf' is a flag tells the server to stop walking through the URL path. Each slash-delimited component of the URL is put into a list, and the resource tree is walked (the Site is asked for a child indexed by the first component, that child is asked for one indexed by the second, etc). If a child is returned with the .isLeaf flag set, the walk is terminated, and that child is responsible for dealing with the rest of the path on its own. The request.postpath array contains the remaining components of the URL. This is useful when you want to change the resource lookup behavior below some sub-node of the URL tree. Above the child that sets .isLeaf=1, the usual Resource.getChild() scheme is used to find a Resource. Below that, the child can do whatever it wants. For example, let's pretend that we have a web server which serves users' ~/public_html/ directories, but each user is in a department, and the URL is always supposed to look like http://foo.com/Department/User/Dir/Subdir/File . The top two levels of the tree (department and user) must do some kind of lookup to find out who is in which department and where their home directory is. The child that is returned by the User lookup would set .isLeaf=1, and then get the actual file based upon the directory path contained in the remaining part of the URL. We're basically breaking the URL into two pieces: [http://foo.com/Department/User], and [Dir/Subdir/File]. The resource tree is walked with the first part, but the second part is given all at once (as a list) to the results of the lookup on the first part. class MySite(Resource): def getChild(self, path, request): dept = self.findDepartment(path) # returns a DepartmentResource if dept == None: return error.NoResource("No such department '%s'" % path) return dept class DepartmentResource(Resource): def getChild(self, path, request): user = self.findUser(path) # returns a UserResource if user == None: return error.NoResource("No such user '%s'" % path) return user class UserResource(Resource): isLeaf = 1 # don't call our .getChild(), just do render() def render(self, request): # rest of URL is in request.postpath for p in request.postpath: if p == "..": return error.NoResource("naughty naughty") filename = os.path.join(self.homedir(), "public_html", request.postpath) file = File(filename) return file.render(request) I'm sure there are better examples of isLeaf out there.. this is what comes to my mind when I see that flag. cheers, -Brian
[Brian Warner]
[Fran�ois Pinard]
[...] What is the purpose of `isLeaf'? It does not seem to be called ever, when I run your example. [...]
class ReportRequest(twisted.web.resource.Resource): def isLeaf(self): return true
Oops. 'isLeaf' is a boolean attribute, not a function. [...]
Brian and Christopher, let me thank you both for your replies. I did not find a presentation in the manual (but I'm far from having read it all yet) about a tree organisation of resources. Brian's explanation is quite comprehensive, and in my opinion, should happily find its way into the current Twisted book. Is there someone listening, who is taking care of such things? We surely have good documentation material in Brian's reply! -------------------- I also found, by re-reading various saved messages from the list, that three of them were especially instructive about the nature and justification of TAPs, which are: 2003-02-14 07:42 -0500 Glyph Lefkowitz <glyph@twistedmatrix.com> 2003-02-11 15:17 -0500 Glyph Lefkowitz <glyph@twistedmatrix.com> 2003-02-11 01:35 -0500 Moshe Zadka <twisted@moshez.org> all having "In Defense of Taps (was Re: [Twisted-Python] where to begin)" for a Subject. Also likely interesting for users is: 2003-02-12 08:51 -0500 Glyph Lefkowitz <glyph@twistedmatrix.com> In all these messages, and surely others, there is documentation material that could be retrofitted in the Twisted manual (editing out some unwanted parts and maybe rephrasing others). Is there someone, taking care of the documentation, to whom I should direct a copy of the above (if needed)? -- François Pinard http://www.iro.umontreal.ca/~pinard
On Tue, Apr 29, 2003 at 04:42:55PM -0400, Fran?ois Pinard wrote: [...]
In all these messages, and surely others, there is documentation material that could be retrofitted in the Twisted manual (editing out some unwanted parts and maybe rephrasing others). Is there someone, taking care of the documentation, to whom I should direct a copy of the above (if needed)?
Send it to the list, or if you think it's too large, post it on a website somewhere and send the URL to the list. You are welcome to send it directly to me, but then you'll miss out on feedback from everyone else... -Andrew.
Andrew Bennetts <andrew-twisted@puzzling.org> writes:
On Tue, Apr 29, 2003 at 04:42:55PM -0400, Fran?ois Pinard wrote: [...]
In all these messages, and surely others, there is documentation material that could be retrofitted in the Twisted manual (editing out some unwanted parts and maybe rephrasing others). Is there someone, taking care of the documentation, to whom I should direct a copy of the above (if needed)?
Send it to the list, or if you think it's too large, post it on a website somewhere and send the URL to the list.
You are welcome to send it directly to me, but then you'll miss out on feedback from everyone else...
A wiki could be ideal for this... examples and documentation draft. I post two times my example, and it's the first time somebody correct it, if i didn't look at the list (i'm very busy now) i could post the mistake again... Maybe somebody will look in the archive and use my first post with the mistake ! -- William Dode - http://flibuste.net
On Wed, Apr 30, 2003 at 02:46:13PM +0200, William Dode wrote:
Andrew Bennetts <andrew-twisted@puzzling.org> writes:
Send it to the list, or if you think it's too large, post it on a website somewhere and send the URL to the list.
You are welcome to send it directly to me, but then you'll miss out on feedback from everyone else...
A wiki could be ideal for this... examples and documentation draft.
I post two times my example, and it's the first time somebody correct it, if i didn't look at the list (i'm very busy now) i could post the mistake again... Maybe somebody will look in the archive and use my first post with the mistake !
A wiki would probably be just as bad -- if people aren't looking in the list archives for unreplied to patches, they won't check the wiki either, and things would get lost. The current recommended strategy is to post to the list, maybe post a reminder (like you just did... thanks! :), and finally, if we still haven't responded, add it to our bug tracker on SourceForge, so we won't forget it for eternity. Pestering random developers on IRC can work well, too, but that can require a fair bit your time to make that work :) -Andrew.
participants (10)
-
Andrew Bennetts
-
Brian Warner
-
Christopher Armstrong
-
Donovan Preston
-
Matthias Urlichs
-
Moshe Zadka
-
Peter Hansen
-
pinard@iro.umontreal.ca
-
VAN DER HEIJDEN Maarten
-
William Dode