Skip to content

Instantly share code, notes, and snippets.

@msaisushma
Created April 15, 2014 06:23
Show Gist options
  • Save msaisushma/10706919 to your computer and use it in GitHub Desktop.
Save msaisushma/10706919 to your computer and use it in GitHub Desktop.
""".....code for the bus tracking system by the help of pyramid frame work....."""
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
from sqlalchemy import create_engine
from busroutes import BusRoute
from busroutes import Base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from pyramid.renderers import render
from pyramid.response import Response
from pyramid.view import view_config
DB_NAME = 'buses.db'
engine = create_engine('sqlite:///%s' % DB_NAME, echo=True)
"""to display the home page"""
def bus_routes(request):
return Response('<form action="/response" method="POST">Enter the bus no<input type="text" name="bus_no"/><button type="submit">Search Bus</button></form>')
def get_session(engine):
"""
Creates session object every single time this method is called
"""
return sessionmaker(bind=engine)()
def fetch_bus_info(request):
"""route X are the lists containing the bus routes information."""
# TODO: Dump these into the table in one shot.
# route1=['Corporation','Chamrajpet','NR colony','Chikkallasandra']
# route2=['Netkallappa circle','Chamrajpet','Navrang','Sujatha']
# route3=['Gandhi Bazaar', 'Jayanagar 4th block', 'East End', 'BTM Layout']
# route4=['Lalbagh', 'Nanda Theatre', 'Sangam Circle', 'Banashankari']
"""to fetch the data from the text box."""
bus_no = request.params.get('bus_no', '')
assert bus_no, 'Keyword expected as bus_no. Got nothing instead'
Session = get_session(engine)
results = Session.query(BusRoute).filter(BusRoute.bus_no==bus_no)
if results.count():
bus_route = results.one()
TEMPLATE_TEXT = "<b>Starting:{0}<br/>Ending:{1}<br/>Route Info:{2}".format(bus_route.start_point, bus_route.end_point, bus_route.route)
return Response(TEMPLATE_TEXT)
return Response("Invalid Bus No:%s" % bus_no)
# ''.format()
# print 'I am {0} and I\'m working with {1} and I am using {1} version no:{2}}'.format(('nanda', 'sqlalchemy', '0.9.4'))
# if (bus_no=='210N'):
# return Response('<b>'"Starting"'</b>'+':'+"Majestic"+" "+'<br>''<b>'"Ending"'</b>'+':'+"Uttarahalli"+" "+'<br>''<b>' "Stops in the route: " '</b>'+'<br>' ' '.join(str(place) for place in route1))
# elif (bus_no=='77B'):
# return Response('<b>'"Starting"'</b>'+':'+"NR colony"+" "+'<br>''<b>'"Ending"'</b>'+':'+"Mahalakshmi Layout"+" "+'<br>''<b>' "Stops in the route: " '</b>'+'<br>' ' '.join(str(place) for place in route2))
# elif (bus_no=='3E'):
# return Response('<b>'"Starting"'</b>'+':'+"Banashankari 3rd stage"+" "+'<br>''<b>'"Ending"'</b>'+':'+"BTM layout"+" "+'<br>''<b>' "Stops in the route: " '</b>'+'<br>' ' '.join(str(place) for place in route3))
# elif (bus_no=='12'):
# return Response('<b>'"Starting"'</b>'+':'+"KBS"+" "+'<br>''<b>'"Ending"'</b>'+':'+"Banashankari"+" "+'<br>''<b>' "Stops in the route: " '</b>'+'<br>' ' '.join(str(place) for place in route4))
# else:
# return Response("Invalid")
def sample_view(request):
result = render('templates/templates.pt',
{'start_point':'m','end_point':'u','route':'l'},
request=request)
response = Response(result)
return response
if __name__ == '__main__':
config = Configurator()
config.add_route('home', '/')
config.add_view(bus_routes, route_name='home')
config.add_route('bus', '/bus/')
config.add_view(bus_routes, route_name='bus')
config.add_route('bus_info', '/response')
config.add_view(fetch_bus_info, route_name='bus_info')
#config.scan('views')
app = config.make_wsgi_app()
server = make_server('0.0.0.0', 8083, app)
server.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment