Skip to content

Instantly share code, notes, and snippets.

@philippeowagner
Forked from stefanfoulis/waffle_middleware.py
Last active August 29, 2015 14:14
Show Gist options
  • Save philippeowagner/765a585fb2ce14bf9c99 to your computer and use it in GitHub Desktop.
Save philippeowagner/765a585fb2ce14bf9c99 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment