<s> from django . core . exceptions import ObjectDoesNotExist , ValidationError <EOL> from django . shortcuts import get_object_or_404 , redirect <EOL> from django . shortcuts import render <EOL> from django . contrib import messages <EOL> from django . forms . util import ErrorList <EOL> from django . http import HttpResponse <EOL> from systems . models import System <EOL> from core . interface . static_intr . models import StaticInterface <EOL> from core . interface . static_intr . models import StaticIntrKeyValue <EOL> from core . interface . static_intr . forms import StaticInterfaceForm <EOL> from core . interface . static_intr . forms import FullStaticInterfaceForm <EOL> from core . interface . static_intr . forms import StaticInterfaceQuickForm <EOL> from core . interface . static_intr . forms import CombineForm <EOL> from core . keyvalue . utils import get_attrs , update_attrs , get_aa , get_docstrings <EOL> from core . keyvalue . utils import dict_to_kv <EOL> from core . range . models import Range <EOL> from core . network . utils import calc_parent_str <EOL> from mozdns . domain . models import Domain <EOL> from mozdns . address_record . models import AddressRecord <EOL> from mozdns . ptr . models import PTR <EOL> import re <EOL> import simplejson as json <EOL> def do_combine_a_ptr_to_interface ( <EOL> addr , ptr , system , mac_address = None , <EOL> interface = None , dhcp_hostname = None , <EOL> dhcp_domain_name = None , dhcp_domain_name_servers = None , <EOL> dhcp_filename = None ) : <EOL> if mac_address == '<STR_LIT>' or mac_address is None : <EOL> system . get_next_key_value_adapter ( ) <EOL> if ( addr . ip_str != ptr . ip_str or addr . fqdn != ptr . name or <EOL> addr . ip_type != ptr . ip_type ) : <EOL> raise ValidationError ( "<STR_LIT>" ) <EOL> intr = StaticInterface ( <EOL> label = addr . label , mac = mac_address , domain = addr . domain , <EOL> ip_str = addr . ip_str , ip_type = addr . ip_type , system = system ) <EOL> addr_deleted = False <EOL> ptr_deleted = False <EOL> addr . delete ( check_cname = False ) <EOL> addr_deleted = True <EOL> ptr . delete ( ) <EOL> ptr_deleted = True <EOL> intr . full_clean ( ) <EOL> intr . save ( ) <EOL> if interface : <EOL> from api_v3 . system_api import SystemResource <EOL> intr . update_attrs ( ) <EOL> adapter_type , primary , alias = SystemResource . extract_nic_attrs ( <EOL> interface ) <EOL> intr . attrs . primary = primary <EOL> intr . attrs . alias = alias <EOL> intr . attrs . interface_type = adapter_type <EOL> if dhcp_hostname : <EOL> intr . attrs . hostname = dhcp_hostname <EOL> if dhcp_filename : <EOL> intr . attrs . filename = dhcp_filename <EOL> if dhcp_domain_name : <EOL> intr . attrs . domain_name = dhcp_domain_name <EOL> if dhcp_domain_name_servers : <EOL> intr . attrs . domain_name_servers = dhcp_domain_name_servers <EOL> return intr , addr_deleted , ptr_deleted <EOL> def combine_a_ptr_to_interface ( request , addr_pk , ptr_pk ) : <EOL> """<STR_LIT>""" <EOL> addr = get_object_or_404 ( AddressRecord , pk = addr_pk ) <EOL> ptr = get_object_or_404 ( PTR , pk = ptr_pk ) <EOL> is_ajax = request . POST . get ( '<STR_LIT>' ) <EOL> system_hostname = request . POST . get ( '<STR_LIT>' ) <EOL> if is_ajax and system_hostname : <EOL> system = None <EOL> try : <EOL> system = System . objects . get ( hostname = system_hostname ) <EOL> except : <EOL> try : <EOL> system_hostname = re . sub ( '<STR_LIT>' , '<STR_LIT>' , <EOL> system_hostname ) <EOL> system = System . objects . get ( hostname = system_hostname ) <EOL> except : <EOL> system = None <EOL> if system : <EOL> try : <EOL> x = do_combine_a_ptr_to_interface ( <EOL> addr , ptr , system , mac_address = "<STR_LIT>" ) <EOL> ( intr , addr_deleted , ptr_deleted ) = x <EOL> except ValidationError , e : <EOL> return HttpResponse ( json . dumps ( { '<STR_LIT:success>' : False , '<STR_LIT:error>' : <EOL> e . messages [ <NUM_LIT:0> ] } ) ) <EOL> ret_dict = { } <EOL> ret_dict [ '<STR_LIT:success>' ] = True <EOL> ret_dict [ '<STR_LIT>' ] = system . hostname <EOL> ret_dict [ '<STR_LIT:id>' ] = system . id <EOL> return HttpResponse ( json . dumps ( ret_dict ) ) <EOL> else : <EOL> return HttpResponse ( json . dumps ( { '<STR_LIT:success>' : False , '<STR_LIT:error>' : <EOL> '<STR_LIT>' } ) ) <EOL> if request . method == "<STR_LIT:POST>" : <EOL> form = CombineForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> system = form . cleaned_data [ '<STR_LIT>' ] <EOL> try : <EOL> x = do_combine_a_ptr_to_interface ( addr , ptr , system ) <EOL> intr , addr_deleted , ptr_deleted = x <EOL> return redirect ( intr ) <EOL> except ValidationError , e : <EOL> form . errors [ '<STR_LIT>' ] = ErrorList ( e . messages ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : addr , <EOL> '<STR_LIT>' : ptr , <EOL> '<STR_LIT>' : form <EOL> } ) <EOL> else : <EOL> form = CombineForm ( ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT>' : addr , <EOL> '<STR_LIT>' : ptr , <EOL> } ) <EOL> def create_no_system_static_interface ( request ) : <EOL> if request . method == "<STR_LIT:POST>" : <EOL> interface_form = FullStaticInterfaceForm ( request . POST ) <EOL> try : <EOL> if not interface_form . is_valid ( ) : <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : interface_form , <EOL> } ) <EOL> intr = interface_form . save ( ) <EOL> except ValidationError , e : <EOL> interface_form . _errors [ '<STR_LIT>' ] = ErrorList ( e . messages ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : interface_form , <EOL> } ) <EOL> return redirect ( intr . system . get_absolute_url ( ) ) <EOL> else : <EOL> initial = { } <EOL> if '<STR_LIT>' in request . GET and '<STR_LIT>' in request . GET : <EOL> ip_str = request . GET [ '<STR_LIT>' ] <EOL> ip_type = request . GET [ '<STR_LIT>' ] <EOL> network = calc_parent_str ( ip_str , ip_type ) <EOL> if network and network . vlan and network . site : <EOL> expected_name = "<STR_LIT>" . format ( <EOL> network . vlan . name , network . site . get_site_path ( ) ) <EOL> try : <EOL> domain = Domain . objects . get ( name = expected_name ) <EOL> except ObjectDoesNotExist , e : <EOL> domain = None <EOL> if domain : <EOL> initial [ '<STR_LIT>' ] = { '<STR_LIT>' : ip_str , '<STR_LIT>' : domain , <EOL> '<STR_LIT>' : ip_type } <EOL> else : <EOL> initial [ '<STR_LIT>' ] = { '<STR_LIT>' : ip_str , '<STR_LIT>' : ip_type } <EOL> interface_form = FullStaticInterfaceForm ( ** initial ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : interface_form , <EOL> } ) <EOL> def detail_static_interface ( reqeust , intr_pk ) : <EOL> intr = get_object_or_404 ( StaticInterface , pk = intr_pk ) <EOL> system = intr . system <EOL> return redirect ( system ) <EOL> def delete_static_interface ( reqeust , intr_pk ) : <EOL> intr = get_object_or_404 ( StaticInterface , pk = intr_pk ) <EOL> system = intr . system <EOL> try : <EOL> intr . delete ( ) <EOL> except ValidationError : <EOL> pass <EOL> return redirect ( system ) <EOL> def delete_attr ( request , attr_pk ) : <EOL> """<STR_LIT>""" <EOL> attr = get_object_or_404 ( StaticIntrKeyValue , pk = attr_pk ) <EOL> attr . delete ( ) <EOL> return HttpResponse ( "<STR_LIT>" ) <EOL> def edit_static_interface ( request , intr_pk ) : <EOL> intr = get_object_or_404 ( StaticInterface , pk = intr_pk ) <EOL> system = intr . system <EOL> attrs = intr . staticintrkeyvalue_set . all ( ) <EOL> aa = get_aa ( StaticIntrKeyValue ( ) ) <EOL> docs = get_docstrings ( StaticIntrKeyValue ( ) ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> interface_form = StaticInterfaceForm ( request . POST , instance = intr ) <EOL> if interface_form . is_valid ( ) : <EOL> try : <EOL> kv = None <EOL> kv = get_attrs ( request . POST ) <EOL> update_attrs ( kv , attrs , StaticIntrKeyValue , intr ) <EOL> intr = interface_form . save ( ) <EOL> intr . clean ( ) <EOL> intr . save ( ) <EOL> except ValidationError , e : <EOL> interface_form . _errors [ '<STR_LIT>' ] = ErrorList ( e . messages ) <EOL> if kv : <EOL> attrs = dict_to_kv ( kv , StaticIntrKeyValue ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : interface_form , <EOL> '<STR_LIT>' : intr , <EOL> '<STR_LIT>' : attrs , <EOL> '<STR_LIT>' : json . dumps ( aa ) , <EOL> '<STR_LIT>' : docs , <EOL> '<STR_LIT>' : '<STR_LIT>' . format ( <EOL> system ) , <EOL> '<STR_LIT>' : intr . domain <EOL> } ) <EOL> else : <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : interface_form , <EOL> '<STR_LIT>' : intr , <EOL> '<STR_LIT>' : attrs , <EOL> '<STR_LIT>' : json . dumps ( aa ) , <EOL> '<STR_LIT>' : docs , <EOL> '<STR_LIT>' : '<STR_LIT>' . format ( <EOL> system ) , <EOL> '<STR_LIT>' : intr . domain <EOL> } ) <EOL> messages . success ( request , "<STR_LIT>" ) <EOL> return redirect ( intr . get_edit_url ( ) ) <EOL> else : <EOL> interface_form = StaticInterfaceForm ( instance = intr ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : interface_form , <EOL> '<STR_LIT>' : intr , <EOL> '<STR_LIT>' : attrs , <EOL> '<STR_LIT>' : json . dumps ( aa ) , <EOL> '<STR_LIT>' : docs , <EOL> '<STR_LIT>' : '<STR_LIT>' . format ( system ) , <EOL> '<STR_LIT>' : intr . domain <EOL> } ) <EOL> def create_static_interface ( request , system_pk ) : <EOL> system = get_object_or_404 ( System , pk = system_pk ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> interface_form = StaticInterfaceForm ( request . POST ) <EOL> interface_form . instance . system = system <EOL> a , ptr , r = None , None , None <EOL> if interface_form . is_valid ( ) : <EOL> try : <EOL> intr = interface_form . instance <EOL> intr . system = system <EOL> intr . full_clean ( ) <EOL> intr . save ( ) <EOL> except ValidationError , e : <EOL> interface_form . _errors [ '<STR_LIT>' ] = ErrorList ( e . messages ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : interface_form , <EOL> '<STR_LIT>' : '<STR_LIT>' . format ( <EOL> system ) <EOL> } ) <EOL> else : <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : interface_form , <EOL> '<STR_LIT>' : '<STR_LIT>' . format ( system ) <EOL> } ) <EOL> messages . success ( request , "<STR_LIT>" ) <EOL> return redirect ( system ) <EOL> else : <EOL> interface_form = StaticInterfaceForm ( ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : interface_form , <EOL> '<STR_LIT>' : '<STR_LIT>' . format ( system ) <EOL> } ) <EOL> def quick_create ( request , system_pk ) : <EOL> system = get_object_or_404 ( System , pk = system_pk ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> interface_form = StaticInterfaceQuickForm ( request . POST ) <EOL> a , ptr , r = None , None , None <EOL> if interface_form . is_valid ( ) : <EOL> try : <EOL> if '<STR_LIT:label>' in interface_form . cleaned_data : <EOL> label = interface_form . cleaned_data [ '<STR_LIT:label>' ] <EOL> else : <EOL> label = "<STR_LIT>" <EOL> mrange_pk = interface_form . cleaned_data [ '<STR_LIT>' ] <EOL> mrange = get_object_or_404 ( Range , pk = mrange_pk ) <EOL> network = mrange . network <EOL> ip_type = network . ip_type <EOL> vlan = network . vlan <EOL> site = network . site <EOL> networks = [ ] <EOL> for network in vlan . network_set . all ( ) : <EOL> if not network . site : <EOL> continue <EOL> if network . site . get_site_path ( ) == site . get_site_path ( ) : <EOL> networks . append ( network ) <EOL> if not networks : <EOL> raise ValidationError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> ip = mrange . get_next_ip ( ) <EOL> if ip is None : <EOL> raise ValidationError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( site . name , vlan . name , <EOL> mrange . start_str , mrange . end_str ) ) <EOL> expected_name = "<STR_LIT>" . format ( <EOL> vlan . name , site . get_site_path ( ) ) <EOL> print "<STR_LIT>" . format ( expected_name ) <EOL> try : <EOL> domain = Domain . objects . get ( name = expected_name ) <EOL> except ObjectDoesNotExist , e : <EOL> raise ValidationError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . <EOL> format ( expected_name ) ) <EOL> intr = StaticInterface ( label = label , domain = domain , <EOL> ip_str = str ( ip ) , <EOL> ip_type = ip_type , system = system ) <EOL> intr . full_clean ( ) <EOL> intr . save ( ) <EOL> except ValidationError , e : <EOL> interface_form . _errors [ '<STR_LIT>' ] = ErrorList ( e . messages ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : interface_form , <EOL> '<STR_LIT>' : "<STR_LIT>" <EOL> "<STR_LIT>" . format ( system ) <EOL> } ) <EOL> else : <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : interface_form , <EOL> '<STR_LIT>' : '<STR_LIT>' . format ( <EOL> system ) <EOL> } ) <EOL> messages . success ( request , "<STR_LIT>" ) <EOL> return redirect ( system ) <EOL> else : <EOL> interface_form = StaticInterfaceQuickForm ( ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : interface_form , <EOL> '<STR_LIT>' : '<STR_LIT>' . format ( <EOL> system ) <EOL> } ) </s>
