Skip to content

Commit 6b19f15

Browse files
Moves related ips to a tab (#12502)
* moves related ips to a tab #12233 * Refactor IP address templates to use a base template --------- Co-authored-by: jeremystretch <[email protected]>
1 parent 57156f0 commit 6b19f15

File tree

5 files changed

+53
-18
lines changed

5 files changed

+53
-18
lines changed

netbox/ipam/models/ip.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,14 @@ def get_next_available_ip(self):
783783
if available_ips:
784784
return next(iter(available_ips))
785785

786+
def get_related_ips(self):
787+
"""
788+
Return all IPAddresses belonging to the same VRF.
789+
"""
790+
return IPAddress.objects.exclude(address=str(self.address)).filter(
791+
vrf=self.vrf, address__net_contained_or_equal=str(self.address)
792+
)
793+
786794
def clean(self):
787795
super().clean()
788796

netbox/ipam/views.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -755,19 +755,9 @@ def get_extra_context(self, request, instance):
755755
# Limit to a maximum of 10 duplicates displayed here
756756
duplicate_ips_table = tables.IPAddressTable(duplicate_ips[:10], orderable=False)
757757

758-
# Related IP table
759-
related_ips = IPAddress.objects.restrict(request.user, 'view').exclude(
760-
address=str(instance.address)
761-
).filter(
762-
vrf=instance.vrf, address__net_contained_or_equal=str(instance.address)
763-
)
764-
related_ips_table = tables.IPAddressTable(related_ips, orderable=False)
765-
related_ips_table.configure(request)
766-
767758
return {
768759
'parent_prefixes_table': parent_prefixes_table,
769760
'duplicate_ips_table': duplicate_ips_table,
770-
'related_ips_table': related_ips_table,
771761
}
772762

773763

@@ -872,6 +862,24 @@ class IPAddressBulkDeleteView(generic.BulkDeleteView):
872862
table = tables.IPAddressTable
873863

874864

865+
@register_model_view(IPAddress, 'related_ips', path='related-ip-addresses')
866+
class IPAddressRelatedIPsView(generic.ObjectChildrenView):
867+
queryset = IPAddress.objects.prefetch_related('vrf__tenant', 'tenant')
868+
child_model = IPAddress
869+
table = tables.IPAddressTable
870+
filterset = filtersets.IPAddressFilterSet
871+
template_name = 'ipam/ipaddress/ip_addresses.html'
872+
tab = ViewTab(
873+
label=_('Related IPs'),
874+
badge=lambda x: x.get_related_ips().count(),
875+
weight=500,
876+
hide_if_empty=True,
877+
)
878+
879+
def get_children(self, request, parent):
880+
return parent.get_related_ips().restrict(request.user, 'view')
881+
882+
875883
#
876884
# VLAN groups
877885
#

netbox/templates/ipam/ipaddress.html

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,6 @@
33
{% load plugins %}
44
{% load render_table from django_tables2 %}
55

6-
{% block breadcrumbs %}
7-
{{ block.super }}
8-
{% if object.vrf %}
9-
<li class="breadcrumb-item"><a href="{% url 'ipam:ipaddress_list' %}?vrf_id={{ object.vrf.pk }}">{{ object.vrf }}</a></li>
10-
{% endif %}
11-
{% endblock %}
12-
136
{% block content %}
147
<div class="row">
158
<div class="col col-md-4">
@@ -116,7 +109,6 @@ <h5 class="card-header">
116109
{% if duplicate_ips_table.rows %}
117110
{% include 'inc/panel_table.html' with table=duplicate_ips_table heading='Duplicate IPs' panel_class='danger' %}
118111
{% endif %}
119-
{% include 'inc/panel_table.html' with table=related_ips_table heading='Related IPs' %}
120112
<div class="card">
121113
<h5 class="card-header">Services</h5>
122114
<div class="card-body htmx-container table-responsive"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{% extends 'generic/object.html' %}
2+
3+
{% block breadcrumbs %}
4+
{{ block.super }}
5+
{% if object.vrf %}
6+
<li class="breadcrumb-item"><a href="{% url 'ipam:ipaddress_list' %}?vrf_id={{ object.vrf.pk }}">{{ object.vrf }}</a></li>
7+
{% endif %}
8+
{% endblock %}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{% extends 'ipam/ipaddress/base.html' %}
2+
{% load helpers %}
3+
4+
{% block content %}
5+
{% include 'inc/table_controls_htmx.html' with table_modal="IPAddressTable_config" %}
6+
<form method="post">
7+
{% csrf_token %}
8+
<div class="card">
9+
<div class="card-body" id="object_list">
10+
{% include 'htmx/table.html' %}
11+
</div>
12+
</div>
13+
</form>
14+
{% endblock content %}
15+
16+
{% block modals %}
17+
{{ block.super }}
18+
{% table_config_form table %}
19+
{% endblock modals %}

0 commit comments

Comments
 (0)