diff --git a/awx/api/serializers.py b/awx/api/serializers.py index 8462ba4528..9dbdfd8d63 100644 --- a/awx/api/serializers.py +++ b/awx/api/serializers.py @@ -1839,11 +1839,15 @@ class OrganizationCredentialSerializerCreate(CredentialSerializerCreate): class LabelsListMixin(object): def _summary_field_labels(self, obj): - label_list = [{'id': x.id, 'name': x.name} for x in obj.labels.all().order_by('name')[:10]] - if len(label_list) < 10: - label_ct = len(label_list) + if hasattr(obj, '_prefetched_objects_cache') and obj.labels.prefetch_cache_name in obj._prefetched_objects_cache: + label_list = [{'id': x.id, 'name': x.name} for x in obj.labels.all()[:10]] + label_ct = len(obj.labels.all()) else: - label_ct = obj.labels.count() + label_list = [{'id': x.id, 'name': x.name} for x in obj.labels.all().order_by('name')[:10]] + if len(label_list) < 10: + label_ct = len(label_list) + else: + label_ct = obj.labels.count() return {'count': label_ct, 'results': label_list} def get_summary_fields(self, obj): diff --git a/awx/api/views.py b/awx/api/views.py index 9e046f5162..0f75d6ca61 100644 --- a/awx/api/views.py +++ b/awx/api/views.py @@ -2568,6 +2568,9 @@ class JobTemplateLabelList(DeleteLastUnattachLabelMixin, SubListCreateAttachDeta request.data['id'] = existing.id del request.data['name'] del request.data['organization'] + if Label.objects.filter(unifiedjobtemplate_labels=self.kwargs['pk']).count() > 100: + return Response(dict(msg=_('Maximum labels limit for a job template reached.')), + status=status.HTTP_400_BAD_REQUEST) return super(JobTemplateLabelList, self).post(request, *args, **kwargs) diff --git a/awx/main/access.py b/awx/main/access.py index 97d3131bb5..5adb4649ed 100644 --- a/awx/main/access.py +++ b/awx/main/access.py @@ -8,7 +8,7 @@ import logging # Django from django.conf import settings -from django.db.models import Q +from django.db.models import Q, Prefetch from django.contrib.auth.models import User from django.contrib.contenttypes.models import ContentType from django.utils.translation import ugettext_lazy as _ @@ -1868,6 +1868,7 @@ class UnifiedJobTemplateAccess(BaseAccess): qs = qs.prefetch_related( 'last_job', 'current_job', + Prefetch('labels', queryset=Label.objects.all().order_by('name')) ) # WISH - sure would be nice if the following worked, but it does not.