Skip to content

Commit

Permalink
Changed the behaviour of Prefix table: allow sorting (#5452)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Timizuo <ebideritimizuo@gmail.com>
Co-authored-by: Hanlin Miao <46973263+HanlinMiao@users.noreply.github.com>
Co-authored-by: Glenn Matthews <glenn.matthews@networktocode.com>
  • Loading branch information
4 people committed Mar 21, 2024
1 parent f76ff6c commit f038c8f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 21 deletions.
1 change: 1 addition & 0 deletions changes/5452.changed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Changed the behavior of Prefix table: now they are sortable, and after sorting is applied, all hierarchy indentations are removed.
23 changes: 2 additions & 21 deletions nautobot/ipam/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,10 @@
{% if record.present_in_database %}{% utilization_graph record.get_utilization %}{% else %}&mdash;{% endif %}
"""

PREFIX_LINK = """
{% load helpers %}
{% for i in record.ancestors.count|as_range %}
<i class="mdi mdi-circle-small"></i>
{% endfor %}
<a href="\
{% if record.present_in_database %}\
{% url 'ipam:prefix' pk=record.pk %}\
{% else %}\
{% url 'ipam:prefix_add' %}\
?prefix={{ record }}&namespace={{ object.namespace.pk }}\
{% for loc in object.locations.all %}&locations={{ loc.pk }}{% endfor %}\
{% if object.tenant %}&tenant_group={{ object.tenant.tenant_group.pk }}&tenant={{ object.tenant.pk }}{% endif %}\
{% endif %}\
">{{ record.prefix }}</a>
"""

PREFIX_COPY_LINK = """
{% load helpers %}
{% for i in record.ancestors.count|as_range %}
<i class="mdi mdi-circle-small"></i>
{% endfor %}
{% tree_hierarchy_ui_representation record.ancestors.count|as_range table.order_by %}
<span class="hover_copy">
<a href="\
{% if record.present_in_database %}\
Expand Down Expand Up @@ -369,15 +351,14 @@ class PrefixTable(StatusTableMixin, RoleTableMixin, BaseTable):
namespace = tables.Column(linkify=True)
vlan = tables.Column(linkify=True, verbose_name="VLAN")
rir = tables.Column(linkify=True)
children = tables.Column(accessor="descendants_count")
children = tables.Column(accessor="descendants_count", orderable=False)
date_allocated = tables.DateTimeColumn()
location_count = LinkedCountColumn(
viewname="dcim:location_list", url_params={"prefixes": "pk"}, verbose_name="Locations"
)

class Meta(BaseTable.Meta):
model = Prefix
orderable = False
fields = (
"pk",
"prefix",
Expand Down
42 changes: 42 additions & 0 deletions nautobot/ipam/tests/test_tables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from django.test import TestCase

from nautobot.core.models.querysets import count_related
from nautobot.dcim.models.locations import Location
from nautobot.ipam.models import Prefix
from nautobot.ipam.tables import PrefixTable


class PrefixTableTestCase(TestCase):
def _validate_sorted_queryset_same_with_table_queryset(self, queryset, table_class, field_name):
with self.subTest(f"Assert sorting {table_class.__name__} on '{field_name}'"):
table = table_class(queryset, order_by=field_name)
table_queryset_data = table.data.data.values_list("pk", flat=True)
sorted_queryset = queryset.order_by(field_name).values_list("pk", flat=True)
self.assertEqual(list(table_queryset_data), list(sorted_queryset))

def test_prefix_table_sort(self):
"""Assert TreeNode model table are orderable."""
# Due to MySQL's lack of support for combining 'LIMIT' and 'ORDER BY' in a single query,
# hence this approach.
pk_list = Prefix.objects.all().values_list("pk", flat=True)[:20]
pk_list = [str(pk) for pk in pk_list]
queryset = Prefix.objects.filter(pk__in=pk_list)

# Assets model names
table_avail_fields = ["tenant", "vlan", "namespace"]
for table_field_name in table_avail_fields:
self._validate_sorted_queryset_same_with_table_queryset(queryset, PrefixTable, table_field_name)
self._validate_sorted_queryset_same_with_table_queryset(queryset, PrefixTable, f"-{table_field_name}")

# Assert `prefix`
table_queryset_data = PrefixTable(queryset, order_by="prefix").data.data.values_list("pk", flat=True)
prefix_queryset = queryset.order_by("network", "prefix_length").values_list("pk", flat=True)
self.assertEqual(list(table_queryset_data), list(prefix_queryset))
table_queryset_data = PrefixTable(queryset, order_by="-prefix").data.data.values_list("pk", flat=True)
prefix_queryset = queryset.order_by("-network", "-prefix_length").values_list("pk", flat=True)
self.assertEqual(list(table_queryset_data), list(prefix_queryset))

# Assets `location_count`
location_count_queryset = queryset.annotate(location_count=count_related(Location, "prefixes")).all()
self._validate_sorted_queryset_same_with_table_queryset(location_count_queryset, PrefixTable, "location_count")
self._validate_sorted_queryset_same_with_table_queryset(location_count_queryset, PrefixTable, "-location_count")

0 comments on commit f038c8f

Please sign in to comment.