Skip to content

Instantly share code, notes, and snippets.

@stefanfoulis
Created January 27, 2014 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stefanfoulis/8654943 to your computer and use it in GitHub Desktop.
Save stefanfoulis/8654943 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
from waffle import TEST_COOKIE_NAME, flag_is_active
from waffle.middleware import WaffleMiddleware
# FIXME: only works if TEST_COOKIE_NAME ends with the flag name
TEST_COOKIE_PREFIX = TEST_COOKIE_NAME.split('%s')[0]
class AlwaysWaffleMiddleware(WaffleMiddleware):
"""
version of the waffle middle ware that will set the cookie any time the waffle
get parameters are in the url. not just on views where the feature flag actually
happens do be checked.
"""
def process_response(self, request, response):
# sets cookies for any flags in the get parameters
for key, value in request.GET.items():
if key.startswith(TEST_COOKIE_PREFIX):
flag_name = key.replace(TEST_COOKIE_PREFIX, '', 1)
# implicitly sets the cookie and adds it to request
flag_is_active(request, flag_name)
return super(BetterWaffleMiddleware, self).process_response(request, response)
@benje
Copy link

benje commented Aug 13, 2015

Hey @stefanfloulis - I know this gist is a few years old but seems to be breaking on latest version of waffle (even though It would still be really useful!).

from waffle import TEST_COOKIE_NAME, flag_is_active
ImportError: cannot import name TEST_COOKIE_NAME

I think that static name might have changed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment