コード例 #1
0
ファイル: models.py プロジェクト: fixr-app/django-waffle
 def get_flush_keys(self, flush_keys=None):
     flush_keys = super(AbstractUserFlag, self).get_flush_keys(flush_keys)
     flush_keys.extend([
         keyfmt(get_setting('FLAG_USERS_CACHE_KEY'), self.name),
         keyfmt(get_setting('FLAG_GROUPS_CACHE_KEY'), self.name),
     ])
     return flush_keys
コード例 #2
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('SWITCHES_SITES_CACHE_KEY'), switch.name), None, 5)

    cache.set(keyfmt(get_setting('ALL_SWITCHES_CACHE_KEY')), None, 5)
コード例 #3
0
ファイル: interface.py プロジェクト: revupsw/django-waffle
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
コード例 #4
0
ファイル: models.py プロジェクト: jsocol/django-waffle
 def get_flush_keys(self, flush_keys=None):
     flush_keys = super(AbstractUserFlag, self).get_flush_keys(flush_keys)
     flush_keys.extend([
         keyfmt(get_setting('FLAG_USERS_CACHE_KEY'), self.name),
         keyfmt(get_setting('FLAG_GROUPS_CACHE_KEY'), self.name),
     ])
     return flush_keys
コード例 #5
0
ファイル: models.py プロジェクト: zacharyvoase/django-waffle
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())
コード例 #6
0
ファイル: models.py プロジェクト: wsantos/django-waffle
 def flush(self):
     keys = [
         self._cache_key(self.name),
         keyfmt(get_setting('FLAG_USERS_CACHE_KEY'), self.name),
         keyfmt(get_setting('FLAG_GROUPS_CACHE_KEY'), self.name),
         get_setting('ALL_FLAGS_CACHE_KEY'),
     ]
     cache.delete_many(keys)
コード例 #7
0
ファイル: models.py プロジェクト: koliber/django-waffle
 def flush(self):
     keys = [
         self._cache_key(self.name),
         keyfmt(get_setting('FLAG_USERS_CACHE_KEY'), self.name),
         keyfmt(get_setting('FLAG_GROUPS_CACHE_KEY'), self.name),
         get_setting('ALL_FLAGS_CACHE_KEY'),
     ]
     cache.delete_many(keys)
コード例 #8
0
ファイル: models.py プロジェクト: spookylukey/django-waffle
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)
コード例 #9
0
ファイル: models.py プロジェクト: isotoma/django-waffle
def uncache_flag(**kwargs):
    flag = kwargs.get('instance')
    data = [
        keyfmt(get_setting('FLAG_CACHE_KEY'), flag.name, flag.site),
        keyfmt(get_setting('FLAG_USERS_CACHE_KEY'), flag.name, flag.site),
        keyfmt(get_setting('FLAG_GROUPS_CACHE_KEY'), flag.name, flag.site),
        keyfmt(get_setting('ALL_FLAGS_CACHE_KEY'))
    ]
    cache.delete_many(data)
コード例 #10
0
ファイル: models.py プロジェクト: batcave/django-waffle
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)
コード例 #11
0
ファイル: models.py プロジェクト: zacharyvoase/django-waffle
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)
コード例 #12
0
ファイル: models.py プロジェクト: isotoma/django-waffle
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.site), f)
        cache.add(keyfmt(get_setting('FLAG_USERS_CACHE_KEY'), f.name, f.site),
                  f.users.all())
        cache.add(keyfmt(get_setting('FLAG_GROUPS_CACHE_KEY'), f.name, f.site),
                  f.groups.all())
