diff --git a/awx/api/views.py b/awx/api/views.py index 734640577b..6bd917cab2 100644 --- a/awx/api/views.py +++ b/awx/api/views.py @@ -2497,6 +2497,18 @@ class JobTemplateLabelList(SubListCreateAttachDetachAPIView, DeleteLastUnattachL parent_model = JobTemplate relationship = 'labels' + def post(self, request, *args, **kwargs): + # If a label already exists in the database, attach it instead of erroring out + # that it already exists + if 'id' not in request.data and 'name' in request.data and 'organization' in request.data: + existing = Label.objects.filter(name=request.data['name'], organization_id=request.data['organization']) + if existing.exists(): + existing = existing[0] + request.data['id'] = existing.id + del request.data['name'] + del request.data['organization'] + return super(JobTemplateLabelList, self).post(request, *args, **kwargs) + class JobTemplateCallback(GenericAPIView): model = JobTemplate @@ -3815,7 +3827,6 @@ class RoleChildrenList(SubListAPIView): - # Create view functions for all of the class-based views to simplify inclusion # in URL patterns and reverse URL lookups, converting CamelCase names to # lowercase_with_underscore (e.g. MyView.as_view() becomes my_view).