def test_cache_all_samples(self): """Test the 'ALL_SAMPLES' list caches correctly.""" Sample.objects.create(name='mysample1', percent='100.0') # cache is initially empty cached_samples = cache.get(keyfmt(get_setting('ALL_SAMPLES_CACHE_KEY'))) self.assertIsNone(cached_samples) # calling waffejs view causes cache to be set self.client.get(reverse('wafflejs')) cached_samples = cache.get(keyfmt(get_setting('ALL_SAMPLES_CACHE_KEY'))) self.assertIsNotNone(cached_samples) self.assertSequenceEqual(['mysample1'], cached_samples) # creating a new flag invalidates the cache Sample.objects.create(name='mysample2', percent='100.0') cached_samples = cache.get(keyfmt(get_setting('ALL_SAMPLES_CACHE_KEY'))) self.assertIsNone(cached_samples) # calling waffejs view causes cache to be set for more than 1 switch self.client.get(reverse('wafflejs')) cached_samples = cache.get(keyfmt(get_setting('ALL_SAMPLES_CACHE_KEY'))) self.assertIsNotNone(cached_samples) self.assertSequenceEqual(['mysample1', 'mysample2'], cached_samples)
def _generate_waffle_js(request): flags = cache.get(keyfmt(FLAGS_ALL_CACHE_KEY)) if flags is None: flags = Flag.objects.values_list('name', flat=True) cache.add(keyfmt(FLAGS_ALL_CACHE_KEY), flags) flag_values = [(f, flag_is_active(request, f)) for f in flags] switches = cache.get(keyfmt(SWITCHES_ALL_CACHE_KEY)) if switches is None: switches = Switch.objects.values_list('name', 'active') cache.add(keyfmt(SWITCHES_ALL_CACHE_KEY), switches) samples = cache.get(keyfmt(SAMPLES_ALL_CACHE_KEY)) if samples is None: samples = Sample.objects.values_list('name', flat=True) cache.add(keyfmt(SAMPLES_ALL_CACHE_KEY), samples) sample_values = [(s, sample_is_active(s)) for s in samples] flag_default = getattr(settings, 'WAFFLE_FLAG_DEFAULT', False) switch_default = getattr(settings, 'WAFFLE_SWITCH_DEFAULT', False) sample_default = getattr(settings, 'WAFFLE_SAMPLE_DEFAULT', False) return loader.render_to_string('waffle/waffle.js', { 'flags': flag_values, 'switches': switches, 'samples': sample_values, 'flag_default': flag_default, 'switch_default': switch_default, 'sample_default': sample_default, })
def test_cache_all_flags(self): """Test the 'ALL_FLAGS' list caches correctly.""" Flag.objects.create(name='myflag1', everyone=True) # cache is initially empty cached_flags = cache.get(keyfmt(get_setting('ALL_FLAGS_CACHE_KEY'))) self.assertIsNone(cached_flags) # calling waffejs view causes cache to be set self.client.get(reverse('wafflejs')) cached_flags = cache.get(keyfmt(get_setting('ALL_FLAGS_CACHE_KEY'))) self.assertIsNotNone(cached_flags) self.assertSequenceEqual(['myflag1'], cached_flags) # creating a new flag invalidates the cache Flag.objects.create(name='myflag2', everyone=True) cached_flags = cache.get(keyfmt(get_setting('ALL_FLAGS_CACHE_KEY'))) self.assertIsNone(cached_flags) # calling waffejs view causes cache to be set for more than 2 flags self.client.get(reverse('wafflejs')) cached_flags = cache.get(keyfmt(get_setting('ALL_FLAGS_CACHE_KEY'))) self.assertIsNotNone(cached_flags) self.assertSequenceEqual(['myflag1', 'myflag2'], cached_flags)
def test_cache_all_switches(self): """Test the 'ALL_SWITCHES' list caches correctly.""" Switch.objects.create(name='myswitch1', active=True) # cache is initially empty cached_switches = cache.get(keyfmt(get_setting('ALL_SWITCHES_CACHE_KEY'))) self.assertIsNone(cached_switches) # calling waffejs view causes cache to be set self.client.get(reverse('wafflejs')) cached_switches = cache.get(keyfmt(get_setting('ALL_SWITCHES_CACHE_KEY'))) self.assertIsNotNone(cached_switches) self.assertSequenceEqual([('myswitch1', True)], cached_switches) # creating a new flag invalidates the cache Switch.objects.create(name='myswitch2', active=True) cached_switches = cache.get(keyfmt(get_setting('ALL_SWITCHES_CACHE_KEY'))) self.assertIsNone(cached_switches) # calling waffejs view causes cache to be set for more than 1 switch self.client.get(reverse('wafflejs')) cached_switches = cache.get(keyfmt(get_setting('ALL_SWITCHES_CACHE_KEY'))) self.assertIsNotNone(cached_switches) self.assertSequenceEqual([('myswitch1', True), ('myswitch2', True)], cached_switches)
def _flag_is_active_for_user(flag, user=None, **kwargs): if flag.staff and user.is_staff: return True if flag.superusers and user.is_superuser: return True flag_users = cache.get(keyfmt(get_setting('FLAG_USERS_CACHE_KEY'), flag.name)) if flag_users is None: flag_users = flag.users.all() cache_flag(instance=flag) if user in flag_users: return True flag_groups = cache.get(keyfmt(get_setting('FLAG_GROUPS_CACHE_KEY'), flag.name)) if flag_groups is None: flag_groups = flag.groups.all() cache_flag(instance=flag) user_groups = user.groups.all() for group in flag_groups: if group in user_groups: return True
def _generate_waffle_js(request): flags = cache.get(keyfmt(get_setting('ALL_FLAGS_CACHE_KEY'))) if flags is None: flags = Flag.objects.values_list('name', flat=True) cache.set(keyfmt(get_setting('ALL_FLAGS_CACHE_KEY')), flags) flag_values = [(f, flag_is_active(request, f)) for f in flags] switches = cache.get(keyfmt(get_setting('ALL_SWITCHES_CACHE_KEY'))) if switches is None: switches = Switch.objects.values_list('name', 'active') cache.set(keyfmt(get_setting('ALL_SWITCHES_CACHE_KEY')), switches) samples = cache.get(keyfmt(get_setting('ALL_SAMPLES_CACHE_KEY'))) if samples is None: samples = Sample.objects.values_list('name', flat=True) cache.set(keyfmt(get_setting('ALL_SAMPLES_CACHE_KEY')), samples) sample_values = [(s, sample_is_active(s)) for s in samples] return loader.render_to_string('waffle/waffle.js', { 'flags': flag_values, 'switches': switches, 'samples': sample_values, 'flag_default': get_setting('FLAG_DEFAULT'), 'switch_default': get_setting('SWITCH_DEFAULT'), 'sample_default': get_setting('SAMPLE_DEFAULT'), })
def _generate_waffle_js(request): flags = cache.get(keyfmt(get_setting('ALL_FLAGS_CACHE_KEY'))) if flags is None: flags = Flag.objects.values_list('name', flat=True) cache.add(keyfmt(get_setting('ALL_FLAGS_CACHE_KEY')), flags) flag_values = [(f, flag_is_active(request, f)) for f in flags] switches = cache.get(keyfmt(get_setting('ALL_SWITCHES_CACHE_KEY'))) if switches is None: switches = Switch.objects.values_list('name', 'active') cache.add(keyfmt(get_setting('ALL_SWITCHES_CACHE_KEY')), switches) samples = cache.get(keyfmt(get_setting('ALL_SAMPLES_CACHE_KEY'))) if samples is None: samples = Sample.objects.values_list('name', flat=True) cache.add(keyfmt(get_setting('ALL_SAMPLES_CACHE_KEY')), samples) sample_values = [(s, sample_is_active(s)) for s in samples] return loader.render_to_string( 'waffle/waffle.js', { 'flags': flag_values, 'switches': switches, 'samples': sample_values, 'flag_default': get_setting('FLAG_DEFAULT'), 'switch_default': get_setting('SWITCH_DEFAULT'), 'sample_default': get_setting('SAMPLE_DEFAULT'), })
def _get_flag(flag_name): flag = cache.get(keyfmt(get_setting('FLAG_CACHE_KEY'), flag_name)) if flag is None: try: flag = Flag.objects.get(name=flag_name) cache_flag(instance=flag) except Flag.DoesNotExist: flag = None return flag
def sample_is_active(sample_name): sample = cache.get(keyfmt(SAMPLE_CACHE_KEY, sample_name)) if sample is None: try: sample = Sample.objects.get(name=sample_name) cache_sample(instance=sample) except Sample.DoesNotExist: return getattr(settings, 'WAFFLE_SAMPLE_DEFAULT', False) return Decimal(str(random.uniform(0, 100))) <= sample.percent
def switch_is_active(switch_name): switch = cache.get(keyfmt(SWITCH_CACHE_KEY, switch_name)) if switch is None: try: switch = Switch.objects.get(name=switch_name) cache_switch(instance=switch) except Switch.DoesNotExist: switch = DoesNotExist() switch.name = switch_name cache_switch(instance=switch) return switch.active
def sample_is_active(sample_name): from .models import cache_sample, Sample from .compat import cache sample = cache.get(keyfmt(get_setting('SAMPLE_CACHE_KEY'), sample_name)) if sample is None: try: sample = Sample.objects.get(name=sample_name) cache_sample(instance=sample) except Sample.DoesNotExist: return get_setting('SAMPLE_DEFAULT') return Decimal(str(random.uniform(0, 100))) <= sample.percent
def switch_is_active(switch_name): from .models import cache_switch, Switch from .compat import cache switch = cache.get(keyfmt(get_setting('SWITCH_CACHE_KEY'), switch_name)) if switch is None: try: switch = Switch.objects.get(name=switch_name) cache_switch(instance=switch) except Switch.DoesNotExist: switch = DoesNotExist() switch.name = switch_name cache_switch(instance=switch) return switch.active
def flag_is_active(request, flag_name): flag = cache.get(keyfmt(FLAG_CACHE_KEY, flag_name)) if flag is None: try: flag = Flag.objects.get(name=flag_name) cache_flag(instance=flag) except Flag.DoesNotExist: return getattr(settings, 'WAFFLE_FLAG_DEFAULT', False) if getattr(settings, 'WAFFLE_OVERRIDE', False): if flag_name in request.GET: return request.GET[flag_name] == '1' if flag.everyone: return True elif flag.everyone is False: return False if flag.testing: # Testing mode is on. tc = TEST_COOKIE_NAME % 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 on if tc in request.COOKIES: return request.COOKIES[tc] == 'True' user = request.user if flag.authenticated and user.is_authenticated(): return True if flag.staff and user.is_staff: return True if flag.superusers and user.is_superuser: return True if flag.languages: languages = flag.languages.split(',') if (hasattr(request, 'LANGUAGE_CODE') and request.LANGUAGE_CODE in languages): return True flag_users = cache.get(keyfmt(FLAG_USERS_CACHE_KEY, flag.name)) if flag_users is None: flag_users = flag.users.all() cache_flag(instance=flag) if user in flag_users: return True flag_groups = cache.get(keyfmt(FLAG_GROUPS_CACHE_KEY, flag.name)) if flag_groups is None: flag_groups = flag.groups.all() cache_flag(instance=flag) user_groups = user.groups.all() for group in flag_groups: if group in user_groups: return True if flag.percent and flag.percent > 0: if not hasattr(request, 'waffles'): request.waffles = {} elif flag_name in request.waffles: return request.waffles[flag_name][0] cookie = COOKIE_NAME % flag_name if cookie in request.COOKIES: flag_active = (request.COOKIES[cookie] == 'True') set_flag(request, flag_name, flag_active, flag.rollout) return flag_active if Decimal(str(random.uniform(0, 100))) <= flag.percent: set_flag(request, flag_name, True, flag.rollout) return True set_flag(request, flag_name, False, flag.rollout) return False