From ea644868a6c072c8131c9b3e16071e3a54569d73 Mon Sep 17 00:00:00 2001 From: Jason Yates Date: Wed, 12 Jan 2022 14:06:22 +0000 Subject: [PATCH 1/3] Adding asdot notation to ASN views Adds custom property to asn model to compute asdot notation if required. Updates asn view to show asdot notation if one exists in the format xxxxx (yyy.yyy) Adds a custom column renderer to asn table to display asdot notation if one exists --- netbox/ipam/models/ip.py | 8 ++++++++ netbox/ipam/tables/ip.py | 7 +++++++ netbox/templates/ipam/asn.html | 2 +- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/netbox/ipam/models/ip.py b/netbox/ipam/models/ip.py index 9c00a9068..c92b54537 100644 --- a/netbox/ipam/models/ip.py +++ b/netbox/ipam/models/ip.py @@ -130,6 +130,14 @@ class ASN(PrimaryModel): def get_absolute_url(self): return reverse('ipam:asn', args=[self.pk]) + @property + def asdot_notation(self): + # Return asdot notation for an ASN larger than 65535 + if self.asn > 65535: + return '{}.{}'.format(self.asn // 65536, self.asn % 65536) + else: + return None + @extras_features('custom_fields', 'custom_links', 'export_templates', 'tags', 'webhooks') class Aggregate(GetAvailablePrefixesMixin, PrimaryModel): diff --git a/netbox/ipam/tables/ip.py b/netbox/ipam/tables/ip.py index 3fddbf48e..988c89aa2 100644 --- a/netbox/ipam/tables/ip.py +++ b/netbox/ipam/tables/ip.py @@ -106,6 +106,13 @@ class ASNTable(BaseTable): asn = tables.Column( linkify=True ) + + def render_asn(self, value, record): + if record.asdot_notation: + return f'{value} ({record.asdot_notation})' + else: + return value + site_count = LinkedCountColumn( viewname='dcim:site_list', url_params={'asn_id': 'pk'}, diff --git a/netbox/templates/ipam/asn.html b/netbox/templates/ipam/asn.html index 53afd5ebb..85eaf8122 100644 --- a/netbox/templates/ipam/asn.html +++ b/netbox/templates/ipam/asn.html @@ -18,7 +18,7 @@ - + From 85f588e8c9965193b3b037fab1c65b473fd6665e Mon Sep 17 00:00:00 2001 From: Jason Yates Date: Wed, 12 Jan 2022 16:44:22 +0000 Subject: [PATCH 2/3] Updating page title to include asdot notation --- netbox/ipam/models/ip.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/netbox/ipam/models/ip.py b/netbox/ipam/models/ip.py index c92b54537..fa27aaf1b 100644 --- a/netbox/ipam/models/ip.py +++ b/netbox/ipam/models/ip.py @@ -125,7 +125,10 @@ class ASN(PrimaryModel): verbose_name_plural = 'ASNs' def __str__(self): - return f'AS{self.asn}' + if self.asn > 65535: + return 'AS{} ({}.{})'.format(self.asn, self.asn // 65536, self.asn % 65536) + else: + return f'AS{self.asn}' def get_absolute_url(self): return reverse('ipam:asn', args=[self.pk]) From 62fc7717c84a31a9a83cae4af25d26cbea128828 Mon Sep 17 00:00:00 2001 From: Jason Yates Date: Thu, 13 Jan 2022 04:58:51 +0000 Subject: [PATCH 3/3] Suggested changes * Updating asdot computation to use an fstring * Cleaning code. Custom property now returns either the ASN with ASDOT notation or just the ASN. asn_with_asdot can now be referenced in ASNTable & objet template. --- netbox/ipam/models/ip.py | 13 +++++-------- netbox/ipam/tables/ip.py | 5 +---- netbox/templates/ipam/asn.html | 2 +- 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/netbox/ipam/models/ip.py b/netbox/ipam/models/ip.py index fa27aaf1b..5cce88481 100644 --- a/netbox/ipam/models/ip.py +++ b/netbox/ipam/models/ip.py @@ -125,21 +125,18 @@ class ASN(PrimaryModel): verbose_name_plural = 'ASNs' def __str__(self): - if self.asn > 65535: - return 'AS{} ({}.{})'.format(self.asn, self.asn // 65536, self.asn % 65536) - else: - return f'AS{self.asn}' + return f'AS{self.asn_with_asdot}' def get_absolute_url(self): return reverse('ipam:asn', args=[self.pk]) @property - def asdot_notation(self): - # Return asdot notation for an ASN larger than 65535 + def asn_with_asdot(self): + # Return asn with asdot notation for an ASN larger than 65535 otherwise return the plain ASN if self.asn > 65535: - return '{}.{}'.format(self.asn // 65536, self.asn % 65536) + return f'{self.asn} ({self.asn // 65536}.{self.asn % 65536})' else: - return None + return self.asn @extras_features('custom_fields', 'custom_links', 'export_templates', 'tags', 'webhooks') diff --git a/netbox/ipam/tables/ip.py b/netbox/ipam/tables/ip.py index 988c89aa2..8efb1caf3 100644 --- a/netbox/ipam/tables/ip.py +++ b/netbox/ipam/tables/ip.py @@ -108,10 +108,7 @@ class ASNTable(BaseTable): ) def render_asn(self, value, record): - if record.asdot_notation: - return f'{value} ({record.asdot_notation})' - else: - return value + return record.asn_with_asdot site_count = LinkedCountColumn( viewname='dcim:site_list', diff --git a/netbox/templates/ipam/asn.html b/netbox/templates/ipam/asn.html index 85eaf8122..4a1ecda0d 100644 --- a/netbox/templates/ipam/asn.html +++ b/netbox/templates/ipam/asn.html @@ -18,7 +18,7 @@
AS Number{{ object.asn }}{{ object.asn }} {% if object.asdot_notation %}({{ object.asdot_notation }}){% endif %}
RIR
- +
AS Number{{ object.asn }} {% if object.asdot_notation %}({{ object.asdot_notation }}){% endif %}{{ object.asn_with_asdot }}
RIR