Skip to main content

django-reroute

django-reroute is a drop-in replacement for django.conf.urls.defaults that supports HTTP verb dispatch and view wrapping.

Download

Github: http://github.com/dnerdy/django-reroute

easy_install:

easy_install django-reroute

Source:

# Download the source and run
python setup.py install

Adding django-reroute to your project

django-reroute is a drop-in replacement for django.conf.urls.defaults:

# Replace
from django.conf.urls.defaults *

# with
from reroute import *

Although it’s better to be explicit:

# Replace
from django.conf.urls.defaults import handler404, handler500, patterns, url, include

# with
from reroute import handler404, handler500, patterns, url, include

HTTP verb dispatching

verb_url patterns can match HTTP verbs in addition to regexes:

from reroute.verbs import verb_url

urlpatterns = patterns('myapp.views',
    url('^regular$', 'regular_old_view'),
    verb_url('GET', '^restful$', 'restful_view')
)

verb_url pattern regexes can be overloaded, enabling routing solely based on HTTP verb:

urlpatterns = patterns('myapp.views',
    verb_url('GET', '^restful$', 'restful_view'),
    verb_url('PUT', '^restful$', 'another_restful_view')
)

Restful resource example:

paychecks = patterns('myapp.views.employees.paychecks',
    verb_url('GET',     '^paychecks$', 'index_paychecks'),
    verb_url('POST',    '^paychecks$', 'add_paycheck'),
)

urlpatterns = patterns('myapp.views.employees',
    verb_url('GET',     '^employees$', 'index_employees'),
    verb_url('POST',    '^employees$', 'add_employee'),

    verb_url('GET',     '^employees/(?P<employee_id>\d+)$', 'show_employee')
    verb_url('PUT',     '^employees/(?P<employee_id>\d+)$', 'update_employee')
    verb_url('DELETE',  '^employees/(?P<employee_id>\d+)$', 'delete_employee'),

    url('^employees/(?P<employee_id>\d+)/', include(paychecks)),
)

Rerouting through wrappers

You can configure a list of view wrappers for a set of url patterns:

from reroute import reroute_patterns

def params_wrapper(view, request, *args, **kwargs):
    # Provide uniform access of GET, POST or PUT parameters
    # through request.PARAMS

    if request.method == 'POST':
        request.PARAMS = dict(request.POST.iteritems())
    else:
        request.PARAMS = dict(request.GET.iteritems())

    return view(request, *args, **kwargs)

urlpatterns = reroute_patterns([params_wrapper], 'myapp.views',
    verb_url('GET', '^restful$', 'restful_view'),
    verb_url('PUT', '^restful$', 'another_restful_view')
)

A wrapper is any callable that takes the arguments: view, request, *args, **kwargs:

class Wrapper(object):
    def __call__(self, view, request, *args, **kwargs):
        return view(request, *args, **kwargs)

urlpatterns = reroute_patterns([Wrapper()], 'myapp.views',
    verb_url('GET', '^restful$', 'restful_view'),
    verb_url('PUT', '^restful$', 'another_restful_view')
)

And you can even get fancy and create your own drop-in replacement for patterns:

from functools import partial
import logging

def wrapper_one(view, request, *args, **kwargs):
    logging.debug("wrapper one")
    return view(request, *args, **kwargs)

def wrapper_two(view, request, *args, **kwargs):
    logging.debug("wrapper two")
    return view(request, *args, **kwargs)

patterns = partial(reroute_patterns, [wrapper_one, wrapper_two])

urlpatterns = patterns('myapp.views',
    verb_url('GET', '^restful$', 'restful_view'),
    verb_url('PUT', '^restful$', 'another_restful_view')
)

Author

django-reroute was written by Mark Sandstrom.

Metadata

Release files for django-reroute 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-reroute 0.1.1
File Size Uploaded Provenance
django-reroute-0.1.1.tar.gz 6.7 kB Without provenance Details

Release files / django-reroute-0.1.1.tar.gz

Download URL django-reroute-0.1.1.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
c957c10a31e64b063e50e038554e1cd62729cbd28659e37dec10948d85e753e9
BLAKE2b-256 checksum
How to use checksums
ef42c3b56176854c470d243c9d244e31101df8ab2896836f293dea1fef251b32
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for django-reroute 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page