mirror of
https://github.com/ansible/awx.git
synced 2026-06-22 15:17:44 -02:30
Handle missing Organization Member/Admin role definitions gracefully
Use filter().first() instead of get() for RoleDefinition lookups in organization list and detail views. Returns 0 for user/admin counts when role definitions are not yet created, preventing 500 errors in environments where post_migrate signals haven't run. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -179,8 +179,10 @@ class OrganizationCountsMixin(object):
|
|||||||
|
|
||||||
db_results['projects'] = project_qs.values('organization').annotate(Count('organization')).order_by('organization')
|
db_results['projects'] = project_qs.values('organization').annotate(Count('organization')).order_by('organization')
|
||||||
|
|
||||||
member_rd = RoleDefinition.objects.get(name='Organization Member')
|
member_rd = RoleDefinition.objects.filter(name='Organization Member').first()
|
||||||
admin_rd = RoleDefinition.objects.get(name='Organization Admin')
|
admin_rd = RoleDefinition.objects.filter(name='Organization Admin').first()
|
||||||
|
|
||||||
|
if member_rd and admin_rd:
|
||||||
|
|
||||||
def assignment_count(rd):
|
def assignment_count(rd):
|
||||||
return Coalesce(
|
return Coalesce(
|
||||||
|
|||||||
@@ -79,8 +79,10 @@ class OrganizationDetail(RelatedJobsPreventDeleteMixin, RetrieveUpdateDestroyAPI
|
|||||||
|
|
||||||
org_counts = {}
|
org_counts = {}
|
||||||
access_kwargs = {'accessor': self.request.user, 'role_field': 'read_role'}
|
access_kwargs = {'accessor': self.request.user, 'role_field': 'read_role'}
|
||||||
member_rd = RoleDefinition.objects.get(name='Organization Member')
|
member_rd = RoleDefinition.objects.filter(name='Organization Member').first()
|
||||||
admin_rd = RoleDefinition.objects.get(name='Organization Admin')
|
admin_rd = RoleDefinition.objects.filter(name='Organization Admin').first()
|
||||||
|
|
||||||
|
if member_rd and admin_rd:
|
||||||
|
|
||||||
def assignment_count(rd):
|
def assignment_count(rd):
|
||||||
return Coalesce(
|
return Coalesce(
|
||||||
@@ -105,10 +107,13 @@ class OrganizationDetail(RelatedJobsPreventDeleteMixin, RetrieveUpdateDestroyAPI
|
|||||||
.values('users', 'admins')
|
.values('users', 'admins')
|
||||||
)
|
)
|
||||||
|
|
||||||
if not direct_counts:
|
if direct_counts:
|
||||||
return full_context
|
|
||||||
|
|
||||||
org_counts = direct_counts[0]
|
org_counts = direct_counts[0]
|
||||||
|
else:
|
||||||
|
org_counts = {'users': 0, 'admins': 0}
|
||||||
|
|
||||||
|
if not org_counts:
|
||||||
|
return full_context
|
||||||
org_counts['inventories'] = Inventory.accessible_objects(**access_kwargs).filter(organization__id=org_id).count()
|
org_counts['inventories'] = Inventory.accessible_objects(**access_kwargs).filter(organization__id=org_id).count()
|
||||||
org_counts['teams'] = Team.accessible_objects(**access_kwargs).filter(organization__id=org_id).count()
|
org_counts['teams'] = Team.accessible_objects(**access_kwargs).filter(organization__id=org_id).count()
|
||||||
org_counts['projects'] = Project.accessible_objects(**access_kwargs).filter(organization__id=org_id).count()
|
org_counts['projects'] = Project.accessible_objects(**access_kwargs).filter(organization__id=org_id).count()
|
||||||
|
|||||||
Reference in New Issue
Block a user