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@222 | 24 | content_type_charset = content_type.charset |
paulb@195 | 25 | else: |
paulb@195 | 26 | content_type_str = None |
paulb@222 | 27 | content_type_charset = None |
paulb@195 | 28 | |
paulb@222 | 29 | # Optional encodings can be employed. |
paulb@222 | 30 | |
paulb@222 | 31 | fields_from_path = trans.get_fields_from_path() |
paulb@222 | 32 | |
paulb@222 | 33 | # Send the appropriate kind of response. |
paulb@87 | 34 | |
paulb@222 | 35 | if fields_from_path.has_key("charset"): |
paulb@222 | 36 | charset = fields_from_path["charset"][0] |
paulb@222 | 37 | trans.set_content_type(WebStack.Generic.ContentType("text/html", charset)) |
paulb@222 | 38 | elif content_type_charset: |
paulb@222 | 39 | charset = content_type_charset |
paulb@222 | 40 | trans.set_content_type(WebStack.Generic.ContentType("text/html", charset)) |
paulb@222 | 41 | else: |
paulb@222 | 42 | charset = None |
paulb@222 | 43 | trans.set_content_type(WebStack.Generic.ContentType("text/html")) |
paulb@87 | 44 | |
paulb@222 | 45 | # Handle charset issues. |
paulb@222 | 46 | |
paulb@222 | 47 | if charset: |
paulb@222 | 48 | fields = trans.get_fields_from_body(charset) |
paulb@222 | 49 | else: |
paulb@222 | 50 | fields = trans.get_fields_from_body() |
paulb@222 | 51 | |
paulb@87 | 52 | out = trans.get_response_stream() |
paulb@87 | 53 | out.write(""" |
paulb@87 | 54 | <html> |
paulb@87 | 55 | <head> |
paulb@87 | 56 | <title>Form Test</title> |
paulb@87 | 57 | </head> |
paulb@87 | 58 | <body> |
paulb@87 | 59 | <h1>Form Test</h1> |
paulb@87 | 60 | <h2>Normal</h2> |
paulb@87 | 61 | <form method="post" action=""> |
paulb@87 | 62 | <input name="x" type="text" value="1"/><br/> |
paulb@87 | 63 | <input name="x" type="text" value="2"/><br/> |
paulb@87 | 64 | <input name="y" type="text" value="3"/><br/> |
paulb@87 | 65 | <input name="f" type="file"/><br/> |
paulb@87 | 66 | <input name="send" type="submit" value="Send!"/> |
paulb@87 | 67 | </form> |
paulb@87 | 68 | <h2>Multipart</h2> |
paulb@87 | 69 | <form method="post" action="" enctype="multipart/form-data"> |
paulb@87 | 70 | <input name="x" type="text" value="1"/><br/> |
paulb@87 | 71 | <input name="x" type="text" value="2"/><br/> |
paulb@87 | 72 | <input name="y" type="text" value="3"/><br/> |
paulb@87 | 73 | <input name="f" type="file"/><br/> |
paulb@87 | 74 | <input name="send" type="submit" value="Send!"/> |
paulb@87 | 75 | </form> |
paulb@87 | 76 | <h2>Content Type</h2> |
paulb@87 | 77 | <p>%s</p> |
paulb@222 | 78 | <h2>Charset</h2> |
paulb@222 | 79 | <h3>From Content Type</h3> |
paulb@222 | 80 | <p>%s</p> |
paulb@222 | 81 | <h3>In Use</h3> |
paulb@222 | 82 | <p>%s</p> |
paulb@195 | 83 | <h2>Fields from Body</h2> |
paulb@195 | 84 | <ul>%s</ul> |
paulb@87 | 85 | </body> |
paulb@87 | 86 | </html> |
paulb@195 | 87 | """ % ( |
paulb@195 | 88 | content_type_str, |
paulb@222 | 89 | content_type_charset, |
paulb@222 | 90 | charset, |
paulb@195 | 91 | self._format_fields(fields) |
paulb@195 | 92 | )) |
paulb@195 | 93 | |
paulb@195 | 94 | def _format_fields(self, d): |
paulb@195 | 95 | return "".join([ |
paulb@195 | 96 | "<li>%s<ul>%s</ul></li>" % (key, self._format_list(value)) |
paulb@195 | 97 | for key, value in d.items() |
paulb@195 | 98 | ]) |
paulb@195 | 99 | |
paulb@195 | 100 | def _format_list(self, l): |
paulb@195 | 101 | return "".join([ |
paulb@195 | 102 | "<li>%s</li>" % value |
paulb@195 | 103 | for value in l |
paulb@195 | 104 | ]) |
paulb@87 | 105 | |
paulb@87 | 106 | # vim: tabstop=4 expandtab shiftwidth=4 |