1 #!/usr/bin/env python 2 3 "A form submission test." 4 5 import WebStack.Generic 6 7 class FormResource: 8 9 """ 10 A resource which handles incoming form submissions. 11 """ 12 13 def respond(self, trans): 14 15 """ 16 Examine the incoming request, decoding the form information. 17 """ 18 19 # NOTE: Some frameworks do not pass in the content type. 20 21 content_type = trans.get_content_type() 22 if content_type: 23 content_type_str = content_type.content_type 24 else: 25 content_type_str = None 26 27 fields = trans.get_fields_from_body() 28 29 # NOTE: Send the appropriate kind of response. 30 31 trans.set_content_type(WebStack.Generic.ContentType("text/html")) 32 out = trans.get_response_stream() 33 out.write(""" 34 <html> 35 <head> 36 <title>Form Test</title> 37 </head> 38 <body> 39 <h1>Form Test</h1> 40 <h2>Normal</h2> 41 <form method="post" action=""> 42 <input name="x" type="text" value="1"/><br/> 43 <input name="x" type="text" value="2"/><br/> 44 <input name="y" type="text" value="3"/><br/> 45 <input name="f" type="file"/><br/> 46 <input name="send" type="submit" value="Send!"/> 47 </form> 48 <h2>Multipart</h2> 49 <form method="post" action="" enctype="multipart/form-data"> 50 <input name="x" type="text" value="1"/><br/> 51 <input name="x" type="text" value="2"/><br/> 52 <input name="y" type="text" value="3"/><br/> 53 <input name="f" type="file"/><br/> 54 <input name="send" type="submit" value="Send!"/> 55 </form> 56 <h2>Content Type</h2> 57 <p>%s</p> 58 <h2>Fields from Body</h2> 59 <ul>%s</ul> 60 </body> 61 </html> 62 """ % ( 63 content_type_str, 64 self._format_fields(fields) 65 )) 66 67 def _format_fields(self, d): 68 return "".join([ 69 "<li>%s<ul>%s</ul></li>" % (key, self._format_list(value)) 70 for key, value in d.items() 71 ]) 72 73 def _format_list(self, l): 74 return "".join([ 75 "<li>%s</li>" % value 76 for value in l 77 ]) 78 79 # vim: tabstop=4 expandtab shiftwidth=4