2 from django.conf import settings
5 def experiments_middleware(get_response):
6 def middleware(request):
9 overrides = getattr(settings, 'EXPERIMENTS_OVERRIDES', {})
10 for exp in settings.EXPERIMENTS:
13 exps[slug] = overrides[slug]
16 cookie_value = request.COOKIES.get(f'EXPERIMENT_{slug}')
17 if cookie_value is not None:
18 for cohort in exp.get('cohorts', []):
19 if cohort['value'] == cookie_value:
20 exps[slug] = cookie_value
27 (slug + request.META['REMOTE_ADDR']).encode('utf-8')
31 for cohort in exp.get('cohorts', []):
32 number -= cohort.get('size', 1)
34 exps[slug] = cohort['value']
37 request.EXPERIMENTS = exps
38 response = get_response(request)