Skip to content

Commit

Permalink
Merge pull request #8 from caltechads/master
Browse files Browse the repository at this point in the history
Removed process_exception(). Improved docs.
Fixes #7
  • Loading branch information
Alir3z4 authored May 2, 2018
2 parents 206e690 + 5d7ba1e commit cb605b8
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 20 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ answer newbie questions, and generally made ``django-crequest`` that much better

* Alireza Savand <[email protected]>
* Richard Royal <[email protected]>
* Robert Rollins <[email protected]>

A big THANK YOU goes to:

Expand Down
2 changes: 1 addition & 1 deletion crequest/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = (2016, 3, 16)
__version__ = '2018.5.1'
30 changes: 13 additions & 17 deletions crequest/middleware.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import threading

try:
from django.utils.deprecation import MiddlewareMixin
except ImportError:
Expand All @@ -8,46 +7,43 @@

class CrequestMiddleware(MiddlewareMixin):
"""
Always have access to the current request
Provides storage for the "current" request object, so that code anywhere
in your project can access it, without it having to be passed to that code
from the view.
"""
_request = {}
_requests = {}

def process_request(self, request):
"""
Store request
Store the current request.
"""
self.__class__.set_request(request)

def process_response(self, request, response):
"""
Delete request
Delete the current request to avoid leaking memory.
"""
self.__class__.del_request()
return response

def process_exception(self, request, exception):
"""
Delete request
"""
self.__class__.del_request()

@classmethod
def get_request(cls, default=None):
"""
Retrieve request
Retrieve the request object for the current thread, or the optionally
provided default if there is no current request.
"""
return cls._request.get(threading.current_thread(), default)
return cls._requests.get(threading.current_thread(), default)

@classmethod
def set_request(cls, request):
"""
Store request
Save the given request into storage for the current thread.
"""
cls._request[threading.current_thread()] = request
cls._requests[threading.current_thread()] = request

@classmethod
def del_request(cls):
"""
Delete request
Delete the request that was stored for the current thread.
"""
cls._request.pop(threading.current_thread(), None)
cls._requests.pop(threading.current_thread(), None)
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

setup(
name='django-crequest',
version=".".join(map(str, __import__('crequest').__version__)),
description='Middleware to make current request always available.',
version=__import__('crequest').__version__,
description='Middleware that makes the current request available from anywhere.',
long_description=open('README.rst').read(),
license=open('LICENSE', encoding='utf-8').read(),
author='Alireza Savand',
Expand Down

0 comments on commit cb605b8

Please sign in to comment.