Skip to main content
Author:

Keryn Knight

Version:
0.1.1

Release

Status

stable (0.1.1)

travis_stable

master

travis_master

What it does

Ever used Django and wished you could have a context processor which received the existing context, along with the request, so that it could do different things depending on the values the view provided? This does that, as long as you’re using TemplateResponse objects and not render() or render_to_response().

Installation

You can use pip to install the 0.1.1 version from PyPI:

pip install django-contextaware-processors==0.1.1

Or you can grab it from GitHub like this:

pip install -e git+https://github.com/kezabelle/django-contextaware-processors.git#egg=django-contextaware-processors

Usage

Add a new CONTEXTAWARE_PROCESSORS setting to your project configuration. It should be an iterable of strings representing the dotted paths to your processors, just the same as the Django context processors are configured:

CONTEXTAWARE_PROCESSORS = ('path.to.my_processor', 'another_processor.lives.here')

Processors are executed in the order in which they are declared, and update the original context data. The new context is given to subsequent processors, such that the last processor above (another_processor.lives.here) will see any changes made by path.to.my_processor.

Using the middleware

In most cases, if you’re using TemplateResponse objects (or any Class Based View which uses them for you), you want to use the provided middleware:

MIDDLEWARE_CLASSES = (
    # ...
    'contextaware_processors.middleware.ContextawareProcessors',
    # ...
)

As this makes use of process_response(request, response) you probably want it somewhere near the bottom, so that it modifies the context on the way out as soon as possible. The middleware will automatically apply any processors defined in CONTEXTAWARE_PROCESSORS

Using the TemplateResponse subclass

For custom situations, there is context_processors.response.ContextawareTemplateResponse class which exposes an add_context_callback(callback_function) which can be used to apply view-specific context modifiers, though why you’d need to is not immeidiately obvious to me ;) If the ContextawareProcessors middleware notices a ContextawareTemplateResponse it will add those defined in CONTEXTAWARE_PROCESSORS after any previously registered custom modifiers.

Writing a context-aware processor

The API contract for a processor is the same as a normal context processor, but with the addition of a context parameter, sent as a named-kwarg.

A normal context processor looks like:

def my_processor(request):
    return {'MY_VALUE': 1}

While a context-aware processor looks like:

def my_processor(request, context):
    if 'MY_KEY' in context:
        return {'MY_VALUE': 2}
    return {'MY_VALUE': None}

Return values

A context-aware processor must return one of 3 things: - A dictionary to .update(...) the existing context with, - NotImplemented may be used to mark it as irrelevant for the request - For convienience, None may also be used to skip updating the context.

Supported Django versions

The tests are run against Django 1.8 through 1.10, and Python 2.7, 3.3, 3.4 and 3.5.

Running the tests

If you have a cloned copy, you can do:

python setup.py test

If you have tox, you can just do:

tox

Contributing

Please do!

The project is hosted on GitHub in the kezabelle/django-contextaware-processors repository.

Bug reports and feature requests can be filed on the repository’s issue tracker.

If something can be discussed in 140 character chunks, there’s also my Twitter account.

The license

It’s FreeBSD. There’s should be a LICENSE file in the root of the repository, and in any archives.


Copyright (c) 2017, Keryn Knight All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


Change history for django-contextaware-processors

0.1.1

  • Initial release

Release files for django-contextaware-processors 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-contextaware-processors 0.1.1
File Size Uploaded
django-contextaware-processors-0.1.1.tar.gz 10.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-contextaware-processors 0.1.1
File Interpreter ABI Platform
django_contextaware_processors-0.1.1-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 25.3 kB

Release files / django-contextaware-processors-0.1.1.tar.gz

Download URL django-contextaware-processors-0.1.1.tar.gz
Size 10.8 kB
Tags Source
SHA-256 checksum
How to use checksums
8e505ce7825cedb5b85ee3faeae5caa2b8ffc288ab93b508d69e768fc9f98b13
BLAKE2b-256 checksum
How to use checksums
870fa25d361ca978b761a50c8823b5cb2aca1b7f3f5affe411be888e677f4381
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / django_contextaware_processors-0.1.1-py2.py3-none-any.whl

Download URL django_contextaware_processors-0.1.1-py2.py3-none-any.whl
Size 14.5 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
cd2d87da65455721e4414f7a5c1a42ea4abf68c5468b4b7098cfa045df966bf7
BLAKE2b-256 checksum
How to use checksums
f76575d0fe07724e971d73d20d38ebd430a07da98faa02dd00ba943a29fa85b0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

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