16.01.2014 Views

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

494 CHAPTER 26 ■ PROJECT 7: YOUR OWN BULLETIN BOARD<br />

import psycopg<br />

conn = psycopg.connect('dbname=foo user=bar')<br />

curs = conn.cursor()<br />

import cgi, sys<br />

form = cgi.FieldS<strong>to</strong>rage()<br />

reply_<strong>to</strong> = form.getvalue('reply_<strong>to</strong>')<br />

print """<br />

<br />

<br />

Compose Message<br />

<br />

<br />

Compose Message<br />

<br />

"""<br />

subject = ''<br />

if reply_<strong>to</strong> is not None:<br />

print '' % reply_<strong>to</strong><br />

curs.execute('SELECT subject FROM messages WHERE id = %s' % reply_<strong>to</strong>)<br />

subject = curs.fetchone()[0]<br />

if not subject.startswith('Re: '):<br />

subject = 'Re: ' + subject<br />

print """<br />

Subject:<br />

<br />

Sender:<br />

<br />

Message:<br />

<br />

<br />

<br />

<br />

Back <strong>to</strong> the main page'<br />

<br />

<br />

""" % subject<br />

save.cgi<br />

Now on <strong>to</strong> the final script. The save script will receive information about a message (from the<br />

form generated by edit.cgi) and will s<strong>to</strong>re it in the database. That means using an SQL insert

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!