# Buggy code
def can_copy(self, obj):
    if self.save_messages:
        missing_ujt = []
        missing_credentials = []
        missing_inventories = []
        qs = obj.workflow_job_template_nodes
        qs = qs.prefetch_related(
            "unified_job_template", "inventory__use_role", "credentials__use_role"
        )
        for node in qs.all():
            node_errors = {}
            if node.inventory and self.user not in node.inventory.use_role:
                missing_inventories.append(node.inventory.name)
            for cred in node.credentials.all():
                if self.user not in cred.use_role:
                    missing_credentials.append(node.credential.name)
            ujt = node.unified_job_template
            if ujt and not self.user.can_access(
                UnifiedJobTemplate, "start", ujt, validate_license=False
            ):
                missing_ujt.append(ujt.name)
            if node_errors:
                wfjt_errors[node.id] = node_errors
        if missing_ujt:
            self.messages["templates_unable_to_copy"] = missing_ujt
        if missing_credentials:
            self.messages["credentials_unable_to_copy"] = missing_credentials
        if missing_inventories:
            self.messages["inventories_unable_to_copy"] = missing_inventories

    return self.check_related(
        "organization",
        Organization,
        {"reference_obj": obj},
        role_field="workflow_admin_role",
        mandatory=True,
    )


# Traceback (KeyError)
2018-03-23 15:38:36,900 ERROR    django.request Internal Server Error: /api/v2/inventory_sources/25/
Traceback (most recent call last):
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/usr/lib/python2.7/site-packages/awx/wsgi.py", line 65, in _legacy_get_response
return super(AWXWSGIHandler, self)._legacy_get_response(request)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
response = self._get_response(request)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/utils/decorators.py", line 185, in inner
return func(*args, **kwargs)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/awx/api/generics.py", line 284, in dispatch
return super(APIView, self).dispatch(request, *args, **kwargs)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/rest_framework/views.py", line 494, in dispatch
response = self.handle_exception(exc)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/rest_framework/views.py", line 454, in handle_exception
self.raise_uncaught_exception(exc)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/rest_framework/views.py", line 491, in dispatch
response = handler(request, *args, **kwargs)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/rest_framework/generics.py", line 257, in put
return self.update(request, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/awx/api/generics.py", line 741, in update
return super(RetrieveUpdateAPIView, self).update(request, *args, **kwargs)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/rest_framework/mixins.py", line 67, in update
instance = self.get_object()
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/rest_framework/generics.py", line 101, in get_object
self.check_object_permissions(self.request, obj)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/rest_framework/views.py", line 344, in check_object_permissions
if not permission.has_object_permission(request, self, obj):
File "/usr/lib/python2.7/site-packages/awx/api/permissions.py", line 138, in has_object_permission
return self.has_permission(request, view, obj)
File "/usr/lib/python2.7/site-packages/awx/api/permissions.py", line 129, in has_permission
response = self.check_permissions(request, view, obj)
File "/usr/lib/python2.7/site-packages/awx/api/permissions.py", line 118, in check_permissions
result = check_method and check_method(request, view, obj)
File "/usr/lib/python2.7/site-packages/awx/api/permissions.py", line 82, in check_put_permissions
request.data)
File "/usr/lib/python2.7/site-packages/awx/main/access.py", line 117, in check_user_access
result = access_method(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/awx/main/access.py", line 157, in wrapper
return func(self, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/awx/main/access.py", line 931, in can_change
self.check_related('credential', Credential, data, obj=obj, role_field='use_role') and
File "/usr/lib/python2.7/site-packages/awx/main/access.py", line 290, in check_related
if current and (changed or mandatory) and (not user_has_resource_access(current)):
File "/usr/lib/python2.7/site-packages/awx/main/access.py", line 283, in user_has_resource_access
access_method_type = {'admin_role': 'change', 'execute_role': 'start'}[role_field]
KeyError: 'use_role'

# Fixed code
def can_copy(self, obj):
    if self.save_messages:
        missing_ujt = []
        missing_credentials = []
        missing_inventories = []
        qs = obj.workflow_job_template_nodes
        qs = qs.prefetch_related(
            "unified_job_template", "inventory__use_role", "credentials__use_role"
        )
        for node in qs.all():
            node_errors = {}
            if node.inventory and self.user not in node.inventory.use_role:
                missing_inventories.append(node.inventory.name)
            for cred in node.credentials.all():
                if self.user not in cred.use_role:
                    missing_credentials.append(cred.name)
            ujt = node.unified_job_template
            if ujt and not self.user.can_access(
                UnifiedJobTemplate, "start", ujt, validate_license=False
            ):
                missing_ujt.append(ujt.name)
            if node_errors:
                wfjt_errors[node.id] = node_errors
        if missing_ujt:
            self.messages["templates_unable_to_copy"] = missing_ujt
        if missing_credentials:
            self.messages["credentials_unable_to_copy"] = missing_credentials
        if missing_inventories:
            self.messages["inventories_unable_to_copy"] = missing_inventories

    return self.check_related(
        "organization",
        Organization,
        {"reference_obj": obj},
        role_field="workflow_admin_role",
        mandatory=True,
    )