-
Notifications
You must be signed in to change notification settings - Fork 16
Peering table sortable columns clean #252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Jawny
wants to merge
8
commits into
nautobot:develop
Choose a base branch
from
Jawny:peering-table-sortable-columns-clean
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
332c6ff
[Feature] Added more columns to BGP Peerings table
Jawny 50f5bbd
[Feature] Added new columns and filters to Peerings Table
Jawny 14afede
[Chore] Fixed lint errors
Jawny 6943468
[Fix] Fixed import naming convention
Jawny db74869
Added change file
Jawny 3b66f86
Fixed lint errors
Jawny 09961de
Merge branch 'develop' of github.com:Jawny/nautobot-app-bgp-models in…
Jawny c9f7a61
[Fix] Addressed comments and added tests
Jawny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Added new columns to Peerings Table and added sorting + filtering functionality |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,253 @@ | ||
| """Custom table columns for nautobot_bgp_models.""" | ||
|
|
||
| import django_tables2 as tables | ||
| from django.db import models as django_models | ||
| from django.urls import reverse | ||
| from django.utils.html import format_html | ||
| from nautobot.core.templatetags.helpers import hyperlinked_object | ||
|
|
||
| from nautobot_bgp_models.models import PeerEndpoint | ||
|
|
||
|
|
||
| class BaseEndpointColumn(tables.Column): | ||
| """Base class for endpoint-related columns.""" | ||
|
|
||
| def __init__(self, *args, **kwargs): | ||
| """Initialize BaseEndpointColumn.""" | ||
| kwargs.setdefault("empty_values", ()) | ||
| super().__init__(*args, **kwargs) | ||
|
|
||
|
|
||
| class ADeviceColumn(BaseEndpointColumn): | ||
| """Column for A Side Device.""" | ||
|
|
||
| def render(self, record): # pylint: disable=arguments-renamed | ||
| """Render A Side Device.""" | ||
| if record.endpoint_a and record.endpoint_a.routing_instance and record.endpoint_a.routing_instance.device: | ||
| device = record.endpoint_a.routing_instance.device | ||
| return hyperlinked_object(device) | ||
| return None | ||
|
|
||
| def order(self, queryset, is_descending): | ||
Jawny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """Custom ordering for A Side Device.""" | ||
| # Use a subquery to get specifically the first endpoint's device name | ||
| first_endpoint_device = ( | ||
| PeerEndpoint.objects.filter(peering=django_models.OuterRef("pk")) | ||
| .order_by("pk") | ||
| .values("routing_instance__device__name")[:1] | ||
| ) | ||
|
|
||
| queryset = queryset.annotate(a_device_name=django_models.Subquery(first_endpoint_device)).order_by( | ||
| ("-" if is_descending else "") + "a_device_name" | ||
| ) | ||
|
|
||
| return queryset, True | ||
|
|
||
|
|
||
| class ZDeviceColumn(BaseEndpointColumn): | ||
| """Column for Z Side Device.""" | ||
|
|
||
| def render(self, record): # pylint: disable=arguments-renamed | ||
| """Render Z Side Device.""" | ||
| if record.endpoint_z and record.endpoint_z.routing_instance and record.endpoint_z.routing_instance.device: | ||
| device = record.endpoint_z.routing_instance.device | ||
| return hyperlinked_object(device) | ||
| return None | ||
|
|
||
| def order(self, queryset, is_descending): | ||
| """Custom ordering for Z Side Device.""" | ||
| # Use a subquery to get specifically the second endpoint's device name | ||
| second_endpoint_device = ( | ||
| PeerEndpoint.objects.filter(peering=django_models.OuterRef("pk")) | ||
| .order_by("pk") | ||
| .values("routing_instance__device__name")[1:2] | ||
| ) | ||
|
|
||
| queryset = queryset.annotate(z_device_name=django_models.Subquery(second_endpoint_device)).order_by( | ||
| ("-" if is_descending else "") + "z_device_name" | ||
| ) | ||
|
|
||
| return queryset, True | ||
|
|
||
|
|
||
| class AEndpointIPColumn(BaseEndpointColumn): | ||
| """Column for A Endpoint IP.""" | ||
|
|
||
| def render(self, record): # pylint: disable=arguments-renamed | ||
| """Render A Endpoint IP.""" | ||
| if record.endpoint_a and record.endpoint_a.local_ip: | ||
| return hyperlinked_object(record.endpoint_a) | ||
| return None | ||
|
|
||
| def order(self, queryset, is_descending): | ||
| """Custom ordering for A Endpoint IP.""" | ||
| first_endpoint_interface_ip = ( | ||
| PeerEndpoint.objects.filter(peering=django_models.OuterRef("pk")) | ||
| .order_by("pk") | ||
| .values( | ||
| "source_interface__ip_addresses__mask_length", | ||
| "source_interface__ip_addresses__ip_version", | ||
| "source_interface__ip_addresses__host", | ||
| )[:1] | ||
| ) | ||
|
|
||
| queryset = queryset.annotate( | ||
| a_endpoint_mask=django_models.Subquery( | ||
| first_endpoint_interface_ip.values("source_interface__ip_addresses__mask_length") | ||
| ), | ||
| a_endpoint_ip_version=django_models.Subquery( | ||
| first_endpoint_interface_ip.values("source_interface__ip_addresses__ip_version") | ||
| ), | ||
| a_endpoint_host=django_models.Subquery( | ||
| first_endpoint_interface_ip.values("source_interface__ip_addresses__host") | ||
| ), | ||
| ) | ||
|
|
||
| if is_descending: | ||
| order_fields = ["a_endpoint_mask", "-a_endpoint_ip_version", "-a_endpoint_host"] | ||
| else: | ||
| order_fields = ["-a_endpoint_mask", "a_endpoint_ip_version", "a_endpoint_host"] | ||
|
|
||
| return queryset.order_by(*order_fields), True | ||
|
|
||
|
|
||
| class ZEndpointIPColumn(BaseEndpointColumn): | ||
| """Column for Z Endpoint IP.""" | ||
|
|
||
| def render(self, record): # pylint: disable=arguments-renamed | ||
| """Render Z Endpoint IP.""" | ||
| if record.endpoint_z and record.endpoint_z.local_ip: | ||
| return hyperlinked_object(record.endpoint_z) | ||
| return None | ||
|
|
||
| def order(self, queryset, is_descending): | ||
| """Custom ordering for Z Endpoint IP.""" | ||
| second_endpoint_interface_ip = ( | ||
| PeerEndpoint.objects.filter(peering=django_models.OuterRef("pk")) | ||
| .order_by("pk") | ||
| .values( | ||
| "source_interface__ip_addresses__mask_length", | ||
| "source_interface__ip_addresses__ip_version", | ||
| "source_interface__ip_addresses__host", | ||
| )[1:2] | ||
| ) | ||
|
|
||
| queryset = queryset.annotate( | ||
| z_endpoint_mask=django_models.Subquery( | ||
| second_endpoint_interface_ip.values("source_interface__ip_addresses__mask_length") | ||
| ), | ||
| z_endpoint_ip_version=django_models.Subquery( | ||
| second_endpoint_interface_ip.values("source_interface__ip_addresses__ip_version") | ||
| ), | ||
| z_endpoint_host=django_models.Subquery( | ||
| second_endpoint_interface_ip.values("source_interface__ip_addresses__host") | ||
| ), | ||
| ) | ||
|
|
||
| if is_descending: | ||
| order_fields = ["z_endpoint_mask", "-z_endpoint_ip_version", "-z_endpoint_host"] | ||
| else: | ||
| order_fields = ["-z_endpoint_mask", "z_endpoint_ip_version", "z_endpoint_host"] | ||
|
|
||
| return queryset.order_by(*order_fields), True | ||
|
|
||
|
|
||
| class AASNColumn(BaseEndpointColumn): | ||
| """Column for A Side ASN.""" | ||
|
|
||
| def render(self, record): # pylint: disable=arguments-renamed | ||
| """Render A Side ASN using inherited autonomous system.""" | ||
| if record.endpoint_a: | ||
| asn, _, _ = record.endpoint_a.get_inherited_field("autonomous_system") | ||
| if asn: | ||
| return hyperlinked_object(asn) | ||
| return None | ||
|
|
||
| def order(self, queryset, is_descending): | ||
| """Custom ordering for A Side ASN.""" | ||
| first_endpoint_asn = ( | ||
| PeerEndpoint.objects.filter(peering=django_models.OuterRef("pk")) | ||
| .order_by("pk") | ||
| .values("routing_instance__autonomous_system__asn")[:1] | ||
| ) | ||
|
|
||
| queryset = queryset.annotate(a_side_asn_value=django_models.Subquery(first_endpoint_asn)) | ||
|
|
||
| order_field = "-a_side_asn_value" if is_descending else "a_side_asn_value" | ||
| return queryset.order_by(order_field), True | ||
|
|
||
|
|
||
| class ZASNColumn(BaseEndpointColumn): | ||
| """Column for Z Side ASN.""" | ||
|
|
||
| def render(self, record): # pylint: disable=arguments-renamed | ||
| """Render Z Side ASN using inherited autonomous system.""" | ||
| if record.endpoint_z: | ||
| asn, _, _ = record.endpoint_z.get_inherited_field("autonomous_system") | ||
| if asn: | ||
| url = reverse("plugins:nautobot_bgp_models:autonomoussystem", args=[asn.pk]) | ||
| return format_html('<a href="{}">{}</a>', url, asn.asn) | ||
| return None | ||
|
|
||
| def order(self, queryset, is_descending): | ||
| """Custom ordering for Z Side ASN.""" | ||
| second_endpoint_asn = ( | ||
| PeerEndpoint.objects.filter(peering=django_models.OuterRef("pk")) | ||
| .order_by("pk") | ||
| .values("routing_instance__autonomous_system__asn")[1:2] | ||
| ) | ||
|
|
||
| queryset = queryset.annotate(z_side_asn_value=django_models.Subquery(second_endpoint_asn)) | ||
|
|
||
| order_field = "-z_side_asn_value" if is_descending else "z_side_asn_value" | ||
| return queryset.order_by(order_field), True | ||
|
|
||
|
|
||
| class AProviderColumn(BaseEndpointColumn): | ||
| """Column for A Side Provider.""" | ||
|
|
||
| def render(self, record): # pylint: disable=arguments-renamed | ||
| """Render Provider A using inherited autonomous system.""" | ||
| if record.endpoint_a: | ||
| asn, _, _ = record.endpoint_a.get_inherited_field("autonomous_system") | ||
| if asn and asn.provider: | ||
| return hyperlinked_object(asn.provider) | ||
| return None | ||
|
|
||
| def order(self, queryset, is_descending): | ||
| """Custom ordering for A Side Provider.""" | ||
| first_endpoint_provider = ( | ||
| PeerEndpoint.objects.filter(peering=django_models.OuterRef("pk")) | ||
| .order_by("pk") | ||
| .values("routing_instance__autonomous_system__provider__name")[:1] | ||
| ) | ||
|
|
||
| queryset = queryset.annotate(a_side_provider_name=django_models.Subquery(first_endpoint_provider)) | ||
|
|
||
| order_field = "-a_side_provider_name" if is_descending else "a_side_provider_name" | ||
| return queryset.order_by(order_field), True | ||
|
|
||
|
|
||
| class ZProviderColumn(BaseEndpointColumn): | ||
| """Column for Z Side Provider.""" | ||
|
|
||
| def render(self, record): # pylint: disable=arguments-renamed | ||
| """Render Provider Z using inherited autonomous system.""" | ||
| if record.endpoint_z: | ||
| asn, _, _ = record.endpoint_z.get_inherited_field("autonomous_system") | ||
| if asn and asn.provider: | ||
| return hyperlinked_object(asn.provider) | ||
| return None | ||
|
|
||
| def order(self, queryset, is_descending): | ||
| """Custom ordering for Z Side Provider.""" | ||
| second_endpoint_provider = ( | ||
| PeerEndpoint.objects.filter(peering=django_models.OuterRef("pk")) | ||
| .order_by("pk") | ||
| .values("routing_instance__autonomous_system__provider__name")[1:2] | ||
| ) | ||
|
|
||
| queryset = queryset.annotate(z_side_provider_name=django_models.Subquery(second_endpoint_provider)) | ||
|
|
||
| order_field = "-z_side_provider_name" if is_descending else "z_side_provider_name" | ||
| return queryset.order_by(order_field), True | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.