3 # Copyright 2011 Google Inc.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 """A guestbook sample app for Google Cloud SQL.
19 Deployed at http://sql-guestbook.appspot.com/
23 CREATE TABLE `Greetings` (
24 `id` int(11) NOT NULL AUTO_INCREMENT,
25 `author` varchar(32) DEFAULT NULL,
26 `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
27 `content` varchar(512) DEFAULT NULL,
30 ) DEFAULT CHARSET=utf8;
37 from google
.appengine
.api
import rdbms
38 from google
.appengine
.ext
import webapp
39 from google
.appengine
.ext
.webapp
import util
41 # rdbms db-api connection, globally cached
44 DATABASE_NAME
= 'guestbook'
45 INSTANCE
='google.com:speckle-python-demos:sqlguestbook-55'
47 _PARAM_MARKERS
= { 'qmark': '?', 'format': '%s' }
48 MARKER
= _PARAM_MARKERS
[rdbms
.paramstyle
]
51 class Guestbook(webapp
.RequestHandler
):
53 super(Guestbook
, self
).__init
__()
56 """HTTP GET. Render the guestbook.
61 """Render all of the posts in the datastore.
63 self
.response
.out
.write(HEADER
)
65 cursor
= conn
.cursor()
67 'SELECT author, date, content FROM Greetings ORDER BY date DESC LIMIT 10')
68 for author
, date
, content
in cursor
.fetchall():
69 self
.response
.out
.write("""
70 <p class="signature"> %s
72 <i> -%s, %s UTC</i></p>
73 """ % (content
, author
, date
))
75 self
.response
.out
.write(FOOTER
)
78 """HTTP POST. Store a new message, then render the guestbook.
80 cursor
= conn
.cursor()
81 query
= 'INSERT INTO Greetings (author, date, content) VALUES(%s, %s, %s)' \
82 % (MARKER
, MARKER
, MARKER
)
84 (cgi
.escape(self
.request
.get('author')),
85 rdbms
.Timestamp(*datetime
.datetime
.now().timetuple()[:6]),
86 cgi
.escape(self
.request
.get('content', default_value
= ''))))
89 self
.response
.set_status(302)
90 self
.response
.headers
['Location'] = '/'
94 application
= webapp
.WSGIApplication([('.*', Guestbook
)], debug
=DEBUG
)
96 conn
= rdbms
.connect(instance
=INSTANCE
, database
=DATABASE_NAME
)
97 util
.run_wsgi_app(application
)
101 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
102 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
105 <title> Cloud SQL Guestbook </title>
106 <style type="text/css">
112 p.signature, div.form {
114 border: 1px solid Navy;
118 p.signature { background-color: Ivory; }
119 div.form { background-color: LightSteelBlue; }
127 <h1> Cloud SQL Guestbook </h1>
129 <p style="font-style: italic">
130 This is a sample Python app
131 (<a href="http://code.google.com/p/google-app-engine-samples/source/browse/#svn/trunk/sql-guestbook">source</a>)
132 for <a href="https://code.google.com/apis/sql/">Google Cloud SQL</a>.
139 <form action="/post" method="POST">
141 <tr><td> Name: </td><td><input type="text" name="author"</td></tr>
142 <td> Message: </td><td><input type="textarea" name="content"</td></tr>
143 <td /><td><input type="submit" value="Sign"></td>
155 if __name__
== '__main__':