Comment out the 'loadtest' backend in the 'counter' backend sample so that it does...
[gae-samples.git] / sql-guestbook / guestbook.py
blob0c51ccc8da788af45dfffdbbfd59430bab887142
1 #!/usr/bin/python
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/
21 Schema:
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,
28 PRIMARY KEY (`id`),
29 KEY `date` (`date`)
30 ) DEFAULT CHARSET=utf8;
31 """
33 import cgi
34 import datetime
35 import sys
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
42 conn = None
43 DEBUG = True
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):
52 def __init__(self):
53 super(Guestbook, self).__init__()
55 def get(self):
56 """HTTP GET. Render the guestbook.
57 """
58 self.Render()
60 def Render(self):
61 """Render all of the posts in the datastore.
62 """
63 self.response.out.write(HEADER)
65 cursor = conn.cursor()
66 cursor.execute(
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
71 <br />
72 <i>&nbsp;&nbsp;-%s, %s UTC</i></p>
73 """ % (content, author, date))
75 self.response.out.write(FOOTER)
77 def post(self):
78 """HTTP POST. Store a new message, then render the guestbook.
79 """
80 cursor = conn.cursor()
81 query = 'INSERT INTO Greetings (author, date, content) VALUES(%s, %s, %s)' \
82 % (MARKER, MARKER, MARKER)
83 cursor.execute(query,
84 (cgi.escape(self.request.get('author')),
85 rdbms.Timestamp(*datetime.datetime.now().timetuple()[:6]),
86 cgi.escape(self.request.get('content', default_value= ''))))
87 conn.commit()
89 self.response.set_status(302)
90 self.response.headers['Location'] = '/'
93 def main(argv):
94 application = webapp.WSGIApplication([('.*', Guestbook)], debug=DEBUG)
95 global conn
96 conn = rdbms.connect(instance=INSTANCE, database=DATABASE_NAME)
97 util.run_wsgi_app(application)
100 HEADER = """
101 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
102 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
103 <html>
104 <head>
105 <title> Cloud SQL Guestbook </title>
106 <style type="text/css">
107 body {
108 width: 500px;
109 margin: 15px;
112 p.signature, div.form {
113 padding: 10px;
114 border: 1px solid Navy;
115 width: auto;
118 p.signature { background-color: Ivory; }
119 div.form { background-color: LightSteelBlue; }
120 </style>
121 </head>
123 <body>
124 <div id="main">
125 <div id="body">
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>.
133 </p>
135 <hr />
136 <div class="form">
137 Sign the guestbook!
139 <form action="/post" method="POST">
140 <table>
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>
144 </table>
145 </form>
146 </div>
149 FOOTER = """
150 </div></div>
151 </body>
152 </html>
155 if __name__ == '__main__':
156 main(sys.argv)