Skip to content

Instantly share code, notes, and snippets.

@embedly
Created April 22, 2010 20:13
Show Gist options
  • Save embedly/375751 to your computer and use it in GitHub Desktop.
Save embedly/375751 to your computer and use it in GitHub Desktop.
import os
import re
import cgi
import datetime
import wsgiref.handlers
from django.utils import simplejson
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
url_replace = re.compile('(?P<t>http:\/\/[-\w+&@#\/%?=~_|!:,.;]*[-\w+&@#\/%=~_|])')
class Share(db.Model):
"""Share Model."""
message = db.StringProperty(multiline=True)
time = db.DateTimeProperty(auto_now_add=True)
ip = db.StringProperty()
@property
def message_html(self):
"""Linkify the message for display"""
return url_replace.sub(r'<a target="_blank" href="\g<t>">\g<t></a>', self.message)
class IndexPage(webapp.RequestHandler):
"""The Main Page Handler"""
def get(self):
d = {}
#Make the call to the db to get the last 15 shares for display
d['shares'] = db.GqlQuery("SELECT * "
"FROM Share "
"ORDER BY time DESC LIMIT 15")
path = os.path.join(os.path.dirname(__file__), 'templates', 'index.html')
self.response.out.write(template.render(path,d))
class PostShare(webapp.RequestHandler):
"""Handles the Post."""
def post(self):
share = Share()
share.message = self.request.get('message')
share.ip = self.request.remote_addr
share.put()
self.response.out.write('')
application = webapp.WSGIApplication([
('/', IndexPage),
('/ajax', PostShare)
], debug=True)
def main():
wsgiref.handlers.CGIHandler().run(application)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment