diff --git a/waffle/middleware.py b/waffle/middleware.py index ec31b1a7..c7859d0c 100644 --- a/waffle/middleware.py +++ b/waffle/middleware.py @@ -1,11 +1,28 @@ from django.http import HttpRequest, HttpResponse from django.utils.deprecation import MiddlewareMixin from django.utils.encoding import smart_str - from waffle.utils import get_setting +from waffle import get_waffle_flag_model + +WaffleFlag = get_waffle_flag_model() class WaffleMiddleware(MiddlewareMixin): + + def __init__(self, get_response): + self.get_response = get_response + + def __call__(self, request): + """Ensure testing cookie are always set, even if Waffle isn't used""" + for flag in WaffleFlag.objects.filter(testing=True): + tc = get_setting("TEST_COOKIE") % flag.name + if tc in request.GET: + on = request.GET[tc] == "1" + if not hasattr(request, "waffle_tests"): + request.waffle_tests = {} + request.waffle_tests[flag.name] = on + return self.process_response(request, self.get_response(request)) + def process_response(self, request: HttpRequest, response: HttpResponse) -> HttpResponse: secure = get_setting('SECURE') max_age = get_setting('MAX_AGE')