Parcourir la source

Prettify IPv4 subnets in VPN templates, by removing the '/32' at the end

Baptiste Jonglez il y a 10 ans
Parent
commit
cf99080aaa

+ 0 - 0
coin/resources/templatetags/__init__.py


+ 22 - 0
coin/resources/templatetags/subnets.py

@@ -0,0 +1,22 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django import template
+from netaddr import IPNetwork
+
+
+register = template.Library()
+
+@register.filter
+def prettify(subnet):
+    """Prettify an IPv4 subnet by remove the subnet length when it is equal to /32
+    """
+    # Support IPSubnet objects, who have a IPNetwork as attribute
+    if hasattr(subnet, "inet") and isinstance(subnet.inet, IPNetwork):
+        subnet = subnet.inet
+    if isinstance(subnet, IPNetwork):
+        if subnet.version == 4 and subnet.prefixlen == 32:
+            return str(subnet.ip)
+        else:
+            return str(subnet)
+    return subnet

+ 3 - 1
coin/vpn/templates/vpn/vpn.html

@@ -1,5 +1,7 @@
 {% extends "base.html" %}
 
+{% load subnets %}
+
 {% block content %}
 <div class="row">
     <h2>Configuration du VPN</h2>
@@ -62,7 +64,7 @@
                 <tr>
                     <td class="center"><span class="label">Sous-réseaux</span></td>
                     <td>
-                        {% for subnet in object.ip_subnet.all %}{{ subnet }}<br/>{% endfor %}
+                        {% for subnet in object.ip_subnet.all %}{{ subnet|prettify }}<br/>{% endfor %}
                     </td>
                 </tr>
             </table>