<s> from django . db . models import Q <EOL> from django . shortcuts import get_object_or_404 <EOL> from core . site . models import Site <EOL> from core . vlan . models import Vlan <EOL> from core . network . models import Network <EOL> from core . utils import overlap , ip_to_int <EOL> def pks_to_objs ( pks , Klass ) : <EOL> return map ( lambda pk : get_object_or_404 ( Klass , pk = pk ) , pks ) <EOL> UN = ( <EOL> ( Q ( ip_type = '<STR_LIT:4>' ) & Q ( prefixlen__gte = <NUM_LIT:20> ) ) | <EOL> ( Q ( ip_type = '<STR_LIT>' ) & Q ( prefixlen = <NUM_LIT:64> ) ) <EOL> ) <EOL> def calculate_filters ( choice_type , choice_pk ) : <EOL> """<STR_LIT>""" <EOL> if choice_type == '<STR_LIT>' : <EOL> network = get_object_or_404 ( Network , pk = choice_pk ) <EOL> def filter_network ( present_pks ) : <EOL> return [ network ] <EOL> def filter_site ( present_pks ) : <EOL> return [ network . site ] if network . site else [ ] <EOL> def filter_vlan ( present_pks ) : <EOL> return [ network . vlan ] if network . vlan else [ ] <EOL> elif choice_type == '<STR_LIT>' : <EOL> def filter_network ( present_pks ) : <EOL> """<STR_LIT>""" <EOL> site_network_pks = get_object_or_404 ( <EOL> Site , pk = choice_pk <EOL> ) . network_set . filter ( UN ) . values_list ( '<STR_LIT>' , flat = True ) <EOL> net_pks = set ( present_pks ) . intersection ( set ( site_network_pks ) ) <EOL> return pks_to_objs ( net_pks , Network ) <EOL> def filter_site ( present_pks ) : <EOL> return [ get_object_or_404 ( Site , pk = choice_pk ) ] <EOL> def filter_vlan ( present_pks ) : <EOL> vlans = pks_to_objs ( present_pks , Vlan ) <EOL> def is_in_site ( vlan ) : <EOL> return vlan . network_set . filter ( <EOL> site__pk = choice_pk ) . filter ( UN ) . exists ( ) <EOL> return filter ( is_in_site , vlans ) <EOL> elif choice_type == '<STR_LIT>' : <EOL> vlan = get_object_or_404 ( Vlan , pk = choice_pk ) <EOL> def filter_network ( present_pks ) : <EOL> net_pks = vlan . network_set . filter ( UN ) . values_list ( '<STR_LIT>' , flat = True ) <EOL> net_pks = set ( present_pks ) . intersection ( set ( net_pks ) ) <EOL> return pks_to_objs ( net_pks , Network ) <EOL> def filter_site ( present_pks ) : <EOL> networks = vlan . network_set . filter ( UN ) . filter ( ~ Q ( site = None ) ) <EOL> network_site_pks = networks . values_list ( '<STR_LIT>' , flat = True ) <EOL> site_pks = set ( present_pks ) . intersection ( set ( network_site_pks ) ) <EOL> return pks_to_objs ( site_pks , Site ) <EOL> def filter_vlan ( present_pks ) : <EOL> return [ vlan ] <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return filter_network , filter_site , filter_vlan <EOL> def label_value_maker ( ) : <EOL> """<STR_LIT>""" <EOL> def format_network ( networks ) : <EOL> return list ( <EOL> { '<STR_LIT:label>' : n . network_str , '<STR_LIT:value>' : n . pk } for n in networks <EOL> ) <EOL> def format_site ( sites ) : <EOL> return list ( <EOL> { '<STR_LIT:label>' : s . full_name , '<STR_LIT:value>' : s . pk } for s in sites <EOL> ) <EOL> def format_vlan ( vlans ) : <EOL> return list ( <EOL> { '<STR_LIT:label>' : "<STR_LIT>" . format ( v . name , v . number ) , '<STR_LIT:value>' : v . pk } <EOL> for v in vlans <EOL> ) <EOL> return format_network , format_site , format_vlan <EOL> def calc_template_ranges ( network ) : <EOL> """<STR_LIT>""" <EOL> network . update_network ( ) <EOL> dhcp_scope = network . calc_dhcp_scope_name ( ) <EOL> nbase = network . network . network <EOL> name_fragment = calc_name_fragment ( network ) <EOL> if network . ip_type == '<STR_LIT:4>' : <EOL> if network . prefixlen <= <NUM_LIT> and network . prefixlen >= <NUM_LIT:20> : <EOL> template_ranges = [ <EOL> { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:start>' : str ( nbase + <NUM_LIT:1> ) , <EOL> '<STR_LIT:end>' : str ( nbase + <NUM_LIT:15> ) , <EOL> '<STR_LIT>' : dhcp_scope , <EOL> '<STR_LIT>' : name_fragment <EOL> } , <EOL> { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:start>' : str ( nbase + <NUM_LIT:16> ) , <EOL> '<STR_LIT:end>' : str ( nbase + <NUM_LIT> ) , <EOL> '<STR_LIT>' : dhcp_scope , <EOL> '<STR_LIT>' : name_fragment <EOL> } , <EOL> { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:start>' : str ( nbase + <NUM_LIT> ) , <EOL> '<STR_LIT:end>' : str ( nbase + <NUM_LIT> ) , <EOL> '<STR_LIT>' : dhcp_scope , <EOL> '<STR_LIT>' : name_fragment <EOL> } , <EOL> { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:start>' : str ( nbase + <NUM_LIT> ) , <EOL> '<STR_LIT:end>' : str ( nbase + <NUM_LIT> ) , <EOL> '<STR_LIT>' : dhcp_scope , <EOL> '<STR_LIT>' : name_fragment <EOL> } , <EOL> { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:start>' : str ( nbase + <NUM_LIT> ) , <EOL> '<STR_LIT:end>' : str ( nbase + <NUM_LIT> ) , <EOL> '<STR_LIT>' : dhcp_scope , <EOL> '<STR_LIT>' : name_fragment <EOL> } , <EOL> { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:start>' : str ( nbase + <NUM_LIT> ) , <EOL> '<STR_LIT:end>' : str ( nbase + <NUM_LIT:255> ) , <EOL> '<STR_LIT>' : dhcp_scope , <EOL> '<STR_LIT>' : name_fragment <EOL> } <EOL> ] <EOL> if network . prefixlen < <NUM_LIT> : <EOL> template_ranges . append ( { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:start>' : str ( nbase + <NUM_LIT> ) , <EOL> '<STR_LIT:end>' : str ( network . network . broadcast - <NUM_LIT:1> ) , <EOL> '<STR_LIT>' : dhcp_scope , <EOL> '<STR_LIT>' : name_fragment <EOL> } ) <EOL> elif network . prefixlen > <NUM_LIT> and network . prefixlen < <NUM_LIT> : <EOL> template_ranges = [ { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:start>' : str ( nbase + <NUM_LIT:0> ) , <EOL> '<STR_LIT:end>' : str ( nbase + <NUM_LIT:3> ) , <EOL> '<STR_LIT>' : dhcp_scope , <EOL> '<STR_LIT>' : name_fragment <EOL> } ] <EOL> if network . prefixlen > <NUM_LIT:30> : <EOL> template_ranges . append ( { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:start>' : str ( nbase + <NUM_LIT:5> ) , <EOL> '<STR_LIT:end>' : str ( network . network . broadcast - <NUM_LIT:1> ) , <EOL> '<STR_LIT>' : dhcp_scope , <EOL> '<STR_LIT>' : name_fragment <EOL> } ) <EOL> else : <EOL> template_ranges = [ ] <EOL> return template_ranges <EOL> elif network . ip_type == '<STR_LIT>' and network . prefixlen == <NUM_LIT:64> : <EOL> return [ <EOL> { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:start>' : str ( nbase + <NUM_LIT:1> ) , <EOL> '<STR_LIT:end>' : str ( nbase + int ( '<STR_LIT>' , <NUM_LIT:16> ) ) , <EOL> '<STR_LIT>' : dhcp_scope , <EOL> '<STR_LIT>' : name_fragment <EOL> } , <EOL> { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:start>' : str ( nbase + int ( '<STR_LIT>' , <NUM_LIT:16> ) ) , <EOL> '<STR_LIT:end>' : str ( nbase + int ( '<STR_LIT>' , <NUM_LIT:16> ) ) , <EOL> '<STR_LIT>' : dhcp_scope , <EOL> '<STR_LIT>' : name_fragment <EOL> } , <EOL> { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:start>' : str ( nbase + int ( '<STR_LIT>' , <NUM_LIT:16> ) ) , <EOL> '<STR_LIT:end>' : str ( nbase + int ( '<STR_LIT>' , <NUM_LIT:16> ) ) , <EOL> '<STR_LIT>' : dhcp_scope , <EOL> '<STR_LIT>' : name_fragment <EOL> } <EOL> ] <EOL> else : <EOL> return [ ] <EOL> def integrate_real_ranges ( network , template_ranges ) : <EOL> """<STR_LIT>""" <EOL> real_ranges = network . range_set . all ( ) <EOL> if not real_ranges : <EOL> return template_ranges <EOL> name_fragment = calc_name_fragment ( network ) <EOL> dhcp_scope = network . calc_dhcp_scope_name ( ) <EOL> filtered_ranges = [ ] <EOL> no_ovlp_templates = [ ] <EOL> for tr in template_ranges : <EOL> ol = False <EOL> for r in real_ranges : <EOL> ol = overlap ( <EOL> ( r . start_str , r . end_str ) , ( tr [ '<STR_LIT:start>' ] , tr [ '<STR_LIT:end>' ] ) , <EOL> ip_type = network . ip_type , cast_to_int = True <EOL> ) <EOL> if ol : <EOL> break <EOL> if not ol : <EOL> no_ovlp_templates . append ( tr ) <EOL> for r in real_ranges : <EOL> filtered_ranges . append ( { <EOL> '<STR_LIT:name>' : r . name , <EOL> '<STR_LIT>' : r . name , <EOL> '<STR_LIT:start>' : r . start_str , <EOL> '<STR_LIT:end>' : r . end_str , <EOL> '<STR_LIT>' : dhcp_scope , <EOL> '<STR_LIT>' : name_fragment , <EOL> '<STR_LIT>' : r . pk <EOL> } ) <EOL> filtered_ranges += no_ovlp_templates <EOL> return sorted ( <EOL> filtered_ranges , key = lambda r : ip_to_int ( r [ '<STR_LIT:start>' ] , network . ip_type ) <EOL> ) <EOL> def calc_name_fragment ( network , base_name = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if network . site : <EOL> base_name = network . site . full_name <EOL> if network . vlan : <EOL> base_name = '<STR_LIT:.>' . join ( [ network . vlan . name , base_name ] ) <EOL> return base_name </s>
