From a42d882bd6e0ff5f753a4ed8aba1e081be043aae Mon Sep 17 00:00:00 2001 From: Lukasz Zajaczkowski Date: Tue, 10 Apr 2018 07:13:25 +0200 Subject: [PATCH] Fix validators for creating and editing notifications Change-Id: I28470c663635be564fde1b5de979ed4f478008e9 Story: 2001807 Task: 12540 --- monitoring/notifications/forms.py | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/monitoring/notifications/forms.py b/monitoring/notifications/forms.py index edeeb042..7cb11c2a 100644 --- a/monitoring/notifications/forms.py +++ b/monitoring/notifications/forms.py @@ -65,32 +65,28 @@ class BaseNotificationMethodForm(forms.SelfHandlingForm): help_text=_("The email/url address to notify.")) self.fields['period'] = forms.IntegerField(label=_("Period"), min_value=0, + max_value=60, initial=0, required=required, help_text=_("The notification period.")) def clean_period(self): '''Check to make sure period is zero unless type is WEBHOOK. + For WEBHOOK period must be set to 0 or 60. ''' data = self.cleaned_data if data['type'] != constants.NotificationType.WEBHOOK and data['period'] != 0: - raise forms.ValidationError(_("Period must be zero except for type webhook.")) + raise forms.ValidationError( + _('Period must be zero except for type webhook.')) + elif (data['type'] == constants.NotificationType.WEBHOOK and + data['period'] not in [0, 60]): + raise forms.ValidationError(_('Period must be 0 or 60.')) return data['period'] - @cached_property - def notification_types(self): - return api.monitor.notification_type_list(self.request) - - -class CreateMethodForm(BaseNotificationMethodForm): - def __init__(self, request, *args, **kwargs): - super(CreateMethodForm, self).__init__(request, *args, **kwargs) - super(CreateMethodForm, self)._init_fields(readOnly=False) - def clean_address(self): '''Check to make sure address is the correct format depending on the - type of notification + type of notification. ''' data = self.cleaned_data if data['type'] == constants.NotificationType.EMAIL: @@ -102,6 +98,16 @@ class CreateMethodForm(BaseNotificationMethodForm): return data['address'] + @cached_property + def notification_types(self): + return api.monitor.notification_type_list(self.request) + + +class CreateMethodForm(BaseNotificationMethodForm): + def __init__(self, request, *args, **kwargs): + super(CreateMethodForm, self).__init__(request, *args, **kwargs) + super(CreateMethodForm, self)._init_fields(readOnly=False) + def handle(self, request, data): try: api.monitor.notification_create(