Skip to content

Commit

Permalink
Fixed validation, with debug symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
hmpf committed Aug 29, 2023
1 parent ba54047 commit 891a3bc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
31 changes: 27 additions & 4 deletions python/nav/web/alertprofiles/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,22 +546,36 @@ class ExpressionForm(forms.ModelForm):
match_field = forms.ModelChoiceField(
queryset=MatchField.objects.all(), widget=forms.widgets.HiddenInput
)
value = forms.CharField(required=True)

class Meta(object):
model = Expression
fields = '__all__'

def __init__(self, *args, **kwargs):
match_field = kwargs.pop('match_field', None)
match_field = kwargs.pop('match_field', None) # add_expression
if not match_field:
print("XXX: saving")
match_field = args[0].get('match_field', None) # save_expression
self.match_field = match_field
print("XXX: args", args)
print("XXX: kwargs", kwargs)
print("XXX: match_field", match_field)
super(ExpressionForm, self).__init__(*args, **kwargs)

if isinstance(match_field, MatchField):
if not match_field:
return

if not isinstance(match_field, MatchField):
match_field = MatchField.objects.get(pk=match_field)

if True: # maintain indent for the sake off smaller diff!
print("XXX: match_field.show_list", match_field.show_list)
# Get all operators and make a choice field
operators = match_field.operator_set.all()
self.fields['operator'] = forms.models.ChoiceField(
choices=[(o.type, o) for o in operators]
)
print("XXX: operators choice:", [(o.type, o) for o in operators])

if match_field.show_list:
# Values are selected from a multiple choice list.
Expand Down Expand Up @@ -633,19 +647,25 @@ def __init__(self, *args, **kwargs):

# At last we acctually add the multiple choice field.
self.fields['value'] = forms.MultipleChoiceField(choices=choices)
else:
self.fields['value'] = forms.CharField(required=True)

def clean(self) -> Dict[str, Any]:
validated_data = super().clean()
print("XXX: fields", self.fields)
print("XXX: Validated data:", validated_data)

match_field = validated_data["match_field"]
operator_type = validated_data["operator"]
operator_type = int(validated_data["operator"])
value = validated_data["value"]

print("XXX: Operators", Operator)
if match_field.data_type == MatchField.IP:
if operator_type == Operator.IN:
ip_list = value.split()
else:
ip_list = [value]
print("XXX: ip_list:", ip_list)
validated_ip_addresses = []
for ip in ip_list:
try:
Expand All @@ -660,6 +680,7 @@ def clean(self) -> Dict[str, Any]:
# Bring ip address back into original format to be processed below
value = " ".join(validated_ip_addresses)

print("XXX: value:", value)
if operator_type == Operator.IN:
"""If input was a multiple choice list we have to join each option in one
string, where each option is separated by a | (pipe).
Expand All @@ -669,4 +690,6 @@ def clean(self) -> Dict[str, Any]:
else:
validated_data["value"] = "|".join(value)

# if validated_data["value"] != "172.0.0.1":
# assert False, validated_data
return validated_data
2 changes: 2 additions & 0 deletions tests/integration/web/alertprofiles_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ def test_alertprofiles_add_expression_with_multiple_alert_types_should_succeed(
}
response = client.post(url, data=data, follow=True)
assert response.status_code == 200
for expression in Expression.objects.filter(filter=dummy_filter, match_field=string_match_field, operator=Operator.IN):
print("XXX: expression:", vars(expression))
assert Expression.objects.filter(
filter=dummy_filter,
match_field=string_match_field,
Expand Down

0 comments on commit 891a3bc

Please sign in to comment.