Commits

Kang-min Wang committed 76ab5ab

add api.py

Comments (0)

Files changed (1)

+#!/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
+
+
+class GetConfInfo(  webapp.RequestHandler ):
+    """
+    Get conference joke information
+    """
+    def get(self):
+        conf_name = cgi.escape(self.request.get('name'))
+
+        #get conf information
+        #FIXME:need use memcache
+        conf_query = model.Conference.all()
+        conf_query.filter("name =", conf_name)
+        conf_data = conf_query.fetch(1)
+
+        #get confjoke
+        #joke_query = model.Joke.all()
+        #joke_query.filter("conf =". conf_data)
+        print conf_data
+
+
+
+
+
+def main():
+    application = webapp.WSGIApplication([
+                        ('/API/GetConfInfo', GetConfInfo)],
+                        debug=True)
+    util.run_wsgi_app(application)
+
+
+if __name__ == '__main__':
+    main()