-
Notifications
You must be signed in to change notification settings - Fork 28
/
state.py
90 lines (69 loc) · 2.68 KB
/
state.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from django.apps import apps
from django.core.exceptions import AppRegistryNotReady
from flags.models import FlagState
from flags.sources import get_flags
def _get_flag_state(flag_name, **kwargs):
""" A private function that performs the actual state checking """
flags = get_flags(request=kwargs.get("request"))
flag = flags.get(flag_name)
if flag is not None:
return flag.check_state(**kwargs)
return None
def _set_flag_state(
flag_name, state, create_boolean_condition=True, request=None
):
""" A private function to set a boolean condition to the desired state """
flags = get_flags(request=request)
flag = flags.get(flag_name)
if flag is None:
raise KeyError(f"No flag with name {flag_name} exists")
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 not None:
# We already have a boolean condition
boolean_condition_obj = db_boolean_condition.obj
elif db_boolean_condition is None and create_boolean_condition:
# We can create a boolean condition and we need to.
boolean_condition_obj = FlagState.objects.create(
name=flag_name, condition="boolean", value="True"
)
else:
raise ValueError(f"Flag {flag_name} does not have a boolean condition")
boolean_condition_obj.value = state
boolean_condition_obj.save()
def flag_state(flag_name, **kwargs):
""" Return the value for the flag by passing kwargs to its conditions """
if not apps.ready:
raise AppRegistryNotReady(
"Feature flag state cannot be checked before the app registry "
"is ready."
)
return _get_flag_state(flag_name, **kwargs)
def flag_enabled(flag_name, **kwargs):
""" Check if a flag is enabled by passing kwargs to its conditions. """
return flag_state(flag_name, **kwargs)
def flag_disabled(flag_name, **kwargs):
""" Check if a flag is disabled by passing kwargs to its conditions. """
return not flag_state(flag_name, **kwargs)
def enable_flag(flag_name, create_boolean_condition=True, request=None):
""" Add or set a boolean condition to `True` """
_set_flag_state(
flag_name,
True,
create_boolean_condition=create_boolean_condition,
request=request,
)
def disable_flag(flag_name, create_boolean_condition=True, request=None):
""" Add or set a boolean condition to `False` """
_set_flag_state(
flag_name,
False,
create_boolean_condition=create_boolean_condition,
request=request,
)