Skip to content

Commit

Permalink
Always set testing cookies, even on pages which do not use them
Browse files Browse the repository at this point in the history
Addresses #80

Uses code from OpenHumans/open-humans#1109
  • Loading branch information
dragon-dxw committed May 3, 2024
1 parent 3d44a4b commit 8f16a63
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion waffle/middleware.py
Original file line number Diff line number Diff line change
@@ -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')
Expand Down

0 comments on commit 8f16a63

Please sign in to comment.