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