mirror of
https://github.com/ansible/awx.git
synced 2026-05-14 12:57:40 -02:30
prefetch labels
This commit is contained in:
@@ -1839,11 +1839,15 @@ class OrganizationCredentialSerializerCreate(CredentialSerializerCreate):
|
|||||||
class LabelsListMixin(object):
|
class LabelsListMixin(object):
|
||||||
|
|
||||||
def _summary_field_labels(self, obj):
|
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 hasattr(obj, '_prefetched_objects_cache') and obj.labels.prefetch_cache_name in obj._prefetched_objects_cache:
|
||||||
if len(label_list) < 10:
|
label_list = [{'id': x.id, 'name': x.name} for x in obj.labels.all()[:10]]
|
||||||
label_ct = len(label_list)
|
label_ct = len(obj.labels.all())
|
||||||
else:
|
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}
|
return {'count': label_ct, 'results': label_list}
|
||||||
|
|
||||||
def get_summary_fields(self, obj):
|
def get_summary_fields(self, obj):
|
||||||
|
|||||||
@@ -2568,6 +2568,9 @@ class JobTemplateLabelList(DeleteLastUnattachLabelMixin, SubListCreateAttachDeta
|
|||||||
request.data['id'] = existing.id
|
request.data['id'] = existing.id
|
||||||
del request.data['name']
|
del request.data['name']
|
||||||
del request.data['organization']
|
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)
|
return super(JobTemplateLabelList, self).post(request, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import logging
|
|||||||
|
|
||||||
# Django
|
# Django
|
||||||
from django.conf import settings
|
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.auth.models import User
|
||||||
from django.contrib.contenttypes.models import ContentType
|
from django.contrib.contenttypes.models import ContentType
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
@@ -1868,6 +1868,7 @@ class UnifiedJobTemplateAccess(BaseAccess):
|
|||||||
qs = qs.prefetch_related(
|
qs = qs.prefetch_related(
|
||||||
'last_job',
|
'last_job',
|
||||||
'current_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.
|
# WISH - sure would be nice if the following worked, but it does not.
|
||||||
|
|||||||
Reference in New Issue
Block a user