-
Notifications
You must be signed in to change notification settings - Fork 28
/
forms.py
48 lines (38 loc) · 1.42 KB
/
forms.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
from django import forms
from flags.conditions import get_condition, get_conditions
from flags.models import FlagState
from flags.sources import get_flags
class FlagStateForm(forms.ModelForm):
name = forms.ChoiceField(label="Flag", required=True)
condition = forms.ChoiceField(label="Condition name", required=True)
value = forms.CharField(label="Expected value", required=True)
required = forms.BooleanField(
label="Required",
required=False,
help_text=(
'All conditions marked "required" must be met to enable '
"the flag"
),
)
def __init__(self, *args, **kwargs):
super(FlagStateForm, self).__init__(*args, **kwargs)
self.fields["name"].choices = [
(f, f) for f in sorted(get_flags().keys())
]
self.fields["condition"].choices = [
(c, c) for c in sorted(get_conditions())
]
def clean_value(self):
condition_name = self.cleaned_data.get("condition")
value = self.cleaned_data.get("value")
condition = get_condition(condition_name)
validator = getattr(condition, "validate")
if validator is not None:
try:
validator(value)
except Exception as e:
raise forms.ValidationError(e)
return value
class Meta:
model = FlagState
fields = ("name", "condition", "value", "required")