From 757982088cb1b77d1bffa23f2f05d15051b7cdea Mon Sep 17 00:00:00 2001 From: AlanCoding Date: Fri, 17 Feb 2017 22:06:34 -0500 Subject: [PATCH] allow UJT content type list to come from cache --- awx/main/models/unified_jobs.py | 15 +++++++++------ .../tests/functional/models/test_unified_job.py | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/awx/main/models/unified_jobs.py b/awx/main/models/unified_jobs.py index 7da004eb7e..a23091962f 100644 --- a/awx/main/models/unified_jobs.py +++ b/awx/main/models/unified_jobs.py @@ -174,6 +174,13 @@ class UnifiedJobTemplate(PolymorphicModel, CommonModelNameNotUnique, Notificatio return [] return ['project', 'inventorysource', 'systemjobtemplate'] + @classmethod + def _submodels_with_roles(cls): + ujt_classes = [c for c in cls.__subclasses__() + if c._meta.model_name not in ['inventorysource', 'systemjobtemplate']] + ct_dict = ContentType.objects.get_for_models(*ujt_classes) + return [ct.id for ct in ct_dict.values()] + @classmethod def accessible_pk_qs(cls, accessor, role_field): ''' @@ -184,12 +191,8 @@ class UnifiedJobTemplate(PolymorphicModel, CommonModelNameNotUnique, Notificatio # do not use this if in a subclass if cls != UnifiedJobTemplate: return super(UnifiedJobTemplate, cls).accessible_pk_qs(accessor, role_field) - ujt_names = [c.__name__.lower() for c in cls.__subclasses__() - if c.__name__.lower() not in ['inventorysource', 'systemjobtemplate']] - subclass_content_types = list(ContentType.objects.filter( - model__in=ujt_names).values_list('id', flat=True)) - - return ResourceMixin._accessible_pk_qs(cls, accessor, role_field, content_types=subclass_content_types) + return ResourceMixin._accessible_pk_qs( + cls, accessor, role_field, content_types=cls._submodels_with_roles()) def _perform_unique_checks(self, unique_checks): # Handle the list of unique fields returned above. Replace with an diff --git a/awx/main/tests/functional/models/test_unified_job.py b/awx/main/tests/functional/models/test_unified_job.py index 870f9f034a..4d19e4191e 100644 --- a/awx/main/tests/functional/models/test_unified_job.py +++ b/awx/main/tests/functional/models/test_unified_job.py @@ -1,5 +1,20 @@ import pytest +# Django +from django.contrib.contenttypes.models import ContentType + +# AWX +from awx.main.models import UnifiedJobTemplate, JobTemplate, WorkflowJobTemplate, Project + + +@pytest.mark.django_db +def test_subclass_types(rando): + assert set(UnifiedJobTemplate._submodels_with_roles()) == set([ + ContentType.objects.get_for_model(JobTemplate).id, + ContentType.objects.get_for_model(Project).id, + ContentType.objects.get_for_model(WorkflowJobTemplate).id + ]) + class TestCreateUnifiedJob: '''