Skip to main content

Routes based dispatching for TurboGears2

Project description

About tgext.routes

https://travis-ci.org/TurboGears/tgext.routes.png https://coveralls.io/repos/TurboGears/tgext.routes/badge.png https://img.shields.io/:license-mit-blue.svg?style=flat-square

tgext.routes provides a simple way to integrate routes based dispatch into TurboGears2 applications.

Installing

tgext.routes can be installed both from pypi or from bitbucket:

pip install tgext.routes

should just work for most of the users

Routing Single Actions

Routes matching is done through the @route decorator, each exposed method can be bound to one or multiple routes.

The only requirement is that you inherit from the RoutedController instance.

All routes registered through the @route decorator are registered starting from the controller mount point.

The following example registers the entry_by_date method for urls like /date/2012-01 and /date/2012-01-01:

from tgext.routes import RoutedController, route

class DateController(RoutedController):
    @expose()
    @route('{year}-{month}', day=33)
    @route('{year}-{month}-{day}')
    def entry_by_date(self, year, month, day):
        return '%s ++ %s ++ %s' % (year, month, day)

class RootController(BaseController):
    date = DateController()

    @expose()
    def index(self):
        return 'Hello!'

Keep in mind that as @expose wraps it, the method is still accessible through ObjectDispatch routing, @route can just register additional routes.

If there is a route pointing to it, also actions that do not provide an @expose decoration are resolved, keep in mind that in that case you will have to render template manually.

In case you want to disable ObjectDispatch you can set disable_objectdispatch=True inside the controller. Keep in mind that it will disable ObjectDispatch for the whole controller and so you won’t be able to dispatch actions that do not provide a route from that controller on.

Routing Whole application

RoutedController can also be mounted as the application RootController. In that case instead of using the @route decorator you can even provide a routes.Mapper object as controller mapper attribute and register all the routes of your application:

class RootController(RoutedController):
    mapper = Mapper()
    mapper.connect('/', controller='home', action='index')
    mapper.connect('/json', controller='home', action='jsonexposed')
    mapper.connect('/unex', controller='home', action='unexposed')

In this case the controller argument is required and controller will be looked up inside the path specified by tg.config['paths']['controllers']. In the previous example a HomeController class will be looked for into the home.py module to serve the /, /json and /unex paths through its index, jsonexposed and unexposed methods.

In case both a mapper attribute and @route decorator are used inside the same RoutedController, the @route decorator is applied after the mapper routes.

For more documentation about routes refer to Routes Documentation

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tgext.routes-0.1.1.tar.gz (5.2 kB view details)

Uploaded Source

File details

Details for the file tgext.routes-0.1.1.tar.gz.

File metadata

File hashes

Hashes for tgext.routes-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2ce69e7d4e85943869152cc0f5ac1dc0ab65ba7754520044480cde06d9d64c37
MD5 036c5ea70511c3ae96edf962dfc74cda
BLAKE2b-256 62d2aa160bbcf155d093a3a4986060e1b1b6312c0f41d85e8642c25b15cce6b3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page