Merge pull request #6537 from jangsutsr/6410_add_count_to_host_group_summary_field

Add count to host group summary field.
This commit is contained in:
Aaron Tan 2017-06-12 09:50:51 -04:00 committed by GitHub
commit bd4b2cfd6d
3 changed files with 17 additions and 6 deletions

View File

@ -44,7 +44,8 @@ from awx.main.access import get_user_capabilities
from awx.main.fields import ImplicitRoleField
from awx.main.utils import (
get_type_for_model, get_model_for_type, timestamp_apiformat,
camelcase_to_underscore, getattrd, parse_yaml_or_json)
camelcase_to_underscore, getattrd, parse_yaml_or_json,
has_model_field_prefetched)
from awx.main.utils.filters import SmartFilter
from awx.main.validators import vars_validate_or_raise
@ -1247,8 +1248,12 @@ class HostSerializer(BaseSerializerWithVariables):
d['last_job']['job_template_name'] = obj.last_job.job_template.name
except (KeyError, AttributeError):
pass
group_list = [{'id': g.id, 'name': g.name} for g in obj.groups.order_by('name')[:5]]
d.setdefault('groups', group_list)
if has_model_field_prefetched(obj, 'groups'):
group_list = sorted([{'id': g.id, 'name': g.name} for g in obj.groups.all()], key=lambda x: x['name'])[:5]
else:
group_list = [{'id': g.id, 'name': g.name} for g in obj.groups.all().order_by('name')[:5]]
group_cnt = obj.groups.count()
d.setdefault('groups', {'count': group_cnt, 'results': group_list})
d.setdefault('recent_jobs', [{
'id': j.job.id,
'name': j.job.job_template.name if j.job.job_template is not None else "",
@ -2179,7 +2184,7 @@ class OrganizationCredentialSerializerCreate(CredentialSerializerCreate):
class LabelsListMixin(object):
def _summary_field_labels(self, obj):
if hasattr(obj, '_prefetched_objects_cache') and obj.labels.prefetch_cache_name in obj._prefetched_objects_cache:
if has_model_field_prefetched(obj, 'labels'):
label_list = [{'id': x.id, 'name': x.name} for x in obj.labels.all()[:10]]
label_ct = len(obj.labels.all())
else:

View File

@ -19,6 +19,6 @@ from awx.main.utils.common import ( # noqa
build_proot_temp_dir,
wrap_args_with_proot,
get_system_task_capacity,
decrypt_field_value
decrypt_field_value,
has_model_field_prefetched
)

View File

@ -882,3 +882,9 @@ def get_search_fields(model):
'name', 'description'):
fields.append(field.name)
return fields
def has_model_field_prefetched(model_obj, field_name):
# NOTE: Update this function if django internal implementation changes.
return getattr(getattr(model_obj, field_name, None),
'prefetch_cache_name', '') in getattr(model_obj, '_prefetched_objects_cache', {})