Last active
August 29, 2015 13:59
-
-
Save msaisushma/10719647 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""".....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 | |
DOMAIN = '0.0.0.0' | |
PORT = 8000 | |
DB_NAME = 'buses.db' | |
def create_engine(): | |
"""Creates engine every single time this method is called""" | |
engine = create_engine('sqlite:///%s' % DB_NAME, echo=True) | |
return engine | |
def get_session(engine): | |
""" | |
Creates session object every single time this method is called | |
""" | |
return sessionmaker(bind=engine)() | |
def create_configuration(): | |
""" | |
Prepares configuration and adds routes to it | |
""" | |
config = Configurator() | |
config.add_route('home', '/') | |
# TODO: check @view_config decorator | |
#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(bus_info, route_name='bus_info') | |
config.include('pyramid_chameleon') | |
config.scan('views') | |
return config | |
def start_server(config): | |
""" | |
Runs a pyramid server at the configured domain:port | |
""" | |
app = config.make_wsgi_app() | |
server = make_server(DOMAIN, PORT, app) | |
print 'Server Started listening on %s:%s' % (DOMAIN, PORT) | |
server.serve_forever() | |
if __name__ == '__main__': | |
config = create_configuration() | |
start_server(config) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>Bus serach form</title> | |
</head> | |
<body> | |
<form action="/response" method="POST">Enter the bus no<input type="text" name="bus_no"/><button type="submit">Search Bus</button></form> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@view_config(route_name='home', renderer='templates.pt') | |
def home_view(request): | |
return Response('<h1><p>Welcome</p></h1>') | |
@view_config(route_name='bus') | |
def form_view(request): | |
result = render('templates.pt', | |
{'start_point':'m','end_point':'u','route':'l'}, | |
request=request) | |
response = Response(result) | |
return response | |
@view_config(route_name='response', renderer='templates.pt') | |
def fetch_bus_info(request): | |
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) | |
results = Session.query(BusRoute).all() | |
if results.count(): | |
bus_route = results.one() | |
TEMPLATE_TEXT = {'bus_route.start_point':'m','bus_route.end_point':'u','bus_route.route':'l'} | |
return Response(TEMPLATE_TEXT) | |
return Response("Invalid Bus No:%s" % bus_no) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment