diff --git a/python2.7/guestbook-mysql/app.yaml b/python2.7/guestbook-mysql/app.yaml new file mode 100644 index 00000000..e69702cf --- /dev/null +++ b/python2.7/guestbook-mysql/app.yaml @@ -0,0 +1,19 @@ +application: guestbook-mysql +version: 1 +runtime: python27 +api_version: 1 +threadsafe: yes + +handlers: +- url: .* + script: guestbook.application + +libraries: +- name: webapp2 + version: "2.5.2" + +- name: MySQLdb + version: "latest" + +- name: jinja2 + version: "latest" diff --git a/python2.7/guestbook-mysql/guestbook.py b/python2.7/guestbook-mysql/guestbook.py new file mode 100644 index 00000000..c8cee162 --- /dev/null +++ b/python2.7/guestbook-mysql/guestbook.py @@ -0,0 +1,65 @@ +import cgi +import webapp2 +from google.appengine.ext.webapp.util import run_wsgi_app + +import MySQLdb +import os +import jinja2 + +# Configure the Jinja2 environment. +JINJA_ENVIRONMENT = jinja2.Environment( + loader=jinja2.FileSystemLoader(os.path.dirname(__file__)), + autoescape=True, + extensions=['jinja2.ext.autoescape']) + +class MainPage(webapp2.RequestHandler): + def get(self): + # Display existing guestbook entries and a form to add new entries. + db = MySQLdb.connect(host='localhost', port=3306, db='guestbook', + user='root') + + cursor = db.cursor() + cursor.execute('SELECT guestName, content, entryID FROM entries') + + # Create a list of guestbook entries to render with the HTML. + guestlist = []; + for row in cursor.fetchall(): + guestlist.append(dict([('name',cgi.escape(row[0])), + ('message',cgi.escape(row[1])), + ('ID',row[2]) + ])) + + variables = {'guestlist': guestlist} + template = JINJA_ENVIRONMENT.get_template('main.html') + self.response.write(template.render(variables)) + db.close() + +class Guestbook(webapp2.RequestHandler): + def post(self): + # Handle the post to create a new guestbook entry. + fname = self.request.get('fname') + content = self.request.get('content') + + db = MySQLdb.connect(host='localhost', port=3306, db='guestbook', + user='root') + + cursor = db.cursor() + # Note that the only format string supported is %s + cursor.execute('INSERT INTO entries (guestName, content) VALUES (%s, %s)', (fname, content)) + db.commit() + db.close() + + self.redirect("/") + +application = webapp2.WSGIApplication([('/', MainPage), + ('/sign', Guestbook)], + debug=True) + +def main(): + application = webapp2.WSGIApplication([('/', MainPage), + ('/sign', Guestbook)], + debug=True) + run_wsgi_app(application) + +if __name__ == "__main__": + main() diff --git a/python2.7/guestbook-mysql/main.html b/python2.7/guestbook-mysql/main.html new file mode 100644 index 00000000..33ae81bc --- /dev/null +++ b/python2.7/guestbook-mysql/main.html @@ -0,0 +1,32 @@ + + +
+| Name | +Message | +ID | +
|---|---|---|
| {{ guest['name'] }} | +{{ guest['message'] }} | +{{ guest['ID'] }} | +