Merge pull request #28 from itsdave-de/hotfix/27-ip-network-usage-overview-not-sorting-correctly

Sort IPs in Usage Overview table
This commit is contained in:
itsdaveit 2022-09-06 17:15:01 +02:00 committed by GitHub
commit 2d66a7662e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 4 deletions

View File

@ -4,6 +4,7 @@
from __future__ import unicode_literals
import frappe
import socket
from ipaddress import IPv4Address, IPv4Network
from frappe.model.document import Document
@ -16,6 +17,7 @@ class IPNetwork(Document):
SELECT
ipa.name as ip_address_name,
ipa.ip_address,
ipa.protocol,
ito.name as it_object_name,
ito.title,
ito.type,
@ -35,15 +37,19 @@ class IPNetwork(Document):
used_ips.append({
'ip_address': ip_network_reserved_range.start,
'title': ip_network_reserved_range.type,
'type': 'DHCP Range Start'
'type': 'DHCP Range Start',
'protocol': ip_network_reserved_range.protocol
})
used_ips.append({
'ip_address': ip_network_reserved_range.end,
'title': ip_network_reserved_range.type,
'type': 'DHCP Range End'
'type': 'DHCP Range End',
'protocol': ip_network_reserved_range.protocol
})
return used_ips
# Sorting method is using inet_pton built in function which is used to convert IPs from string format to a packed, binary format to be able to compare them. It supports IPv4 and IPv6 IPs
# @see https://docs.python.org/3/library/socket.html#socket.inet_pton and https://stackoverflow.com/a/6545090 for more information
return sorted(used_ips, key=lambda item: socket.inet_pton(socket.AF_INET if item['protocol'] == 'IPv4' or not item['protocol'] else socket.AF_INET6 , item['ip_address']))
@frappe.whitelist()
def calculate_network_data(doc):

View File

@ -9,6 +9,7 @@
"start",
"end",
"type",
"protocol",
"description"
],
"fields": [
@ -35,12 +36,19 @@
"fieldname": "description",
"fieldtype": "Text Editor",
"label": "Description"
},
{
"default": "IPv4",
"fieldname": "protocol",
"fieldtype": "Select",
"label": "Protocol",
"options": "IPv4\nIPv6"
}
],
"index_web_pages_for_search": 1,
"istable": 1,
"links": [],
"modified": "2022-08-17 13:44:07.476976",
"modified": "2022-09-05 09:57:47.527108",
"modified_by": "Administrator",
"module": "MSP",
"name": "IP Network Reserved Range",