def add(caller_id, address, mask): networks = [] for net in AvailableNetwork.objects.all(): networks.append(net.to_ipnetwork()) networks.sort() # Find duplicate ipnet = IPNetwork('%s/%d' % (address, mask)) for i in xrange(len(networks)): if ipnet.prefixlen > networks[i].prefixlen and ipnet > networks[ i].previous() and ipnet < networks[i].next(): raise CMException('network_exists') elif ipnet.prefixlen < networks[i].prefixlen and ipnet.previous( ) < networks[i] and ipnet.next() > networks[i]: raise CMException('network_exists') # Add new network new_net = AvailableNetwork() new_net.address = ipnet.network new_net.mask = mask if ipnet.is_private(): new_net.state = available_network_states['ok'] else: new_net.state = available_network_states['locked'] new_net.save()
def add(caller_id, address, mask): networks = [] for net in AvailableNetwork.objects.all(): networks.append(net.to_ipnetwork()) networks.sort() # Find duplicate ipnet = IPNetwork('%s/%d' % (address, mask)) for i in xrange(len(networks)): if ipnet.prefixlen > networks[i].prefixlen and ipnet > networks[i].previous() and ipnet < networks[i].next(): raise CMException('network_exists') elif ipnet.prefixlen < networks[i].prefixlen and ipnet.previous() < networks[i] and ipnet.next() > networks[i]: raise CMException('network_exists') # Add new network new_net = AvailableNetwork() new_net.address = ipnet.network new_net.mask = mask if ipnet.is_private(): new_net.state = available_network_states['ok'] else: new_net.state = available_network_states['locked'] new_net.save()