listing26-7.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/python
  2. print('Content-type: text/html\n')
  3. import cgitb; cgitb.enable()
  4. import psycopg2
  5. conn = psycopg2.connect('user=foo password=bar dbname=baz')
  6. curs = conn.cursor()
  7. import cgi, sys
  8. form = cgi.FieldStorage()
  9. reply_to = form.getvalue('reply_to')
  10. print("""
  11. <html>
  12. <head>
  13. <title>Compose Message</title>
  14. </head>
  15. <body>
  16. <h1>Compose Message</h1>
  17. <form action='save.cgi' method='POST'>
  18. """)
  19. subject = ''
  20. if reply_to is not None:
  21. print('<input type="hidden" name="reply_to" value="{}"/>'.format(reply_to))
  22. curs.execute('SELECT subject FROM messages WHERE id = %s', (format(reply_to),))
  23. subject = curs.fetchone()[0]
  24. if not subject.startswith('Re: '):
  25. subject = 'Re: ' + subject
  26. print("""
  27. <b>Subject:</b><br />
  28. <input type='text' size='40' name='subject' value='{}' /><br />
  29. <b>Sender:</b><br />
  30. <input type='text' size='40' name='sender' /><br />
  31. <b>Message:</b><br />
  32. <textarea name='text' cols='40' rows='20'></textarea><br />
  33. <input type='submit' value='Save'/>
  34. </form>
  35. <hr />
  36. <a href='main.cgi'>Back to the main page</a>'
  37. </body>
  38. </html>
  39. """.format(subject))