Parcourir la source

Closes #1669: Clicking "add an IP" from the prefix view will default to the first available IP within the prefix

Jeremy Stretch il y a 7 ans
Parent
commit
e50b7174bf
2 fichiers modifiés avec 11 ajouts et 1 suppressions
  1. 10 0
      netbox/ipam/models.py
  2. 1 1
      netbox/templates/ipam/inc/prefix_header.html

+ 10 - 0
netbox/ipam/models.py

@@ -304,6 +304,16 @@ class Prefix(CreatedUpdatedModel, CustomFieldModel):
 
         return available_ips
 
+    def get_first_available_ip(self):
+        """
+        Return the first available IP within the prefix (or None).
+        """
+        available_ips = self.get_available_ips()
+        if available_ips:
+            return '{}/{}'.format(next(available_ips.__iter__()), self.prefix.prefixlen)
+        else:
+            return None
+
     def get_utilization(self):
         """
         Determine the utilization of the prefix and return it as a percentage. For Prefixes with a status of

+ 1 - 1
netbox/templates/ipam/inc/prefix_header.html

@@ -23,7 +23,7 @@
 </div>
 <div class="pull-right">
     {% if perms.ipam.add_ipaddress %}
-		<a href="{% url 'ipam:ipaddress_add' %}?address={{ prefix.prefix }}{% if prefix.vrf %}&vrf={{ prefix.vrf.pk }}{% endif %}{% if prefix.tenant %}&tenant={{ prefix.tenant.pk }}{% endif %}" class="btn btn-success">
+		<a href="{% url 'ipam:ipaddress_add' %}?address={{ prefix.get_first_available_ip }}{% if prefix.vrf %}&vrf={{ prefix.vrf.pk }}{% endif %}{% if prefix.tenant %}&tenant={{ prefix.tenant.pk }}{% endif %}" class="btn btn-success">
 			<span class="fa fa-plus" aria-hidden="true"></span>
 			Add an IP Address
 		</a>