コード例 #13
0
ファイル: models.py プロジェクト: spookylukey/django-waffle
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())
コード例 #14
0
ファイル: __init__.py プロジェクト: isotoma/django-waffle
def switch_is_active(switch_name, current_site=None, site_is_none=False):
    """If current_site is not given, then use Site.objects.get_current(),
    unless site_is_none is True, then look for a switch with no site.

    """
    from .models import cache_switch, Switch
    from .compat import cache

    if current_site is None:
        if site_is_none:
            current_site = None
        else:
            current_site = Site.objects.get_current()
    switch = cache.get(keyfmt(get_setting('SWITCH_CACHE_KEY'), switch_name, current_site))
    if switch is None:
        try:
            switch = Switch.objects.get(name=switch_name, site=current_site)
            cache_switch(instance=switch)
        except Switch.DoesNotExist:
            try:
                switch = Switch.objects.get(name=switch_name, site__isnull=True)
                cache_switch(instance=switch)
            except Switch.DoesNotExist:
                return get_setting('SWITCH_DEFAULT')
    return switch.active
コード例 #15
0
ファイル: __init__.py プロジェクト: isotoma/django-waffle
def sample_is_active(sample_name, current_site=None, site_is_none=None):
    """If current_site is not given, then use Site.objects.get_current(),
    unless site_is_none is True, then look for a sample with no site.

    """
    from .models import cache_sample, Sample
    from .compat import cache

    if current_site is None:
        if site_is_none:
            current_site = None
        else:
            current_site = Site.objects.get_current()
    sample = cache.get(keyfmt(get_setting('SAMPLE_CACHE_KEY'), sample_name, current_site))
    if sample is None:
        try:
            sample = Sample.objects.get(name=sample_name, site=current_site)
            cache_sample(instance=sample)
        except Sample.DoesNotExist:
            try:
                sample = Sample.objects.get(name=sample_name, site__isnull=True)
                cache_sample(instance=sample)
            except Sample.DoesNotExist:
                return get_setting('SAMPLE_DEFAULT')

    return Decimal(str(random.uniform(0, 100))) <= sample.percent
コード例 #16
0
 def get_flush_keys(self, flush_keys=None):
     flush_keys = super(CompanyAwareFlag, self).get_flush_keys(flush_keys)
     companies_cache_key = get_setting(
         CompanyAwareFlag.FLAG_COMPANIES_CACHE_KEY,
         CompanyAwareFlag.FLAG_COMPANIES_CACHE_KEY_DEFAULT)
     flush_keys.append(keyfmt(companies_cache_key, self.name))
     return flush_keys
コード例 #17
0
ファイル: __init__.py プロジェクト: slorg1/django-waffle
def flag_is_active_for_user(user, flag_name):
    """
        Returns True if the given flag_name is active for the given user, False otherwise
    """
    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 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

    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

    return False
コード例 #18
0
ファイル: __init__.py プロジェクト: slorg1/django-waffle
def flag_is_active(request, flag_name):
    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'

    if flag.languages:
        languages = flag.languages.split(',')
        if (hasattr(request, 'LANGUAGE_CODE') and
                request.LANGUAGE_CODE in languages):
            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 flag_is_active_for_user(request.user, flag_name)
コード例 #19
0
ファイル: interface.py プロジェクト: revupsw/django-waffle
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
コード例 #20
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'),
        })
コード例 #21
0
ファイル: views.py プロジェクト: DjangoBD/django-waffle
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'),
    })
コード例 #22
0
ファイル: __init__.py プロジェクト: dkao1978/django-waffle
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
コード例 #23
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
コード例 #24
0
ファイル: __init__.py プロジェクト: dkao1978/django-waffle
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
コード例 #25
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
コード例 #26
0
ファイル: __init__.py プロジェクト: evilkost/django-waffle
def switch_is_active(request, switch_name):
    from .models import cache_switch, Switch
    from .compat import cache

    current_site = Site.objects.get_current(request)
    switch = cache.get(keyfmt(get_setting("SWITCH_CACHE_KEY"), switch_name, current_site))
    if switch is None:
        switch = Switch.objects.filter(name=switch_name).first()
        if switch is None:
            return get_setting("SWITCH_DEFAULT")

        cache_switch(instance=switch)

    return switch.active and current_site in switch.get_sites()
