Source

voosonline / src / app.py

Full commit
#!/usr/bin/python
# -*- coding: utf-8 -*-
from datetime import date
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
import os

from models import Airport, Arrival, Departure, Event
from util import prefetch_refprop

class MyRequestHandler(webapp.RequestHandler):
    def render_template(self, template_name, template_values={}, **kwargs):
        template_folder = os.path.join(os.path.dirname(__file__), 'templates')
        path = os.path.join(template_folder, template_name)
        for key, value in kwargs.iteritems():
            template_values[key] = value
        self.response.out.write(template.render(path, template_values))


class MainPage(MyRequestHandler):
    def get(self):
        airports = Airport.all().order('infraero_name').fetch(1000)
        self.render_template('index.html', airports=airports)


class AirportPage(MyRequestHandler):
    def get(self, iata_code):
        airport = Airport.all().filter('iata_code =', iata_code).get()
        self.render_template('airport.html', airport=airport, iata_code=iata_code)


class EventHandler(MyRequestHandler):
    def get_by_event_type(self, iata_code, departure):
        airport = Airport.all().filter('iata_code =', iata_code).get()
        if departure:
            query = Departure.all().filter('source =', airport)
        else:
            query = Arrival.all().filter('destination =', airport)

        events = query.filter('date =', date.today())\
                      .order('confirmed').fetch(1000)

        prefetch_refprop(events, Event.company)
        prefetch_refprop(events, Event.source)

        template_values = {'airport': airport,
                           'events': events,
                           'departure': departure}

        self.render_template('panel.html', template_values)


class ArrivalsPage(EventHandler):
    def get(self, iata_code):
        self.get_by_event_type(iata_code, departure=False)


class DeparturesPage(EventHandler):
    def get(self, iata_code):
        self.get_by_event_type(iata_code, departure=True)


application = webapp.WSGIApplication([('/', MainPage),
                                      ('/(\w*)/$', AirportPage),
                                      ('/(\w*)/partidas/', DeparturesPage),
                                      ('/(\w*)/chegadas/', ArrivalsPage),
                                      ], debug=True)


def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()