Provides a route decorator for the Tornado framework.
This package is available via pip with pip install tornroutes for the stable
version.
You can also install the latest source (also usually very stable) by the following:
pip install -e git+git://github.com/nod/tornroutes.git#egg=tornroutes
Pretty well tested. You can run them with nosetests if you have nose
installed.
The following run in the base directory of the repo will run the tests:
virtualenv venv
source venv/bin/activate
pip install -r requirements.txt
nosetestsThe best source of information is the comments in tornroutes/init.py.
import tornado.web
from tornroutes import route
@route('/blah')
class SomeHandler(tornado.web.RequestHandler):
pass
t = tornado.web.Application(route.get_routes(), {'some app': 'settings'}Example carried over from above, if you have a template at generic.html and
you want it to get rendered at a certain uri, do the following:
generic_route('/generic/?', 'generic.html')Often, tornado projects end up defining something like BaseHandler that
extends tornado.web.RequestHandler and defines methods necessary for
authentication.
This handler might look like:
class BaseHandler(RequestHandler):
def get_current_user(self):
""" do stuff here to authenticate the user """
return None # NONE SHALL PASSWhich allows us to provide authenticated generic routes:
from tornroutes import authed_generic_route
authed_generic_route('/locked', 'some_locked_template.html', BaseHandler)Now, you'll have a uri of /locked being answered with a render of
some_locked_template.html if the user is authenticated, otherwise, they get
redirected to the value set at settings.login_url.