コード例 #27
0
ファイル: __init__.py プロジェクト: evilkost/django-waffle
def sample_is_active(request, sample_name):
    from .models import cache_sample, Sample
    from .compat import cache

    current_site = Site.objects.get_current(request)
    sample = cache.get(keyfmt(get_setting("SAMPLE_CACHE_KEY"), sample_name, current_site))
    if sample is None:
        sample = Sample.objects.filter(name=sample_name).first()
        if sample is None:
            return get_setting("SAMPLE_DEFAULT")

        cache_sample(instance=sample)

    return probe_a_sample(sample) and current_site in sample.get_sites()
コード例 #28
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))
    switch_sites = cache.get(keyfmt(get_setting('SWITCHES_SITES_CACHE_KEY'),
                                    switch_name))
    if switch is None:
        try:
            switch = Switch.objects.get(name=switch_name)
            switch_sites = switch.sites.all()
            cache_switch(instance=switch)
        except Switch.DoesNotExist:
            from django.contrib.sites.models import Site
            switch = DoesNotExist()
            switch.name = switch_name
            switch.sites = Site.objects.none()
            cache_switch(instance=switch)

    if switch_sites:
        from django.contrib.sites.models import Site
        site = Site.objects.get_current()
        return switch.active and site in switch_sites
    return switch.active
コード例 #29
0
ファイル: models.py プロジェクト: wsantos/django-waffle
    def _get_group_ids(self):
        cache_key = keyfmt(get_setting('FLAG_GROUPS_CACHE_KEY'), self.name)
        cached = cache.get(cache_key)
        if cached == CACHE_EMPTY:
            return set()
        if cached:
            return cached

        group_ids = set(self.groups.all().values_list('pk', flat=True))
        if not group_ids:
            cache.add(cache_key, CACHE_EMPTY)
            return set()

        cache.add(cache_key, group_ids)
        return group_ids
コード例 #30
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))
    sample_sites = cache.get(keyfmt(get_setting('SAMPLE_SITES_CACHE_KEY'),
                                    sample_name))
    if sample is None:
        try:
            sample = Sample.objects.get(name=sample_name)
            sample_sites = sample.sites.all()
            cache_sample(instance=sample)
        except Sample.DoesNotExist:
            return get_setting('SAMPLE_DEFAULT')

    if sample_sites:
        from django.contrib.sites.models import Site
        site = Site.objects.get_current()
        if site in sample_sites:
            return Decimal(str(random.uniform(0, 100))) <= sample.percent
        else:
            return False
    else:
        return Decimal(str(random.uniform(0, 100))) <= sample.percent
コード例 #31
0
    def _get_country_ids(self):
        cache_key = keyfmt(self.FLAG_COUNTRIES_CACHE_KEY, self.name)
        cached = cache.get(cache_key)
        if cached == CACHE_EMPTY:
            return set()
        if cached:
            return cached

        country_ids = set(self.countries.all().values_list('pk', flat=True))
        if not country_ids:
            cache.add(cache_key, CACHE_EMPTY)
            return set()

        cache.add(cache_key, country_ids)
        return country_ids
コード例 #32
0
ファイル: models.py プロジェクト: wsantos/django-waffle
    def _get_user_ids(self):
        cache_key = keyfmt(get_setting('FLAG_USERS_CACHE_KEY'), self.name)
        cached = cache.get(cache_key)
        if cached == CACHE_EMPTY:
            return set()
        if cached:
            return cached

        user_ids = set(self.users.all().values_list('pk', flat=True))
        if not user_ids:
            cache.add(cache_key, CACHE_EMPTY)
            return set()

        cache.add(cache_key, user_ids)
        return user_ids
コード例 #33
0
ファイル: models.py プロジェクト: unicef/etools
    def _get_group_ids(self):
        cache_key = keyfmt(get_setting('FLAG_GROUPS_CACHE_KEY'), self.name)
        cached = cache.get(cache_key)
        if cached == CACHE_EMPTY:
            return set()
        if cached:
            return cached

        group_ids = set(self.groups.all().values_list('pk', flat=True))
        if not group_ids:
            cache.add(cache_key, CACHE_EMPTY)
            return set()

        cache.add(cache_key, group_ids)
        return group_ids
