mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
fixes #5314 - Fix config context rendering when multiple tags are assgined to an object
This commit is contained in:
@ -10,6 +10,7 @@
|
|||||||
### Bug Fixes
|
### Bug Fixes
|
||||||
|
|
||||||
* [#5271](https://github.com/netbox-community/netbox/issues/5271) - Fix auto-population of region field when editing a device
|
* [#5271](https://github.com/netbox-community/netbox/issues/5271) - Fix auto-population of region field when editing a device
|
||||||
|
* [#5314](https://github.com/netbox-community/netbox/issues/5314) - Fix config context rendering when multiple tags are assgined to an object
|
||||||
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
@ -60,7 +60,7 @@ class ConfigContextQuerySet(RestrictedQuerySet):
|
|||||||
Q(tenants=obj.tenant) | Q(tenants=None),
|
Q(tenants=obj.tenant) | Q(tenants=None),
|
||||||
Q(tags__slug__in=obj.tags.slugs()) | Q(tags=None),
|
Q(tags__slug__in=obj.tags.slugs()) | Q(tags=None),
|
||||||
is_active=True,
|
is_active=True,
|
||||||
).order_by('weight', 'name')
|
).order_by('weight', 'name').distinct()
|
||||||
|
|
||||||
if aggregate_data:
|
if aggregate_data:
|
||||||
return queryset.aggregate(
|
return queryset.aggregate(
|
||||||
@ -95,7 +95,7 @@ class ConfigContextModelQuerySet(RestrictedQuerySet):
|
|||||||
_data=EmptyGroupByJSONBAgg('data', ordering=['weight', 'name'])
|
_data=EmptyGroupByJSONBAgg('data', ordering=['weight', 'name'])
|
||||||
).values("_data")
|
).values("_data")
|
||||||
)
|
)
|
||||||
)
|
).distinct()
|
||||||
|
|
||||||
def _get_config_context_filters(self):
|
def _get_config_context_filters(self):
|
||||||
# Construct the set of Q objects for the specific object types
|
# Construct the set of Q objects for the specific object types
|
||||||
|
@ -75,6 +75,7 @@ class ConfigContextTest(TestCase):
|
|||||||
self.tenantgroup = TenantGroup.objects.create(name="Tenant Group")
|
self.tenantgroup = TenantGroup.objects.create(name="Tenant Group")
|
||||||
self.tenant = Tenant.objects.create(name="Tenant", group=self.tenantgroup)
|
self.tenant = Tenant.objects.create(name="Tenant", group=self.tenantgroup)
|
||||||
self.tag = Tag.objects.create(name="Tag", slug="tag")
|
self.tag = Tag.objects.create(name="Tag", slug="tag")
|
||||||
|
self.tag2 = Tag.objects.create(name="Tag2", slug="tag2")
|
||||||
|
|
||||||
self.device = Device.objects.create(
|
self.device = Device.objects.create(
|
||||||
name='Device 1',
|
name='Device 1',
|
||||||
@ -328,3 +329,37 @@ class ConfigContextTest(TestCase):
|
|||||||
|
|
||||||
annotated_queryset = VirtualMachine.objects.filter(name=virtual_machine.name).annotate_config_context_data()
|
annotated_queryset = VirtualMachine.objects.filter(name=virtual_machine.name).annotate_config_context_data()
|
||||||
self.assertEqual(virtual_machine.get_config_context(), annotated_queryset[0].get_config_context())
|
self.assertEqual(virtual_machine.get_config_context(), annotated_queryset[0].get_config_context())
|
||||||
|
|
||||||
|
def test_multiple_tags_return_distinct_objects(self):
|
||||||
|
"""
|
||||||
|
Tagged items use a generic relationship, which results in duplicate rows being returned when queried.
|
||||||
|
This is combatted by by appending distinct() to the config context querysets. This test creates a config
|
||||||
|
context assigned to two tags and ensures objects related by those same two tags result in only a single
|
||||||
|
config context record being returned.
|
||||||
|
|
||||||
|
See https://github.com/netbox-community/netbox/issues/5314
|
||||||
|
"""
|
||||||
|
tag_context = ConfigContext.objects.create(
|
||||||
|
name="tag",
|
||||||
|
weight=100,
|
||||||
|
data={
|
||||||
|
"tag": 1
|
||||||
|
}
|
||||||
|
)
|
||||||
|
tag_context.tags.add(self.tag)
|
||||||
|
tag_context.tags.add(self.tag2)
|
||||||
|
|
||||||
|
device = Device.objects.create(
|
||||||
|
name="Device 3",
|
||||||
|
site=self.site,
|
||||||
|
tenant=self.tenant,
|
||||||
|
platform=self.platform,
|
||||||
|
device_role=self.devicerole,
|
||||||
|
device_type=self.devicetype
|
||||||
|
)
|
||||||
|
device.tags.add(self.tag)
|
||||||
|
device.tags.add(self.tag2)
|
||||||
|
|
||||||
|
annotated_queryset = Device.objects.filter(name=device.name).annotate_config_context_data()
|
||||||
|
self.assertEqual(ConfigContext.objects.get_for_object(device).count(), 1)
|
||||||
|
self.assertEqual(device.get_config_context(), annotated_queryset[0].get_config_context())
|
||||||
|
Reference in New Issue
Block a user