/Pycon2009/internet/examples/ch20/alt/friends3.py

https://github.com/EnTeQuAk/pydanny-event-notes · Python · 113 lines · 66 code · 22 blank · 25 comment · 15 complexity · 39eed87c519ebf6dc8b35478add86171 MD5 · raw file

  1. #!/usr/bin/env python
  2. '''
  3. $Id: friends3.py,v 1.1 2000/12/31 01:32:45 wesc Exp $
  4. Friends CGI demo
  5. '''
  6. import cgi
  7. from urllib import quote_plus
  8. from string import capwords
  9. #from sys import stderr
  10. #s = stderr.write
  11. header = 'Content-Type: text/html\n\n'
  12. url = 'http://localhost/cgi-bin/friends3.py'
  13. errhtml = '''<HTML><HEAD><TITLE>Friends CGI Demo</TITLE></HEAD>
  14. <BODY><H3>ERROR</H3>
  15. <B>%s</B><P>
  16. <FORM><INPUT TYPE=button VALUE=Back ONCLICK="window.history.back()"></FORM>
  17. </BODY></HTML>'''
  18. # showError() --> None
  19. def showError(error_str):
  20. 'showError() -- display error message'
  21. print header + errhtml % (error_str)
  22. friendradio = '<INPUT TYPE=radio NAME=howmany VALUE="%s" %s> %s\n'
  23. formhtml = '''<HTML><HEAD><TITLE>Friends CGI Demo</TITLE></HEAD>
  24. <BODY><H3>Friends list for: <I>%s</I></H3>
  25. <FORM ACTION="%s">
  26. <B>Your Name:</B>
  27. <INPUT TYPE=hidden NAME=action VALUE=edit>
  28. <INPUT TYPE=text NAME=person VALUE="%s" SIZE=15>
  29. <P><B>How many friends do you have?</B>
  30. %s
  31. <P><INPUT TYPE=submit></FORM></body></html>'''
  32. # showForm() --> None
  33. def showForm(who, howmany):
  34. 'showForm() -- presents blank or data-filled form for new input'
  35. friends = ''
  36. for i in [0, 10, 25, 50, 100]:
  37. checked = ''
  38. if str(i) == howmany:
  39. checked = 'CHECKED'
  40. friends = friends + friendradio % (str(i), checked, str(i))
  41. print header + formhtml % (who, url, who, friends)
  42. reshtml = '''<HTML><HEAD><TITLE>Friends CGI Demo</TITLE></HEAD>
  43. <BODY><H3>Friends list for: <I>%s</I></H3>
  44. Your name is: <B>%s</B><P>
  45. You have <B>%s</B> friends.
  46. <P>Click <a href="%s">here</a> to edit your data again.
  47. </BODY></HTML>'''
  48. # doResults() --> None
  49. def doResults(who, howmany):
  50. 'doResults() -- displays results with given form data'
  51. # substitute in real name and number of friends and return
  52. newurl = url + '?action=reedit&person=%s&howmany=%s' % (quote_plus(who), howmany)
  53. print header + reshtml % (who, who, howmany, newurl)
  54. # process() --> None
  55. def process():
  56. 'process() does all the work: grabs user data and determines routine to call'
  57. error = ''
  58. # initialize Data class object
  59. form = cgi.FieldStorage()
  60. #s('name: '+str(form.name)+'\n')
  61. #s('keys: '+str(form.keys())+'\n')
  62. #for i in form.keys():
  63. #s('item: '+str(form[i].name)+' has a value of '+str(form[i].value)+' and is a ' + form[i].__class__.__name__ + '\n')
  64. # get user name
  65. if form.has_key('person'):
  66. who = capwords(form['person'].value)
  67. else:
  68. who = 'NEW USER'
  69. # get name and number of friends
  70. if form.has_key('howmany'):
  71. howmany = form['howmany'].value
  72. else:
  73. if form.has_key('action') and form['action'].value == 'edit':
  74. error = 'Please select the number of friends you have.'
  75. else:
  76. howmany = 0
  77. # no errors, either display form or present results
  78. if not error:
  79. # if editing the first time, show results
  80. if form.has_key('action') and form['action'].value != 'reedit':
  81. doResults(who, howmany)
  82. # otherwise, show form
  83. else:
  84. showForm(who, howmany)
  85. # send error message back if error situation
  86. else:
  87. showError(error)
  88. # invoke if called directly
  89. if __name__ == '__main__':
  90. process()