Back to index

python-weblib  1.3.9
test-forms.py
Go to the documentation of this file.
00001 #!/usr/bin/python2.3
00002 
00003 import sys,os,types
00004 
00005 # Where to find own modules
00006 sys.path.append('/home/michael/Proj/python/pyweblib')
00007 
00008 import pyweblib
00009 
00010 from pyweblib import forms
00011 
00012 def ErrorExit(Msg):
00013   print """Content-type: text/html
00014 
00015 <html>
00016   <title>
00017     Error
00018   </title>
00019   <body>
00020     <h1>Error</h1>
00021     <p>%s</p>
00022   </body>
00023 </html>
00024 """ % Msg
00025 
00026 def PrintEmptyInputForm(form,Msg=''):
00027   print """Content-type: text/html
00028 
00029 <html>
00030   <title>
00031     Give me some data!
00032   </title>
00033   <body>
00034     <h1>Give me some data!</h1>
00035     <form
00036       method="POST"
00037       enctype="multipart/form-data"
00038       action="%s"
00039     >
00040       <table border>
00041 """ % (os.environ['SCRIPT_NAME'])
00042   for name in form.declaredFieldNames:
00043     f = form.field[name]
00044     print '<TR><TD>%s%s</TD><TD>%s</TD></TR>' % (
00045       f.labelHTML(),f.required*'<BR>required',f.inputHTML()
00046     )
00047   print """      </table>
00048       <input type="submit" value="Send">
00049     </form>
00050   </body>
00051 </html>
00052 """
00053 
00054 def PrintInputData(form):
00055   # Anzeige der eingegebenen Daten
00056   print """Content-type: text/html
00057 
00058 <html>
00059   <title>
00060     Your input data
00061   </title>
00062   <body>
00063     <h1>Your input data</h1>
00064     <table border>
00065 """
00066   for i in form.inputFieldNames:
00067     contentlist = []
00068     f = form.field[i]
00069     if f.value != None:
00070       if type(f.value) is types.ListType:
00071         contentlist.extend(f.valueHTML())
00072       else:
00073         contentlist.append(f.valueHTML())
00074     else:
00075       contentlist.append('&nbsp;')
00076     print '<tr><td>%s</td><td>%s</td></tr>' % (i,'<br>'.join(contentlist))
00077   print """
00078     </table>
00079   </body>
00080 </html>
00081 """
00082 
00083 form = forms.Form(sys.stdin,os.environ)
00084 form.addField(
00085   forms.Input(
00086     'param_input','Input',255,1,'abc.*',accessKey='1',
00087     size=25
00088   )
00089 )
00090 form.addField(
00091   forms.HiddenInput(
00092     'param_hidden','HiddenInput',255,1,'.*',default='Hidden Value',
00093     show=1
00094   )
00095 )
00096 form.addField(
00097   forms.Password(
00098     'param_password','Password',16,1,'.*',accessKey='3'
00099   )
00100 )
00101 form.addField(
00102   forms.Select(
00103     'param_select','Select',3,
00104     options=[('value1','Option 1'),'value2',('value3','Option 3')],
00105     default=['value2','value3'],
00106     size=3,multiSelect=1
00107   )
00108 )
00109 form.addField(
00110   forms.Radio('param_radio','Radio',default='value2',
00111     options=[('value1','Option 1'),'value2',('value3','Option 3')],
00112   )
00113 )
00114 form.addField(
00115   forms.Textarea(
00116     'param_textarea',
00117     'Additional Comments',
00118     1000,1,'Longer text:.*',default='Longer text:\nWrite here...',
00119     rows=5,cols=50
00120   )
00121 )
00122 form.addField(
00123   forms.Checkbox(
00124     'param_checkbox','Checkbox',default='Checked value'
00125   )
00126 )
00127 form.addField(
00128   forms.File(
00129     'param_file','File upload',100000,1,None,default='/etc/passwd',size=30
00130   )
00131 )
00132 
00133 try:
00134   form.getInputFields()
00135 except forms.FormException,e:
00136   ErrorExit(e.html())
00137 
00138 if not form.inputFieldNames:
00139   PrintEmptyInputForm(form,'')
00140 else:
00141   PrintInputData(form)
00142 
00143 sys.exit(0)