Example #1
0
 def create_switch(self, name=None, db=False, **kw):
     kw.setdefault('active', True)
     if name is not None:
         kw['name'] = name
     switch = Switch(**kw)
     switch.save() if db else cache_switch(instance=switch)
     return switch
Example #2
0
 def create_switch(self, name=None, db=False, **kw):
     kw.setdefault('active', True)
     if name is not None:
         kw['name'] = name
     switch = Switch(**kw)
     switch.save() if db else cache_switch(instance=switch)
     return switch
Example #3
0
    def test_get_switches_for_site(self):
        self.assertTrue(len(Switch.get_switches_for_site(self.site1)) == 0)
        name1 = "foo"
        Switch.objects.create(name=name1, active=True, site=self.site1)

        self.assertEqual([name1], [sw.name for sw in Switch.get_switches_for_site(self.site1)])
        # by default switch is sites-global
        self.assertEqual([name1], [sw.name for sw in Switch.get_switches_for_site(self.site2)])

        name2 = "bar"
        Switch.objects.create(name=name2, active=True, site=self.site2, all_sites_override=False)
        self.assertEqual({name1, name2}, set([sw.name for sw in Switch.get_switches_for_site(self.site2)]))
        self.assertEqual([name1], [sw.name for sw in Switch.get_switches_for_site(self.site1)])
Example #4
0
def whoami(request):
    """
    Return a JSON object representing the current user, either
    authenticated or anonymous.
    """
    user = request.user
    if user.is_authenticated:
        data = {
            "username": user.username,
            "is_authenticated": True,
            "avatar_url": get_avatar_url(user),
            "email": user.email,
            "subscriber_number": user.subscriber_number,
        }
        if UserSubscription.objects.filter(user=user, canceled__isnull=True).exists():
            data["is_subscriber"] = True
        if user.is_staff:
            data["is_staff"] = True
        if user.is_superuser:
            data["is_superuser"] = True
        if user.is_beta_tester:
            data["is_beta_tester"] = True
    else:
        data = {}

    data["waffle"] = {
        "flags": {},
        "switches": {s.name: True for s in Switch.get_all() if s.is_active()},
    }
    # Specifically and more smartly loop over the waffle Flag objects
    # to avoid unnecessary `cache.get(...)` calls within the `flag.is_active(request)`.
    for flag in Flag.get_all():
        if not request.user.is_authenticated:
            # Majority of users are anonymous, so let's focus on that.
            # Let's see if there's a quick reason to bail the
            # expensive `flag.is_active(request)` call.
            if (
                flag.authenticated or flag.staff or flag.superusers
            ) and not flag.everyone:
                continue
            if not (flag.languages or flag.percent or flag.everyone):
                continue
            if flag.languages:
                languages = [ln.strip() for ln in flag.languages.split(",")]
                if (
                    not hasattr(request, "LANGUAGE_CODE")
                    or request.LANGUAGE_CODE not in languages
                ):
                    continue

        if flag.is_active(request):
            data["waffle"]["flags"][flag.name] = True

    return JsonResponse(data)
Example #5
0
def whoami(request):
    """
    Return a JSON object representing the current user, either
    authenticated or anonymous.
    """
    user = request.user
    if user.is_authenticated:
        data = {
            'username': user.username,
            'timezone': user.timezone,
            'is_authenticated': True,
            'is_staff': user.is_staff,
            'is_superuser': user.is_superuser,
            'is_beta_tester': user.is_beta_tester,
            'gravatar_url': {
                'small': gravatar_url(user.email, size=50),
                'large': gravatar_url(user.email, size=200),
            }
        }
    else:
        data = {
            'username': None,
            'timezone': settings.TIME_ZONE,
            'is_authenticated': False,
            'is_staff': False,
            'is_superuser': False,
            'is_beta_tester': False,
            'gravatar_url': {
                'small': None,
                'large': None,
            }
        }

    # Add waffle data to the dict we're going to be returning.
    # This is what the waffle.wafflejs() template tag does, but we're
    # doing it via an API instead of hardcoding the settings into
    # the HTML page. See also from waffle.views._generate_waffle_js.
    #
    # Note that if we upgrade django-waffle, version 15 introduces a
    # pluggable flag model, and the approved way to get all flag
    # objects will then become:
    #    get_waffle_flag_model().get_all()
    #
    data['waffle'] = {
        'flags': {f.name: f.is_active(request)
                  for f in Flag.get_all()},
        'switches': {s.name: s.is_active()
                     for s in Switch.get_all()},
        'samples': {s.name: s.is_active()
                    for s in Sample.get_all()},
    }

    return JsonResponse(data)
