Skip to content

Instantly share code, notes, and snippets.

@sorah sorah/kae.py
Created Jul 12, 2009

Embed
What would you like to do?
import wsgiref.handlers
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext import db
import os
import random
import string
import time
#methods
def random_str():
return ''.join([random.choice(string.ascii_letters+string.digits) for i in range(10)])
def get_page(urlid):
if not(urlid):
return False
else:
a = Page.all().filter("urlid =",urlid)
if a.count() <= 0:
return False
else:
return a[0]
#model
class Page(db.Model):
title = db.StringProperty (required=True)
body = db.TextProperty(required=True)
urlid = db.StringProperty(required=True)
date = db.TimeProperty (auto_now_add=True)
#pages
class RandomPage(webapp.RequestHandler):
def get(self):
q = Page.all().count()
if q == 0:
self.redirect("/create")
else:
urlid = Page.get_by_id(random.randint(1,q)).urlid
self.redirect('/entry?id='+urlid)
class EntryPage(webapp.RequestHandler):
def get(self):
tpage = get_page(self.request.get("id"))
if not(tpage):
self.response.out.write(
template.render("entry.html",
{"page_title":"Page doesn't exist!",
"page_body":"Sorry, Page doesn't exist.",
"page_id":""}))
else:
self.response.out.write(
template.render("entry.html",
{"page_title":tpage.title,
"page_body":tpage.body,
"page_id":tpage.urlid}))
class EditPage(webapp.RequestHandler):
def get(self):
tpage = get_page(self.request.get("id"))
if tpage:
self.response.out.write(
template.render("edit.html",
{"post_url":"/edit?id="+tpage.urlid,
"page_title":tpage.title,
"page_body":tpage.body}))
else:
self.response.out.write("Error")
def post(self):
tbody = self.request.get("ebody")
ttitle = self.request.get("etitle")
tpage = get_page(self.request.get("id"))
if tpage:
tpage.body = tbody
tpage.title = ttitle
tpage.put()
self.redirect("/entry?id="+tpage.urlid)
else:
self.response.out.write("Error")
class CreatePage(webapp.RequestHandler):
def get(self):
self.response.out.write(
template.render("edit.html",
{"post_url":"/create",
"page_title":"",
"page_body":""}))
def post(self):
tbody = self.request.get("ebody")
ttitle = self.request.get("etitle")
page = Page(title=ttitle,body=tbody,urlid=random_str()+time.strftime("%s"))
page.put()
self.redirect('/entry?id='+str(page.urlid))
#main
def main():
application = webapp.WSGIApplication([
('/',RandomPage),
('/entry',EntryPage),
('/edit',EditPage),
('/create',CreatePage)])
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
You can’t perform that action at this time.