WebStack

examples/Common/Form/__init__.py

266:c0b134728a8f
2004-09-18 paulb [project @ 2004-09-18 17:49:37 by paulb] Improved the presentation of the form example and added "escaping" of characters in the presented values.
     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.media_type    24             content_type_charset = content_type.charset    25         else:    26             content_type_str = None    27             content_type_charset = None    28     29         # Optional encodings can be employed.    30     31         fields_from_path = trans.get_fields_from_path()    32     33         # Send the appropriate kind of response.    34     35         if fields_from_path.has_key("charset"):    36             charset = fields_from_path["charset"][0]    37             trans.set_content_type(WebStack.Generic.ContentType("text/html", charset))    38         elif content_type_charset:    39             charset = content_type_charset    40             trans.set_content_type(WebStack.Generic.ContentType("text/html", charset))    41         else:    42             charset = None    43             trans.set_content_type(WebStack.Generic.ContentType("text/html"))    44     45         # Handle charset issues.    46     47         if charset:    48             fields = trans.get_fields_from_body(charset)    49             all_fields = trans.get_fields(charset)    50         else:    51             fields = trans.get_fields_from_body()    52             all_fields = trans.get_fields()    53     54         out = trans.get_response_stream()    55     56         # Use Unicode for correct character encoding behaviour.    57     58         out.write(u"""    59 <html>    60   <head>    61     <title>Form Test</title>    62   </head>    63   <body>    64     <h1>Form Test</h1>    65     <table border="1" cellspacing="0" cellpadding="5">    66       <tr>    67         <th>Normal</th>    68         <th>Multipart</th>    69       </tr>    70       <tr>    71         <td>    72           <form method="post" action="">    73             <input name="x" type="text" value="1"/><br/>    74             <input name="x" type="text" value="2"/><br/>    75             <input name="y" type="text" value="3"/><br/>    76             <input name="f" type="file"/><br/>    77             <input name="send" type="submit" value="Send!"/>    78           </form>    79         </td>    80         <td>    81           <form method="post" action="" enctype="multipart/form-data">    82             <input name="x" type="text" value="1"/><br/>    83             <input name="x" type="text" value="2"/><br/>    84             <input name="y" type="text" value="3"/><br/>    85             <input name="f" type="file"/><br/>    86             <input name="send" type="submit" value="Send!"/>    87           </form>    88         </td>    89       </tr>    90       <tr>    91         <th rowspan="2">Content Type</th>    92         <th colspan="2">Charset</th>    93       </tr>    94       <tr>    95         <th>From Content Type</th>    96         <th>In Use</th>    97       </tr>    98       <tr>    99         <td>%s</td>   100         <td>%s</td>   101         <td>%s</td>   102       </tr>   103       <tr>   104         <th>Fields from Body</th>   105         <th>Fields from Body and Path</th>   106       </tr>   107       <tr>   108         <td>   109           <ul>%s</ul>   110         </td>   111         <td>   112           <ul>%s</ul>   113         </td>   114       </tr>   115     </table>   116   </body>   117 </html>   118 """ % (   119             content_type_str,   120             content_type_charset,   121             charset,   122             self._format_fields(fields),   123             self._format_fields(all_fields),   124         ))   125    126     def _format_fields(self, d):   127         return "".join([   128             "<li>%s<ul>%s</ul></li>" % (key, self._format_list(value))   129             for key, value in d.items()   130         ])   131    132     def _format_list(self, l):   133         return "".join([   134             "<li>%s</li>" % (value.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;") or "<em>empty</em>")   135             for value in l   136         ])   137    138 # vim: tabstop=4 expandtab shiftwidth=4