Esempio n. 1
0
def flag_index(request, name):
    flag = get_flags().get(name)

    # If there's a database boolean condition, fetch it and treat it as a
    # on/off switch
    if 'enable' in request.GET or 'disable' in request.GET:
        db_boolean_condition = next(
            (c for c in flag.conditions if c.condition == 'boolean'
             and getattr(c, 'obj', None) is not None), None)

        if db_boolean_condition is None:
            boolean_condition_obj = FlagState.objects.create(
                name=name, condition='boolean', value='True')
        else:
            boolean_condition_obj = db_boolean_condition.obj

        if 'enable' in request.GET and not bool_enabled(flag):
            boolean_condition_obj.value = True
        elif 'disable' in request.GET and bool_enabled(flag):
            boolean_condition_obj.value = False

        boolean_condition_obj.save()

        if flag_enabled('WAGTAILFLAGS_ADMIN_BIG_LIST'):
            return redirect('{flags_list}#{flag_name}'.format(
                flags_list=resolve_url('wagtailflags:list'), flag_name=name))
        else:
            return redirect('wagtailflags:flag_index', name=name)

    context = {
        'flag': flag,
    }
    return render(request, 'wagtailflags/flags/flag_index.html', context)
def disablable(flag):
    """Return true if a flag is disablable by Wagtail-Flags.

    A flag is disablable by Wagtail-Flags if it has a required boolean
    condition and that boolean condition is True.
    """
    return not any(
        c.required for c in flag.conditions if c.condition == "boolean"
    ) and bool_enabled(flag)
Esempio n. 3
0
def flag_index(request, name):
    flag = get_flags().get(name)

    if not flag:
        raise Http404

    # If there's a database boolean condition, fetch it and treat it as a
    # on/off switch
    if "enable" in request.GET or "disable" in request.GET:
        db_boolean_condition = next(
            (
                c
                for c in flag.conditions
                if c.condition == "boolean"
                and getattr(c, "obj", None) is not None
            ),
            None,
        )

        if db_boolean_condition is None:
            boolean_condition_obj = FlagState.objects.create(
                name=name, condition="boolean", value="True"
            )
        else:
            boolean_condition_obj = db_boolean_condition.obj

        if "enable" in request.GET and not bool_enabled(flag):
            boolean_condition_obj.value = True
            flag_enabled.send(sender=flag_index, flag_name=flag.name)
        elif "disable" in request.GET and bool_enabled(flag):
            boolean_condition_obj.value = False
            flag_disabled.send(sender=flag_index, flag_name=flag.name)

        boolean_condition_obj.save()
        return redirect("wagtailflags:flag_index", name=name)

    context = {
        "flag": flag,
    }
    return render(request, "wagtailflags/flags/flag_index.html", context)
 def test_enabled_disabled(self):
     flag = get_flags().get("MYFLAG")
     self.assertFalse(bool_enabled(flag))
 def test_enabled_enabled(self):
     flag = get_flags().get("MYFLAG")
     self.assertTrue(bool_enabled(flag))
def enablable(flag):
    return (not any(c.required
                    for c in flag.conditions if c.condition == 'boolean')
            and not bool_enabled(flag))