コード例 #34
0
ファイル: models.py プロジェクト: unicef/etools
    def _get_country_ids(self):
        cache_key = keyfmt(self.FLAG_COUNTRIES_CACHE_KEY, self.name)
        cached = cache.get(cache_key)
        if cached == CACHE_EMPTY:
            return set()
        if cached:
            return cached

        country_ids = set(self.countries.all().values_list('pk', flat=True))
        if not country_ids:
            cache.add(cache_key, CACHE_EMPTY)
            return set()

        cache.add(cache_key, country_ids)
        return country_ids
コード例 #35
0
ファイル: models.py プロジェクト: unicef/etools
    def _get_user_ids(self):
        cache_key = keyfmt(get_setting('FLAG_USERS_CACHE_KEY'), self.name)
        cached = cache.get(cache_key)
        if cached == CACHE_EMPTY:
            return set()
        if cached:
            return cached

        user_ids = set(self.users.all().values_list('pk', flat=True))
        if not user_ids:
            cache.add(cache_key, CACHE_EMPTY)
            return set()

        cache.add(cache_key, user_ids)
        return user_ids
コード例 #36
0
ファイル: __init__.py プロジェクト: webus/django-waffle
def switch_is_active(request, switch_name):
    from .models import cache_switch, Switch
    from .compat import cache

    current_site = Site.objects.get_current(request)
    switch = cache.get(keyfmt(get_setting('SWITCH_CACHE_KEY'), switch_name, current_site))
    if switch is None:
        try:
            switch = Switch.objects.get(name=switch_name, site=current_site)
            cache_switch(instance=switch)
        except Switch.DoesNotExist:
            try:
                switch = Switch.objects.get(name=switch_name, site__isnull=True)
                cache_switch(instance=switch)
            except Switch.DoesNotExist:
                return get_setting('SWITCH_DEFAULT')
    return switch.active
コード例 #37
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:
        if get_setting('SWITCH_AUTOCREATE', False):
            defaults = get_setting('SWITCH_DEFAULTS', {})
            switch, created = Switch.objects.get_or_create(
                name=switch_name, defaults=defaults.get(switch_name, {}))
        else:
            try:
                switch = Switch.objects.get(name=switch_name)
            except Switch.DoesNotExist:
                switch = DoesNotExist()
                switch.name = switch_name
        cache_switch(instance=switch)
    return switch.active
コード例 #38
0
    def _get_pricing_ids(self):
        cache_key = keyfmt(
            get_setting(Flag.FLAG_PRICINGS_CACHE_KEY,
                        Flag.FLAG_PRICINGS_CACHE_KEY_DEFAULT), self.name)
        cached = cache.get(cache_key)
        if cached == CACHE_EMPTY:
            return set()
        if cached:
            return cached

        pricing_ids = set(self.pricings.all().values_list('pk', flat=True))
        if not pricing_ids:
            cache.add(cache_key, CACHE_EMPTY)
            logger.warning('Feature flag does not have assigned subscriptions')
            return set()

        cache.add(cache_key, pricing_ids)
        return pricing_ids
コード例 #39
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:
        if get_setting('SAMPLE_AUTOCREATE', False):
            defaults = get_setting('SAMPLE_DEFAULTS', {})
            sample, created = Sample.objects.get_or_create(name=sample_name,
                    defaults=defaults.get(sample_name, {}))
        else:
            try:
                sample = Sample.objects.get(name=sample_name)
            except Sample.DoesNotExist:
                return get_setting('SAMPLE_DEFAULT', False)
        cache_sample(instance=sample)

    return Decimal(str(random.uniform(0, 100))) <= sample.percent
コード例 #40
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:
        if get_setting('SAMPLE_AUTOCREATE', False):
            defaults = get_setting('SAMPLE_DEFAULTS', {})
            sample, created = Sample.objects.get_or_create(
                name=sample_name, defaults=defaults.get(sample_name, {}))
        else:
            try:
                sample = Sample.objects.get(name=sample_name)
            except Sample.DoesNotExist:
                return get_setting('SAMPLE_DEFAULT', False)
        cache_sample(instance=sample)

    return Decimal(str(random.uniform(0, 100))) <= sample.percent
