Fix validators for creating and editing notifications

Change-Id: I28470c663635be564fde1b5de979ed4f478008e9
Story: 2001807
Task: 12540
This commit is contained in:
Lukasz Zajaczkowski 2018-04-10 07:13:25 +02:00
parent 4bcc3b5b00
commit a42d882bd6
1 changed files with 18 additions and 12 deletions

View File

@ -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(