<s> from core . network . models import Network <EOL> def calc_networks ( network , nq = None ) : <EOL> network . update_network ( ) <EOL> eldars = [ ] <EOL> sub_networks = [ ] <EOL> if not nq : <EOL> nq = Network . objects . all ( ) <EOL> for pnet in nq . order_by ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> pnet . update_network ( ) <EOL> if pnet . pk == network . pk : <EOL> continue <EOL> if pnet . network . overlaps ( network . network ) : <EOL> if pnet . prefixlen > network . prefixlen : <EOL> sub_networks . append ( pnet ) <EOL> else : <EOL> eldars . append ( pnet ) <EOL> return eldars , sub_networks <EOL> def calc_parent ( network ) : <EOL> eldars , sub_net = calc_networks ( network ) <EOL> if not eldars : <EOL> return [ ] <EOL> parent = list ( reversed ( sorted ( eldars , key = lambda n : n . prefixlen ) ) ) [ <NUM_LIT:0> ] <EOL> return parent <EOL> def calc_parent_str ( network_str , ip_type ) : <EOL> network = Network ( network_str = network_str , ip_type = ip_type ) <EOL> return calc_parent ( network ) <EOL> def calc_networks_str ( network_str , ip_type ) : <EOL> network = Network ( network_str = network_str , ip_type = ip_type ) <EOL> return calc_networks ( network ) <EOL> def calc_top_level_networks ( site ) : <EOL> networks = list ( <EOL> site . network_set . order_by ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> ) <EOL> nq = Network . objects . filter ( site = site ) <EOL> tlns = [ ] <EOL> while True : <EOL> try : <EOL> cur = networks . pop ( <NUM_LIT:0> ) <EOL> except IndexError : <EOL> break <EOL> super_nets , sub_nets = calc_networks ( cur , nq = nq ) <EOL> if not super_nets : <EOL> tlns . append ( cur ) <EOL> for sn in sub_nets : <EOL> try : <EOL> networks . remove ( sn ) <EOL> except ValueError : <EOL> pass <EOL> def ncmp ( n1 , n2 ) : <EOL> pd = n1 . prefixlen - n2 . prefixlen <EOL> if pd != <NUM_LIT:0> : <EOL> return pd <EOL> n_u_d = n1 . ip_upper - n2 . ip_upper <EOL> if n_u_d != <NUM_LIT:0> : <EOL> return n_u_d <EOL> return n1 . ip_lower - n2 . ip_lower <EOL> return tlns </s>
