Ejemplo n.º 1
0
 def flush(self):
     cache = get_cache()
     keys = [
         self._cache_key(self.name),
         get_setting(self.ALL_CACHE_KEY),
     ]
     cache.delete_many(keys)
Ejemplo n.º 2
0
 def flush(self):
     cache = get_cache()
     keys = [
         self._cache_key(self.name),
         get_setting(self.ALL_CACHE_KEY),
     ]
     cache.delete_many(keys)
Ejemplo n.º 3
0
    def is_active(self, request):
        if not self.pk:
            log_level = get_setting('LOG_MISSING_FLAGS')
            if log_level:
                logger.log(log_level, 'Flag %s not found', self.name)
            if get_setting('CREATE_MISSING_FLAGS'):
                flag, _created = get_waffle_flag_model().objects.get_or_create(
                    name=self.name,
                    defaults={'everyone': get_setting('FLAG_DEFAULT')})
                cache = get_cache()
                cache.add(self._cache_key(self.name), flag)

            return get_setting('FLAG_DEFAULT')

        if get_setting('OVERRIDE'):
            if self.name in request.GET:
                return request.GET[self.name] == '1'

        if self.everyone:
            return True
        elif self.everyone is False:
            return False

        if self.testing:  # Testing mode is on.
            tc = get_setting('TEST_COOKIE') % self.name
            if tc in request.GET:
                on = request.GET[tc] == '1'
                if not hasattr(request, 'waffle_tests'):
                    request.waffle_tests = {}
                request.waffle_tests[self.name] = on
                return on
            if tc in request.COOKIES:
                return request.COOKIES[tc] == 'True'

        active_for_language = self._is_active_for_language(request)
        if active_for_language is not None:
            return active_for_language

        active_for_user = self._is_active_for_user(request)
        if active_for_user is not None:
            return active_for_user

        if self.percent and self.percent > 0:
            if not hasattr(request, 'waffles'):
                request.waffles = {}
            elif self.name in request.waffles:
                return request.waffles[self.name][0]

            cookie = get_setting('COOKIE') % self.name
            if cookie in request.COOKIES:
                flag_active = (request.COOKIES[cookie] == 'True')
                set_flag(request, self.name, flag_active, self.rollout)
                return flag_active

            if Decimal(str(random.uniform(0, 100))) <= self.percent:
                set_flag(request, self.name, True, self.rollout)
                return True
            set_flag(request, self.name, False, self.rollout)

        return False
Ejemplo n.º 4
0
    def is_active(self):
        if not self.pk:
            log_level = get_setting('LOG_MISSING_SWITCHES')
            if log_level:
                logger.log(log_level, 'Switch %s not found', self.name)
            if get_setting('CREATE_MISSING_SWITCHES'):
                switch, _created = Switch.objects.get_or_create(
                    name=self.name,
                    defaults={'active': get_setting('SWITCH_DEFAULT')})
                cache = get_cache()
                cache.set(self._cache_key(self.name), switch)

            return get_setting('SWITCH_DEFAULT')

        return self.active
Ejemplo n.º 5
0
    def get_all(cls):
        cache = get_cache()
        cache_key = get_setting(cls.ALL_CACHE_KEY)
        cached = cache.get(cache_key)
        if cached == CACHE_EMPTY:
            return []
        if cached:
            return cached

        objs = cls.get_all_from_db()
        if not objs:
            cache.add(cache_key, CACHE_EMPTY)
            return []

        cache.add(cache_key, objs)
        return objs
Ejemplo n.º 6
0
    def _get_group_ids(self):
        cache = get_cache()
        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
Ejemplo n.º 7
0
    def _get_user_ids(self):
        cache = get_cache()
        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
Ejemplo n.º 8
0
    def is_active(self):
        if not self.pk:
            log_level = get_setting('LOG_MISSING_SAMPLES')
            if log_level:
                logger.log(log_level, 'Sample %s not found', self.name)
            if get_setting('CREATE_MISSING_SAMPLES'):

                default_percent = 100 if get_setting('SAMPLE_DEFAULT') else 0

                sample, _created = Sample.objects.get_or_create(
                    name=self.name, defaults={'percent': default_percent})
                cache = get_cache()
                cache.set(self._cache_key(self.name), sample)

            return get_setting('SAMPLE_DEFAULT')
        return Decimal(str(random.uniform(0, 100))) <= self.percent
