bornhack-website/src/utils/middleware.py
Thomas Steen Rasmussen 33383e6559
Event feedback (#451)
* Event feedback functionality and related commits:

* blackness and isort and flake8 - this branch does not have pre-commit so I forgot :/

* finish backoffice management of eventfeedback

* add username to eventfeedback detail panel when viewed in backoffice

* Add feedback url to elm schedule. Fix access when user is anonymous. Remove print statement.

* one prefetch_related call to rule them all

Co-authored-by: Víðir Valberg Guðmundsson <valberg@orn.li>
2020-02-22 14:50:09 +01:00

31 lines
824 B
Python

from django.shortcuts import redirect
class RedirectException(Exception):
"""
An exception class meant to be used to redirect from places where
we cannot just return a HTTPResponse directly (like view setup() methods)
"""
def __init__(self, url):
self.url = url
class RedirectExceptionMiddleware:
"""
A simple middleware to catch exceptions of type RedirectException
and redirect to the url
"""
def __init__(self, get_response):
self.get_response = get_response
def process_exception(self, request, exception):
if isinstance(exception, RedirectException):
if hasattr(exception, "url"):
return redirect(exception.url)
def __call__(self, request):
response = self.get_response(request)
return response