Skip to content

Commit

Permalink
Django 1.11 middleware compatibility fix (#42)
Browse files Browse the repository at this point in the history
* Use MiddlewareMixin for compatibility.

* Handle ImportError if Django version < 1.11.

* Remove var being overwritten by subsequent boolean expression.

* Update documentation to mention MIDDLEWARE and MIDDLEWARE_CLASSES may be used.
  • Loading branch information
okayjeff authored and kumar303 committed Feb 16, 2018
1 parent ebb9690 commit 84b96eb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
5 changes: 3 additions & 2 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ Make sure the module is installed as an app:
'hawkrest',
)
Make sure the middleware is installed:
Make sure the middleware is installed by adding it to your project's
``MIDDLEWARE`` or ``MIDDLEWARE_CLASSES`` (Django version < 1.11) setting:

.. code-block:: python
MIDDLEWARE_CLASSES = (
MIDDLEWARE = (
...
'hawkrest.middleware.HawkResponseMiddleware',
)
Expand Down
10 changes: 8 additions & 2 deletions hawkrest/middleware.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import logging

try:
from django.utils.deprecation import MiddlewareMixin
middleware_cls = MiddlewareMixin
except ImportError: # Django version < 1.11
middleware_cls = object


log = logging.getLogger(__name__)


class HawkResponseMiddleware:
class HawkResponseMiddleware(middleware_cls):

def process_response(self, request, response):
is_hawk_request = False
hawk_auth_was_processed = False
if request.META.get('HTTP_AUTHORIZATION', '').startswith('Hawk'):
is_hawk_request = True

Expand Down

0 comments on commit 84b96eb

Please sign in to comment.