Package: django-contextaware-processors
Author:Keryn KnightVersion:0.1.1ReleaseStatusstable (0.1.1)masterSectionsWhat it doesInstallationUsageUsing the middlewareUsing the TemplateResponse subclassWriting a context-aware processorReturn valuesSupported Django versionsRunning the testsContributingThe licenseChange history for django-contextaware-processorsWhat it doesEver usedDjangoand wished you could have acontext processorwhich
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 usingTemplateResponseobjects and notrender()orrender_to_response().InstallationYou can usepipto install the0.1.1version fromPyPI:pip install django-contextaware-processors==0.1.1Or you can grab it fromGitHublike this:pip install -e git+https://github.com/kezabelle/django-contextaware-processors.git#egg=django-contextaware-processorsUsageAdd a newCONTEXTAWARE_PROCESSORSsetting to your project configuration. It
should be an iterable of strings representing the dotted paths to your
processors, just the same as theDjangocontext 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 bypath.to.my_processor.Using the middlewareIn most cases, if you’re usingTemplateResponseobjects (or anyClass
Based Viewwhich uses them for you), you want to use the provided
middleware:MIDDLEWARE_CLASSES = (
    # ...
    'contextaware_processors.middleware.ContextawareProcessors',
    # ...
)As this makes use ofprocess_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 inCONTEXTAWARE_PROCESSORSUsing the TemplateResponse subclassFor custom situations, there iscontext_processors.response.ContextawareTemplateResponseclass which
exposes anadd_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 theContextawareProcessorsmiddleware notices aContextawareTemplateResponseit
will add those defined inCONTEXTAWARE_PROCESSORSafterany previously
registered custom modifiers.Writing a context-aware processorThe API contract for a processor is the same as a normal context processor, but
with the addition of acontextparameter, sent as anamed-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 valuesA context-aware processor must return one of 3 things:
- Adictionaryto.update(...)the existing context with,
-NotImplementedmay be used to mark it as irrelevant for the request
- For convienience,Nonemay also be used to skip updating the context.Supported Django versionsThe tests are run againstDjango1.8 through 1.10, and Python 2.7, 3.3, 3.4 and 3.5.Running the testsIf you have a cloned copy, you can do:python setup.py testIf you have tox, you can just do:toxContributingPlease do!The project is hosted onGitHubin thekezabelle/django-contextaware-processorsrepository.Bug reports and feature requests can be filed on the repository’sissue tracker.If something can be discussed in 140 character chunks, there’s alsomy Twitter account.The licenseIt’sFreeBSD. There’s should be aLICENSEfile 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:Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.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-processors0.1.1Initial release