Ejemplo n.º 9
0
    def _get_group_ids(self):
        cache = get_cache()
        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
Ejemplo n.º 10
0
    def get_all(cls):
        cache = get_cache()
        cache_key = get_setting(cls.ALL_CACHE_KEY)
        cached = cache.get(cache_key)
        if cached == CACHE_EMPTY:
            return []
        if cached:
            return cached

        objs = cls.get_all_from_db()
        if not objs:
            cache.add(cache_key, CACHE_EMPTY)
            return []

        cache.add(cache_key, objs)
        return objs
Ejemplo n.º 11
0
    def _get_user_ids(self):
        cache = get_cache()
        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
Ejemplo n.º 12
0
    def get(cls, name):
        cache = get_cache()
        cache_key = cls._cache_key(name)
        cached = cache.get(cache_key)
        if cached == CACHE_EMPTY:
            return cls(name=name)
        if cached:
            return cached

        try:
            obj = cls.get_from_db(name)
        except cls.DoesNotExist:
            cache.add(cache_key, CACHE_EMPTY)
            return cls(name=name)

        cache.add(cache_key, obj)
        return obj
Ejemplo n.º 13
0
    def get(cls, name):
        cache = get_cache()
        cache_key = cls._cache_key(name)
        cached = cache.get(cache_key)
        if cached == CACHE_EMPTY:
            return cls(name=name)
        if cached:
            return cached

        try:
            obj = cls.get_from_db(name)
        except cls.DoesNotExist:
            cache.add(cache_key, CACHE_EMPTY)
            return cls(name=name)

        cache.add(cache_key, obj)
        return obj
Ejemplo n.º 14
0
    def get(cls, name):
        cache = get_cache()
        cache_key = cls._cache_key(name)
        cached = cache.get(cache_key)
        if cached == CACHE_EMPTY:
            logger.warning("%s: %s does not exist", cls.__name__, name)
            return cls(name=name)
        if cached:
            return cached

        try:
            obj = cls.get_from_db(name)
        except cls.DoesNotExist:
            cache.add(cache_key, CACHE_EMPTY)
            return cls(name=name)

        cache.add(cache_key, obj)
        return obj
Ejemplo n.º 15
0
from __future__ import absolute_import, unicode_literals

from django.db import models

from waffle.utils import get_setting, get_cache


cache = get_cache()


class BaseManager(models.Manager):
    KEY_SETTING = ''

    def get_by_natural_key(self, name):
        return self.get(name=name)

    def create(self, *args, **kwargs):
        ret = super(BaseManager, self).create(*args, **kwargs)
        cache_key = get_setting(self.KEY_SETTING)
        cache.delete(cache_key)
        return ret


class FlagManager(BaseManager):
    KEY_SETTING = 'ALL_FLAGS_CACHE_KEY'


class SwitchManager(BaseManager):
    KEY_SETTING = 'ALL_SWITCHES_CACHE_KEY'

Ejemplo n.º 16
0
try:
    from django.utils import timezone as datetime
except ImportError:
    from datetime import datetime

from django.conf import settings
from django.contrib.auth.models import Group
from django.db import models
from django.db.models.signals import post_save, post_delete, m2m_changed
from django.utils.encoding import python_2_unicode_compatible

from waffle import managers
from waffle.utils import get_setting, keyfmt, get_cache

cache = get_cache()
CACHE_EMPTY = '-'


@python_2_unicode_compatible
class BaseModel(models.Model):
    SINGLE_CACHE_KEY = ''
    ALL_CACHE_KEY = ''

    class Meta(object):
        abstract = True

    def __str__(self):
        return self.name

    @classmethod
Ejemplo n.º 17
0
 def create(self, *args, **kwargs):
     cache = get_cache()
     ret = super(BaseManager, self).create(*args, **kwargs)
     cache_key = get_setting(self.KEY_SETTING)
     cache.delete(cache_key)
     return ret
Ejemplo n.º 18
0
 def flush(self):
     cache = get_cache()
     keys = self.get_flush_keys()
     cache.delete_many(keys)
Ejemplo n.º 19
0
 def flush(self):
     cache = get_cache()
     keys = self.get_flush_keys()
     cache.delete_many(keys)