Forked from sidmitra/add_user_info_to_request_middleware.py
Last active
February 28, 2019 12:26
-
-
Save benjaoming/ee0baa307dd41a8431b008a299bfc86c to your computer and use it in GitHub Desktop.
Middleware to add user info to the current django request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ExceptionUserInfoMiddleware(object): | |
""" | |
Adds user details to request context on receiving an exception, so that they show up in the error emails. | |
Add to settings.MIDDLEWARE_CLASSES and keep it outermost(i.e. on top if possible). This allows | |
it to catch exceptions in other middlewares as well. | |
Origin: https://gist.github.com/646372 | |
""" | |
def process_exception(self, request, exception): | |
""" | |
Process the exception. | |
:Parameters: | |
- `request`: request that caused the exception | |
- `exception`: actual exception being raised | |
""" | |
try: | |
if request.user.is_authenticated(): | |
request.META['USERNAME'] = str(request.user.username) | |
request.META['USER_EMAIL'] = str(request.user.email) | |
else: | |
request.META['USERNAME'] = 'anonymous' | |
except: | |
pass | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment