#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import wsgiref.handlers
import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from django.utils import simplejson
import db_model
class MainHandler(webapp.RequestHandler):
def get(self):
path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(template.render(path, {}))
class GridHandler(webapp.RequestHandler):
def post(self):
page = int(self.request.get('page'))
rp = int(self.request.get('rp'))
sortname = self.request.get('sortname')
sortorder = self.request.get('sortorder')
query = self.request.get('query')
qtype = self.request.get('qtype')
if sortname == '' : sortname = 'name'
if sortorder == '' : sortorder = '-'
start = (page - 1) * rp
if sortorder == 'desc' :
sortorder = '-'
else:
sortorder = ''
countries = db_model.Country.all()
countries.order( sortorder + sortname )
if query == '' :
rows = countries.fetch( rp, start )
count = countries.count()
else :
countries.filter( qtype + ' =', query.upper() )
rows = countries.fetch( rp, start )
count = countries.count()
cells = [ [r.iso, r.name, r.printable_name, r.iso3, r.numcode] for r in rows ]
results = [ {'cell' : c } for c in cells ]
ret = { 'page' : page,
'total' : count,
'rows' : results }
data = simplejson.dumps(ret)
self.response.out.write(data)
def main():
handlers = [ ('/', MainHandler),
('/grid_data', GridHandler) ]
application = webapp.WSGIApplication(handlers, debug=False)
wsgiref.handlers.CGIHandler().run(application)
if __name__ == '__main__':
main()