mirror of
				https://github.com/itsdave-de/msp.git
				synced 2025-11-04 03:11:00 -03:00 
			
		
		
		
	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:
		
						commit
						2d66a7662e
					
				@ -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):
 | 
			
		||||
 | 
			
		||||
@ -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",
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user