Exemple #1
0
def uncache_flag(**kwargs):
    flag = kwargs.get('instance')
    data = {
        keyfmt(get_setting('FLAG_CACHE_KEY'), flag.name): None,
        keyfmt(get_setting('FLAG_USERS_CACHE_KEY'), flag.name): None,
        keyfmt(get_setting('FLAG_GROUPS_CACHE_KEY'), flag.name): None,
        keyfmt(get_setting('ALL_FLAGS_CACHE_KEY')): None
    }
    cache.set_many(data, 5)
Exemple #2
0
def cache_flag(**kwargs):
    action = kwargs.get('action', None)
    # action is included for m2m_changed signal. Only cache on the post_*.
    if not action or action in ['post_add', 'post_remove', 'post_clear']:
        f = kwargs.get('instance')
        cache.add(keyfmt(get_setting('FLAG_CACHE_KEY'), f.name), f)
        cache.add(keyfmt(get_setting('FLAG_USERS_CACHE_KEY'), f.name),
                  f.users.all())
        cache.add(keyfmt(get_setting('FLAG_GROUPS_CACHE_KEY'), f.name),
                  f.groups.all())
Exemple #3
0
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'),
    })
Exemple #4
0
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
Exemple #5
0
    def process_response(self, request, response):
        secure = get_setting('SECURE')
        max_age = get_setting('MAX_AGE')

        if hasattr(request, 'waffles'):
            for k in request.waffles:
                name = smart_str(get_setting('COOKIE') % k)
                active, rollout = request.waffles[k]
                if rollout and not active:
                    # "Inactive" is a session cookie during rollout mode.
                    age = None
                else:
                    age = max_age
                response.set_cookie(name, value=active, max_age=age,
                                    secure=secure)
        if hasattr(request, 'waffle_tests'):
            for k in request.waffle_tests:
                name = smart_str(get_setting('TEST_COOKIE') % k)
                value = request.waffle_tests[k]
                response.set_cookie(name, value=value)

        return response
Exemple #6
0
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
Exemple #7
0
 def test_override_settings(self):
     assert not get_setting('OVERRIDE')
     with override_settings(WAFFLE_OVERRIDE=True):
         assert get_setting('OVERRIDE')
Exemple #8
0
 def test_default_setting(self):
     age = get_setting('MAX_AGE')
     self.assertEqual(defaults.MAX_AGE, age)
Exemple #9
0
 def test_overridden_setting(self):
     prefix = get_setting('CACHE_PREFIX')
     self.assertEqual(settings.WAFFLE_CACHE_PREFIX, prefix)
Exemple #10
0
def uncache_switch(**kwargs):
    switch = kwargs.get('instance')
    cache.set(keyfmt(get_setting('SWITCH_CACHE_KEY'), switch.name), None, 5)
    cache.set(keyfmt(get_setting('ALL_SWITCHES_CACHE_KEY')), None, 5)
Exemple #11
0
def cache_switch(**kwargs):
    switch = kwargs.get('instance')
    cache.add(keyfmt(get_setting('SWITCH_CACHE_KEY'), switch.name), switch)
Exemple #12
0
def uncache_sample(**kwargs):
    sample = kwargs.get('instance')
    cache.set(keyfmt(get_setting('SAMPLE_CACHE_KEY'), sample.name), None, 5)
    cache.set(keyfmt(get_setting('ALL_SAMPLES_CACHE_KEY')), None, 5)
Exemple #13
0
def cache_sample(**kwargs):
    sample = kwargs.get('instance')
    cache.add(keyfmt(get_setting('SAMPLE_CACHE_KEY'), sample.name), sample)
Exemple #14
0
 def active(self):
     return get_setting("SWITCH_DEFAULT")
Exemple #15
0
def flag_is_active_from_database(request, flag_name):
    """
    A regular waffle.
    """
    from .models import cache_flag, Flag
    from .compat import cache

    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:
            return get_setting("FLAG_DEFAULT")

    if get_setting("OVERRIDE"):
        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 = 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 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(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

    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 = get_setting("COOKIE") % 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