1. Chun-Ping Chang
  2. confjoke

Source

confjoke / api.py

#!/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 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
        #FIXME:need use memcache
        joke_query = model.Joke.all()
        joke_query.filter("conf =", conf_data[0])
        joke_query.fetch(1000)

        output = []
        for d in joke_query:
            output.append(
                {'what':d.what,
                 'when':d.when,
                 'who':d.who,
                 'where':d.where,
                 #FIXME:need define output format
                 'time':d.time.isoformat()}
            )

        self.response.out.write(simplejson.dumps(output))


class SearchConferenceList( webapp.RequestHandler ):
    """
    Search conference name by keyword, if no keyword then list all conference
    """
    def get(self):
        keyword = cgi.escape( self.request.get('keyword') )
        query = model.Conference.all()
        #if had keyword
        if len( keyword ):
            query.filter('name >=', keyword)
        query.fetch(1000)

        output = []
        for conf_list in query:
            output.append(
                {'name': conf_list.name,
                 'desc': conf_list.desc})

        self.response.out.write(simplejson.dumps( output ))

def main():
    application = webapp.WSGIApplication([
                        ('/API/GetConfInfo', GetConfInfo),
                        ('/API/SearchConfList',SearchConferenceList)],
                        debug=True)
    util.run_wsgi_app(application)


if __name__ == '__main__':
    main()