Skip to content

Instantly share code, notes, and snippets.

@rociiu
Created May 5, 2010 12:13
Show Gist options
  • Save rociiu/390696 to your computer and use it in GitHub Desktop.
Save rociiu/390696 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from datetime import datetime
import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
from google.appengine.api import users
from google.appengine.ext import db
_DEBUG = True
class PPMessage(db.Model):
created_at = db.DateTimeProperty(auto_now_add=True)
content = db.StringProperty()
def formatted_datetime(self):
return self.created_at.strftime("%Y-%m-%d %H:%M")
class BaseRequestHandler(webapp.RequestHandler):
"""Supplies a common template generation function.
When you call generate(), we augment the template variables supplied with
the current user in the 'user' variable and the current webapp request
in the 'request' variable.
"""
def generate(self, template_name, template_values={}):
values = {
'request': self.request,
'user': users.get_current_user(),
'login_url': users.create_login_url(self.request.uri),
'logout_url': users.create_logout_url(self.request.uri),
'application_name': 'Checkin Bot',
}
values.update(template_values)
directory = os.path.dirname(__file__)
path = os.path.join(directory, os.path.join('templates', template_name))
self.response.out.write(template.render(path, values, debug=_DEBUG))
def head(self, *args):
pass
def get(self, *args):
pass
def post(self, *args):
pass
class Guestbook(BaseRequestHandler):
def post(self):
message = PPMessage()
message.content = self.request.get('message')
message.put()
self.redirect('/')
class MainHandler(BaseRequestHandler):
def get(self):
query = PPMessage.all()
messages = query.order('-created_at').fetch(10)
self.generate('index.html', {'messages':messages})
def main():
application = webapp.WSGIApplication([('/', MainHandler), ('/create',Guestbook )],
debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment