mirror of
https://github.com/ansible/awx.git
synced 2026-03-21 10:57:36 -02:30
Implement notification serializer and validations
This commit is contained in:
@@ -12,7 +12,7 @@ from rest_framework import serializers
|
|||||||
from rest_framework.request import clone_request
|
from rest_framework.request import clone_request
|
||||||
|
|
||||||
# Ansible Tower
|
# Ansible Tower
|
||||||
from awx.main.models import InventorySource
|
from awx.main.models import InventorySource, NotificationTemplate
|
||||||
|
|
||||||
|
|
||||||
class Metadata(metadata.SimpleMetadata):
|
class Metadata(metadata.SimpleMetadata):
|
||||||
@@ -55,6 +55,12 @@ class Metadata(metadata.SimpleMetadata):
|
|||||||
get_group_by_choices = getattr(InventorySource, 'get_%s_group_by_choices' % cp)
|
get_group_by_choices = getattr(InventorySource, 'get_%s_group_by_choices' % cp)
|
||||||
field_info['%s_group_by_choices' % cp] = get_group_by_choices()
|
field_info['%s_group_by_choices' % cp] = get_group_by_choices()
|
||||||
|
|
||||||
|
# Special handling of notification configuration where the required properties
|
||||||
|
# are conditional on the type selected.
|
||||||
|
if field.field_name == 'notification_configuration':
|
||||||
|
for (notification_type_name, notification_tr_name, notification_type_class) in NotificationTemplate.NOTIFICATION_TYPES:
|
||||||
|
field_info[notification_type_name] = notification_type_class.init_parameters
|
||||||
|
|
||||||
# Update type of fields returned...
|
# Update type of fields returned...
|
||||||
if field.field_name == 'type':
|
if field.field_name == 'type':
|
||||||
field_info['type'] = 'multiple choice'
|
field_info['type'] = 'multiple choice'
|
||||||
|
|||||||
@@ -2035,6 +2035,21 @@ class JobLaunchSerializer(BaseSerializer):
|
|||||||
attrs = super(JobLaunchSerializer, self).validate(attrs)
|
attrs = super(JobLaunchSerializer, self).validate(attrs)
|
||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
|
class NotificationTemplateSerializer(BaseSerializer):
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = NotificationTemplate
|
||||||
|
fields = ('*', 'notification_type', 'notification_configuration')
|
||||||
|
|
||||||
|
def validate(self, attrs):
|
||||||
|
notification_class = NotificationTemplate.CLASS_FOR_NOTIFICATION_TYPE[attrs['notification_type']]
|
||||||
|
missing_fields = []
|
||||||
|
for field in notification_class.init_parameters:
|
||||||
|
if field not in attrs['notification_configuration']:
|
||||||
|
missing_fields.append(field)
|
||||||
|
if missing_fields:
|
||||||
|
raise serializers.ValidationError("Missing required fields for Notification Configuration: {}".format(missing_fields))
|
||||||
|
return attrs
|
||||||
|
|
||||||
class ScheduleSerializer(BaseSerializer):
|
class ScheduleSerializer(BaseSerializer):
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user