Share Pastebin
Guest
Public paste!

Untitled

By: a guest | May 15th, 2008 | Syntax: Python | Size: 2.35 KB | Hits: 130 | Expires: Never
Copy text to clipboard
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2007 Google Inc.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. #     http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17.  
  18.  
  19.  
  20.  
  21. import wsgiref.handlers
  22. import os
  23.  
  24. from google.appengine.ext import webapp
  25. from google.appengine.ext.webapp import template
  26.  
  27. from django.utils import simplejson
  28.  
  29. import db_model
  30.  
  31. class MainHandler(webapp.RequestHandler):
  32.  
  33.   def get(self):
  34.     path = os.path.join(os.path.dirname(__file__), 'index.html')
  35.     self.response.out.write(template.render(path, {}))
  36.  
  37. class GridHandler(webapp.RequestHandler):
  38.   def post(self):
  39.     page = int(self.request.get('page'))
  40.     rp = int(self.request.get('rp'))
  41.     sortname = self.request.get('sortname')
  42.     sortorder = self.request.get('sortorder')
  43.     query = self.request.get('query')
  44.     qtype = self.request.get('qtype')
  45.  
  46.     if sortname == '' : sortname = 'name'
  47.     if sortorder == '' : sortorder = '-'
  48.  
  49.     start = (page - 1) * rp
  50.     if sortorder == 'desc' :
  51.       sortorder = '-'
  52.     else:
  53.       sortorder = ''
  54.  
  55.     countries = db_model.Country.all()
  56.     countries.order( sortorder  + sortname )
  57.     if query == '' :
  58.       rows = countries.fetch( rp, start )
  59.       count = countries.count()
  60.     else :
  61.       countries.filter( qtype + ' =', query.upper() )
  62.       rows = countries.fetch( rp, start )
  63.       count = countries.count()
  64.      
  65.     cells = [ [r.iso, r.name, r.printable_name, r.iso3, r.numcode] for r in rows ]
  66.     results = [ {'cell' : c } for c in cells ]
  67.      
  68.     ret = { 'page' : page,
  69.             'total' : count,
  70.             'rows' : results }
  71.     data = simplejson.dumps(ret)
  72.     self.response.out.write(data)
  73.  
  74. def main():
  75.   handlers = [ ('/', MainHandler),
  76.                ('/grid_data', GridHandler) ]
  77.   application = webapp.WSGIApplication(handlers, debug=False)
  78.   wsgiref.handlers.CGIHandler().run(application)
  79.  
  80.  
  81. if __name__ == '__main__':
  82.   main()