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.

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

print """<br />

Subject: %(subject)s<br />

Sender: %(sender)s<br />

%(text)s<br />

<br />

<br />

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

| Reply<br />

<br />

<br />

""" % row<br />

edit.cgi<br />

The edit script actually performs a dual function—it is used <strong>to</strong> edit new messages, but also <strong>to</strong><br />

edit replies. The difference isn’t all that great: If a reply_<strong>to</strong> is supplied in the CGI request, it is<br />

kept in a hidden input in the edit form. Also, the subject is set <strong>to</strong> "Re: parentsubject" by default<br />

(unless the subject already begins with "Re:"—you don’t want <strong>to</strong> keep adding those). Here is<br />

the code snippet that takes care of these details:<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 />

■Tip Hidden inputs are used <strong>to</strong> temporarily s<strong>to</strong>re information in a Web form. They don’t show up <strong>to</strong> the user<br />

as text areas and the like do, but their value is still passed <strong>to</strong> the CGI script that is the action of the form. That<br />

way the script that generates the form can pass information <strong>to</strong> the script that will eventually process the<br />

same form.<br />

Listing 26-6 shows the source code for the edit script.<br />

Listing 26-6. The Message Edi<strong>to</strong>r (edit.cgi)<br />

#!/usr/bin/python<br />

print 'Content-type: text/html\n'<br />

import cgitb; cgitb.enable()

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

Saved successfully!

Ooh no, something went wrong!