From 26dc430c59b9177608ffa2925ba922306c9bc232 Mon Sep 17 00:00:00 2001 From: Akita Noek Date: Mon, 22 Feb 2016 16:25:09 -0500 Subject: [PATCH] Look for and report on transaction errors within our implicit RBAC fields When a transaction is in a failed state these fields will not be able to create new role/resource entries. This check just makes it easier to see what's going on and aids in debugging. --- awx/main/fields.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/awx/main/fields.py b/awx/main/fields.py index e5f6e5d0f8..15224d43fd 100644 --- a/awx/main/fields.py +++ b/awx/main/fields.py @@ -2,6 +2,7 @@ # All Rights Reserved. # Django +from django.db import connection from django.db.models.signals import post_save from django.db.models.signals import m2m_changed from django.db import models @@ -14,6 +15,7 @@ from django.db.models.fields.related import ( ) from django.core.exceptions import FieldError +from django.db.transaction import TransactionManagementError # AWX @@ -63,6 +65,8 @@ class ResourceFieldDescriptor(ReverseSingleRelatedObjectDescriptor): resource = super(ResourceFieldDescriptor, self).__get__(instance, instance_type) if resource: return resource + if connection.needs_rollback: + raise TransactionManagementError('Current transaction has failed, cannot create implicit resource') resource = Resource.objects.create(content_object=instance) setattr(instance, self.field.name, resource) instance.save(update_fields=[self.field.name,]) @@ -107,6 +111,9 @@ class ImplicitRoleDescriptor(ReverseSingleRelatedObjectDescriptor): if not self.role_name: raise FieldError('Implicit role missing `role_name`') + if connection.needs_rollback: + raise TransactionManagementError('Current transaction has failed, cannot create implicit role') + role = Role.objects.create(name=self.role_name, content_object=instance) if self.parent_role: def resolve_field(obj, field):