mirror of
https://github.com/ansible/awx.git
synced 2026-05-06 08:57:35 -02:30
Merge pull request #140 from AlanCoding/poly_why
hack to work around Django-polymorphic prefetch_related bug
This commit is contained in:
@@ -3662,11 +3662,11 @@ class ActivityStreamSerializer(BaseSerializer):
|
|||||||
for fk, __ in self._local_summarizable_fk_fields:
|
for fk, __ in self._local_summarizable_fk_fields:
|
||||||
if not hasattr(obj, fk):
|
if not hasattr(obj, fk):
|
||||||
continue
|
continue
|
||||||
allm2m = getattr(obj, fk).all()
|
m2m_list = self._get_rel(obj, fk)
|
||||||
if getattr(obj, fk).exists():
|
if m2m_list:
|
||||||
rel[fk] = []
|
rel[fk] = []
|
||||||
id_list = []
|
id_list = []
|
||||||
for thisItem in allm2m:
|
for thisItem in m2m_list:
|
||||||
if getattr(thisItem, 'id', None) in id_list:
|
if getattr(thisItem, 'id', None) in id_list:
|
||||||
continue
|
continue
|
||||||
id_list.append(getattr(thisItem, 'id', None))
|
id_list.append(getattr(thisItem, 'id', None))
|
||||||
@@ -3679,16 +3679,26 @@ class ActivityStreamSerializer(BaseSerializer):
|
|||||||
rel['unified_job_template'] = thisItem.unified_job_template.get_absolute_url(self.context.get('request'))
|
rel['unified_job_template'] = thisItem.unified_job_template.get_absolute_url(self.context.get('request'))
|
||||||
return rel
|
return rel
|
||||||
|
|
||||||
|
def _get_rel(self, obj, fk):
|
||||||
|
related_model = ActivityStream._meta.get_field(fk).related_model
|
||||||
|
related_manager = getattr(obj, fk)
|
||||||
|
if issubclass(related_model, PolymorphicModel) and hasattr(obj, '_prefetched_objects_cache'):
|
||||||
|
# HACK: manually fill PolymorphicModel caches to prevent running query multiple times
|
||||||
|
# unnecessary if django-polymorphic issue #68 is solved
|
||||||
|
if related_manager.prefetch_cache_name not in obj._prefetched_objects_cache:
|
||||||
|
obj._prefetched_objects_cache[related_manager.prefetch_cache_name] = list(related_manager.all())
|
||||||
|
return related_manager.all()
|
||||||
|
|
||||||
def get_summary_fields(self, obj):
|
def get_summary_fields(self, obj):
|
||||||
summary_fields = OrderedDict()
|
summary_fields = OrderedDict()
|
||||||
for fk, related_fields in self._local_summarizable_fk_fields:
|
for fk, related_fields in self._local_summarizable_fk_fields:
|
||||||
try:
|
try:
|
||||||
if not hasattr(obj, fk):
|
if not hasattr(obj, fk):
|
||||||
continue
|
continue
|
||||||
allm2m = getattr(obj, fk).all()
|
m2m_list = self._get_rel(obj, fk)
|
||||||
if getattr(obj, fk).exists():
|
if m2m_list:
|
||||||
summary_fields[fk] = []
|
summary_fields[fk] = []
|
||||||
for thisItem in allm2m:
|
for thisItem in m2m_list:
|
||||||
if fk == 'job':
|
if fk == 'job':
|
||||||
summary_fields['job_template'] = []
|
summary_fields['job_template'] = []
|
||||||
job_template_item = {}
|
job_template_item = {}
|
||||||
@@ -3710,9 +3720,6 @@ class ActivityStreamSerializer(BaseSerializer):
|
|||||||
fval = getattr(thisItem, field, None)
|
fval = getattr(thisItem, field, None)
|
||||||
if fval is not None:
|
if fval is not None:
|
||||||
thisItemDict[field] = fval
|
thisItemDict[field] = fval
|
||||||
if thisItemDict.get('id', None):
|
|
||||||
if thisItemDict.get('id', None) in [obj_dict.get('id', None) for obj_dict in summary_fields[fk]]:
|
|
||||||
continue
|
|
||||||
summary_fields[fk].append(thisItemDict)
|
summary_fields[fk].append(thisItemDict)
|
||||||
except ObjectDoesNotExist:
|
except ObjectDoesNotExist:
|
||||||
pass
|
pass
|
||||||
|
|||||||
@@ -2223,12 +2223,17 @@ class ActivityStreamAccess(BaseAccess):
|
|||||||
- custom inventory scripts
|
- custom inventory scripts
|
||||||
'''
|
'''
|
||||||
qs = self.model.objects.all()
|
qs = self.model.objects.all()
|
||||||
qs = qs.prefetch_related('organization', 'user', 'inventory', 'host', 'group', 'inventory_source',
|
qs = qs.prefetch_related('organization', 'user', 'inventory', 'host', 'group',
|
||||||
'inventory_update', 'credential', 'credential_type', 'team', 'project', 'project_update',
|
'inventory_update', 'credential', 'credential_type', 'team',
|
||||||
'job_template', 'job', 'ad_hoc_command',
|
'ad_hoc_command',
|
||||||
'notification_template', 'notification', 'label', 'role', 'actor',
|
'notification_template', 'notification', 'label', 'role', 'actor',
|
||||||
'schedule', 'custom_inventory_script', 'unified_job_template',
|
'schedule', 'custom_inventory_script', 'unified_job_template',
|
||||||
'workflow_job_template', 'workflow_job', 'workflow_job_template_node')
|
'workflow_job_template_node')
|
||||||
|
# FIXME: the following fields will be attached to the wrong object
|
||||||
|
# if they are included in prefetch_related because of
|
||||||
|
# https://github.com/django-polymorphic/django-polymorphic/issues/68
|
||||||
|
# 'job_template', 'job', 'project', 'project_update', 'workflow_job',
|
||||||
|
# 'inventory_source', 'workflow_job_template'
|
||||||
if self.user.is_superuser or self.user.is_system_auditor:
|
if self.user.is_superuser or self.user.is_system_auditor:
|
||||||
return qs.all()
|
return qs.all()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user