Skip to main content

Pyramid-like routes for Django projects

Project description

Rhetoric

https://pypip.in/v/Rhetoric/badge.png https://pypip.in/d/Rhetoric/badge.png https://api.travis-ci.org/avanov/Rhetoric.png https://coveralls.io/repos/avanov/Rhetoric/badge.png?branch=develop

Status: Beta, Unstable API.

Naive implementation of Pyramid-like routes for Django projects.

Why it is worth your while

There’s a great article on why Pyramid routing subsystem is so convenient for web developers - Pyramid view configuration: Let me count the ways.

As a person who uses Pyramid as a foundation for his pet-projects, and Django - at work, I (the author) had a good opportunity to compare two different approaches to routing configuration provided by these frameworks. And I totally agree with the key points of the article - Pyramid routes are more flexible and convenient for developers writing RESTful services.

The lack of flexibility of standard Django url dispatcher motivated me to create this project. I hope it will be useful for you, and if you liked the idea behind Rhetoric URL Dispatcher, please consider Pyramid Web Framework for one of your future projects.

Project premises

  • Rhetoric components try to follow corresponding Pyramid components whenever possible.

  • Integration with django applications shall be transparent to existing code whenever possible.

  • Performance of Rhetoric URL Dispatcher is worse than of the one of Pyramid, due to naivety of the implementation and limitations imposed by the compatibility with Django API.

Installation

Rhetoric is available as a PyPI package:

$ pip install Rhetoric

The package shall be compatible with Python2.7, and Python3.3 or higher.

Integration with Django

  1. Replace django.middleware.csrf.CsrfViewMiddleware with rhetoric.middleware.CsrfProtectedViewDispatchMiddleware in your project’s MIDDLEWARE_CLASSES:

    # somewhere in a project_name.settings module
    
    MIDDLEWARE_CLASSES = [
        # ...
        'rhetoric.middleware.CsrfProtectedViewDispatchMiddleware',
        #'django.middleware.csrf.CsrfViewMiddleware',
        # ...
    ]
  2. Inside the project’s root urlconf (usually project_name.urls):

    from django.conf.urls import patterns, include, url
    # ... other imports ...
    from rhetoric import Configurator
    
    # ... various definitions ...
    
    urlpatterns = patterns('',
        # ... a number of standard django url definitions ...
    )
    
    # Rhetorical routing
    # ------------------
    config = Configurator()
    config.add_route('test.new.routes', '/test/new/routes/{param:[a-z]+}')
    config.scan(ignore=[
        # do not scan test modules included into the project tree
        re.compile('^.*[.]?tests[.]?.*$').match,
        # do not scan settings modules
        re.compile('^project_name.settings[_]?[_a-z09]*$').match,
    ])
    urlpatterns.extend(config.django_urls())
  3. Register views:

    # project_name.some_app.some_module
    
    from rhetoric import view_config
    
    
    @view_config(route_name="test.new.routes", renderer='json')
    def view_get(request, param):
        return {
            'Hello': param
        }
    
    @view_config(route_name="test.new.routes", renderer='json', request_method='POST')
    def view_post(request, param):
        return {
            'Hello': 'POST'
        }
  4. From this point you can request /test/new/routes/<param> with different methods.

Documentation

See complete documentation at http://rhetoric.readthedocs.org/

Changelog

  • 0.2.3

    • Cache request.json_body value after the first access to the attribute.

  • 0.2.0

  • 0.1.13

    • Depend on Venusian 1.0 and higher.

    • Allow re-assignment of the same ADT case implementations on subsequent venusian scans.

  • 0.1.9

    • Added support for the request.json_body property.

  • 0.1.8

    • Added support for the request.response API.

  • 0.1.7

    • Added support for the api_version predicate.

    • Added the view_defaults decorator.

  • 0.1.5

    • Feature: added support for decorator argument of view_config.

  • 0.1.4

    • Feature: added support for custom renderers.

  • 0.1.2

    • [Bugfix #2]: resolved race condition in rhetoric.view.ViewCallback.

    • [API]: rhetoric.middleware.UrlResolverMiddleware was renamed to rhetoric.middleware.CsrfProtectedViewDispatchMiddleware.

    • [Django integration]: rhetoric.middleware.CsrfProtectedViewDispatchMiddleware should now completely substitute django.middleware.csrf.CsrfViewMiddleware in MIDDLEWARE_CLASSES.

  • 0.1.0 - initial PyPI release. Early development, unstable API.

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

Rhetoric-0.2.3.tar.gz (28.1 kB view details)

Uploaded Source

Built Distribution

Rhetoric-0.2.3-py3-none-any.whl (36.7 kB view details)

Uploaded Python 3

File details

Details for the file Rhetoric-0.2.3.tar.gz.

File metadata

  • Download URL: Rhetoric-0.2.3.tar.gz
  • Upload date:
  • Size: 28.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for Rhetoric-0.2.3.tar.gz
Algorithm Hash digest
SHA256 8812d6d743990e1bb7e267db85a11d67b3544203064f5c642fad16cff50c0458
MD5 2b4b2bd48088d2323b3f3d781ad26afa
BLAKE2b-256 eebd9369dd884b4ba0946bdd2d7d6b4ed8398d4b77760458818e721a4fff67ce

See more details on using hashes here.

File details

Details for the file Rhetoric-0.2.3-py3-none-any.whl.

File metadata

File hashes

Hashes for Rhetoric-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 95aec03acc29832bbabc7d457250238ad74e132a5344b11d8a11d82149e03f7b
MD5 7944233bd29631acadb4c75666063e72
BLAKE2b-256 102149bf86b669d07da26a3d46938e56976f6fa5189c5100b383c7b6c10906f2

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