From 37ecb3ca6619f73541fb63b800315563bdb43624 Mon Sep 17 00:00:00 2001 From: Lukasz Zajaczkowski Date: Tue, 17 Apr 2018 13:21:51 +0200 Subject: [PATCH] Change IntegerField to ChoiceField for notification period Story: 2001506 Task: 6297 Change-Id: I937507e9a65dbf4cd3962c0910180464378aed00 --- monitoring/notifications/forms.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/monitoring/notifications/forms.py b/monitoring/notifications/forms.py index 7cb11c2a..011b751b 100644 --- a/monitoring/notifications/forms.py +++ b/monitoring/notifications/forms.py @@ -44,6 +44,7 @@ class BaseNotificationMethodForm(forms.SelfHandlingForm): choices = [(n['type'], n['type'].capitalize()) for n in self.notification_types] choices = sorted(choices, key=lambda c: c[0]) + period_choices = [(0, '0'), (60, '60')] self.fields['name'] = forms.CharField(label=_("Name"), required=required, @@ -63,24 +64,21 @@ class BaseNotificationMethodForm(forms.SelfHandlingForm): max_length="512", widget=textWidget, 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.")) + self.fields['period'] = forms.ChoiceField(label=_("Period"), + widget=selectWidget, + choices=period_choices, + 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: + if data['type'] != constants.NotificationType.WEBHOOK and data['period'] != '0': 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'] @@ -115,7 +113,7 @@ class CreateMethodForm(BaseNotificationMethodForm): name=data['name'], type=data['type'], address=data['address'], - period=data['period']) + period=int(data['period'])) messages.success(request, _('Notification method has been created ' 'successfully.')) @@ -148,7 +146,7 @@ class EditMethodForm(BaseNotificationMethodForm): kwargs['name'] = data['name'] kwargs['type'] = data['type'] kwargs['address'] = data['address'] - kwargs['period'] = data['period'] + kwargs['period'] = int(data['period']) api.monitor.notification_update( request, **kwargs