Example #6
0
def whoami(request):
    """
    Return a JSON object representing the current user, either
    authenticated or anonymous.
    """
    user = request.user
    if user.is_authenticated:
        data = {
            "username": user.username,
            "is_authenticated": True,
            "avatar_url": get_avatar_url(user),
            "email": user.email,
            "subscriber_number": user.subscriber_number,
        }
        if UserSubscription.objects.filter(user=user,
                                           canceled__isnull=True).exists():
            data["is_subscriber"] = True
        if user.is_staff:
            data["is_staff"] = True
        if user.is_superuser:
            data["is_superuser"] = True
        if user.is_beta_tester:
            data["is_beta_tester"] = True

        # This is rather temporary field. Once we're off the Wiki and into Yari
        # this no longer makes sense to keep.
        data["wiki_contributions"] = user.created_revisions.count()
    else:
        data = {}

    # Add waffle data to the dict we're going to be returning.
    # This is what the waffle.wafflejs() template tag does, but we're
    # doing it via an API instead of hardcoding the settings into
    # the HTML page. See also from waffle.views._generate_waffle_js.
    #
    # Note that if we upgrade django-waffle, version 15 introduces a
    # pluggable flag model, and the approved way to get all flag
    # objects will then become:
    #    get_waffle_flag_model().get_all()
    #
    data["waffle"] = {
        "flags":
        {f.name: True
         for f in Flag.get_all() if f.is_active(request)},
        "switches": {s.name: True
                     for s in Switch.get_all() if s.is_active()},
        "samples": {s.name: True
                    for s in Sample.get_all() if s.is_active()},
    }
    return JsonResponse(data)
Example #7
0
def whoami(request):
    """
    Return a JSON object representing the current user, either
    authenticated or anonymous.
    """
    user = request.user
    if user.is_authenticated:
        data = {
            'username': user.username,
            'timezone': user.timezone,
            'is_authenticated': True,
            'is_staff': user.is_staff,
            'is_superuser': user.is_superuser,
            'is_beta_tester': user.is_beta_tester,
            'gravatar_url': {
                'small': gravatar_url(user.email, size=50),
                'large': gravatar_url(user.email, size=200),
            }
        }
    else:
        data = {
            'username': None,
            'timezone': settings.TIME_ZONE,
            'is_authenticated': False,
            'is_staff': False,
            'is_superuser': False,
            'is_beta_tester': False,
            'gravatar_url': {
                'small': None,
                'large': None,
            }
        }

    # Add waffle data to the dict we're going to be returning.
    # This is what the waffle.wafflejs() template tag does, but we're
    # doing it via an API instead of hardcoding the settings into
    # the HTML page. See also from waffle.views._generate_waffle_js.
    #
    # Note that if we upgrade django-waffle, version 15 introduces a
    # pluggable flag model, and the approved way to get all flag
    # objects will then become:
    #    get_waffle_flag_model().get_all()
    #
    data['waffle'] = {
        'flags': {f.name: f.is_active(request) for f in Flag.get_all()},
        'switches': {s.name: s.is_active() for s in Switch.get_all()},
        'samples': {s.name: s.is_active() for s in Sample.get_all()},
    }

    return JsonResponse(data)
Example #8
0
def whoami(request):
    """
    Return a JSON object representing the current user, either
    authenticated or anonymous.
    """
    user = request.user
    if user.is_authenticated:
        data = {
            "username": user.username,
            "timezone": user.timezone,
            "is_authenticated": True,
            "is_staff": user.is_staff,
            "is_superuser": user.is_superuser,
            "is_beta_tester": user.is_beta_tester,
            "avatar_url": get_avatar_url(user),
            "is_subscriber": UserSubscription.objects.filter(
                user=user, canceled__isnull=True
            ).exists(),
        }
    else:
        data = {
            "username": None,
            "timezone": settings.TIME_ZONE,
            "is_authenticated": False,
            "is_staff": False,
            "is_superuser": False,
            "is_beta_tester": False,
            "avatar_url": None,
            "is_subscriber": False,
        }

    # Add waffle data to the dict we're going to be returning.
    # This is what the waffle.wafflejs() template tag does, but we're
    # doing it via an API instead of hardcoding the settings into
    # the HTML page. See also from waffle.views._generate_waffle_js.
    #
    # Note that if we upgrade django-waffle, version 15 introduces a
    # pluggable flag model, and the approved way to get all flag
    # objects will then become:
    #    get_waffle_flag_model().get_all()
    #
    data["waffle"] = {
        "flags": {f.name: f.is_active(request) for f in Flag.get_all()},
        "switches": {s.name: s.is_active() for s in Switch.get_all()},
        "samples": {s.name: s.is_active() for s in Sample.get_all()},
    }
    return JsonResponse(data)
Example #9
0
def _generate_waffle_js(request):
    flags = get_waffle_flag_model().get_all()
    flag_values = [(f.name, f.is_active(request)) for f in flags]

    switches = Switch.get_all()
    switch_values = [(s.name, s.is_active()) for s in switches]

    samples = Sample.get_all()
    sample_values = [(s.name, s.is_active()) for s in samples]

    return loader.render_to_string('waffle/waffle.js', {
        'flags': flag_values,
        'switches': switch_values,
        'samples': sample_values,
        'flag_default': get_setting('FLAG_DEFAULT'),
        'switch_default': get_setting('SWITCH_DEFAULT'),
        'sample_default': get_setting('SAMPLE_DEFAULT'),
    })
Example #10
0
def _should_process_smses():
    switch = Switch.get(PROCESS_SMSES_SWITCH)
    if not switch.pk:
        # To remain backwards compatible, we will process smses if the switch hasn't been set at all
        return True
    return switch.active
Example #11
0
 def create_switch(self, name=None, db=False, **kw):
     kw.setdefault("active", True)
     if name is not None:
         kw["name"] = name
     switch = Switch(**kw)
     switch.save() if db else cache_switch(instance=switch)
Example #12
0
 def list(self, *args, **kwargs):
     return Switch.get_all() + Sample.get_all() + get_waffle_flag_model().get_all()