コード例 #41
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:
        if get_setting('SWITCH_AUTOCREATE', False):
            defaults = get_setting('SWITCH_DEFAULTS', {})
            switch, created = Switch.objects.get_or_create(name=switch_name,
                    defaults=defaults.get(switch_name, {}))
        else:
            try:
                switch = Switch.objects.get(name=switch_name)
            except Switch.DoesNotExist:
                switch = DoesNotExist()
                switch.name = switch_name
        cache_switch(instance=switch)
    return switch.active
コード例 #42
0
ファイル: models.py プロジェクト: jsocol/django-waffle
    def _get_company_ids(self):
        cache_key = keyfmt(
            get_setting(CompanyAwareFlag.FLAG_COMPANIES_CACHE_KEY, CompanyAwareFlag.FLAG_COMPANIES_CACHE_KEY_DEFAULT),
            self.name
        )
        cached = cache.get(cache_key)
        if cached == CACHE_EMPTY:
            return set()
        if cached:
            return cached

        company_ids = set(self.companies.all().values_list('pk', flat=True))
        if not company_ids:
            cache.add(cache_key, CACHE_EMPTY)
            return set()

        cache.add(cache_key, company_ids)
        return company_ids
コード例 #43
0
    def _get_company_ids(self):
        cache_key = keyfmt(
            get_setting(CompanyAwareFlag.FLAG_COMPANIES_CACHE_KEY,
                        CompanyAwareFlag.FLAG_COMPANIES_CACHE_KEY_DEFAULT),
            self.name)
        cached = cache.get(cache_key)
        if cached == CACHE_EMPTY:
            return set()
        if cached:
            return cached

        company_ids = set(self.companies.all().values_list('pk', flat=True))
        if not company_ids:
            cache.add(cache_key, CACHE_EMPTY)
            return set()

        cache.add(cache_key, company_ids)
        return company_ids
コード例 #44
0
ファイル: models.py プロジェクト: spookylukey/django-waffle
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)
コード例 #45
0
ファイル: __init__.py プロジェクト: dkao1978/django-waffle
def flag_is_active(request, flag_name):
    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

    store = request.store
    flag_stores = flag.stores.all()
    if store in flag_stores:
        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
コード例 #46
0
ファイル: models.py プロジェクト: spookylukey/django-waffle
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)
コード例 #47
0
ファイル: models.py プロジェクト: spookylukey/django-waffle
def cache_sample(**kwargs):
    sample = kwargs.get('instance')
    cache.add(keyfmt(get_setting('SAMPLE_CACHE_KEY'), sample.name), sample)
コード例 #48
0
def flag_is_active(request, flag_name):
    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
コード例 #49
0
ファイル: models.py プロジェクト: spookylukey/django-waffle
def cache_switch(**kwargs):
    switch = kwargs.get('instance')
    cache.add(keyfmt(get_setting('SWITCH_CACHE_KEY'), switch.name), switch)
コード例 #50
0
ファイル: models.py プロジェクト: koliber/django-waffle
 def _cache_key(cls, name):
     return keyfmt(get_setting(cls.SINGLE_CACHE_KEY), name)
コード例 #51
0
ファイル: models.py プロジェクト: wsantos/django-waffle
 def _cache_key(cls, name):
     return keyfmt(get_setting(cls.SINGLE_CACHE_KEY), name)
コード例 #52
0
 def get_flush_keys(self, flush_keys=None):
     flush_keys = super(Flag, self).get_flush_keys(flush_keys)
     pricings_cache_key = get_setting(Flag.FLAG_PRICINGS_CACHE_KEY,
                                      Flag.FLAG_PRICINGS_CACHE_KEY_DEFAULT)
     flush_keys.append(keyfmt(pricings_cache_key, self.name))
     return flush_keys