Source

confjoke / main.py

Full commit
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
from google.appengine.ext.db import BadValueError
from google.appengine.api import memcache
import model
import os
import cgi
from django.utils import simplejson


class MainPage(webapp.RequestHandler):

    def get(self):
        dbJoke = model.Joke.all()
        self.response.out.write(template.render('templates/main.html',
                                    {'jResult': dbJoke}))

class SaveData(webapp.RequestHandler):

    def get(self):
        self.redirect('/')

    def post(self):
        jWhen = self.request.get('txtWhen')
        jWhere = self.request.get('txtWhere')
        jWho = self.request.get('txtWho')
        jWhat = self.request.get('txtWhat')
        try:
            dbJoke = model.Joke(
                            jWhen = cgi.escape(jWhen),
                            jWhere = cgi.escape(jWhere),
                            jWho = cgi.escape(jWho),
                            jWhat = cgi.escape(jWhat)
                        ).put()
            self.redirect('/')
            dd = GetJoke()
            memcache.set('Joke',dd)
        except BadValueError:
            self.response.out.write('<center><p align="center">You should input some data! chu~mi~  ^.< </p><p align="center"><a href="/">&laquo; 返回</a></p>')

class GetData(webapp.RequestHandler):
    """
    Get JW
    """
    def get(self):
        MyData = memcache.get('Joke')

        if MyData is None:
            MyData = GetJoke()
        self.response.out.write(simplejson.dumps(MyData))

def GetJoke():
    """
    Get Joke data
    """
    query = model.Joke.all()
    #query.order('-date')
    data = query.fetch(5000)

    if len(data) > 0:
        dd = []
        for d in data:
            tmp = {}
            tmp['jWhen'] = d.jWhen
            tmp['jWhere'] = d.jWhere
            tmp['jWho'] = d.jWho
            tmp['jWhat'] = d.jWhat

            dd.append(tmp)
    return dd



def main():
    application = webapp.WSGIApplication([
                        ('/', MainPage),
                        ('/save', SaveData),
                        ('/get', GetData)
                        ],
                        debug=True)
    util.run_wsgi_app(application)


if __name__ == '__main__':
    main()