 from django . core . exceptions import ValidationError <EOL> from django . shortcuts import get_object_or_404 <EOL> from django . shortcuts import render <EOL> from django . http import HttpResponse <EOL> from core . utils import int_to_ip , resolve_ip_type <EOL> from core . range . forms import RangeForm <EOL> from core . range . utils import range_usage <EOL> from core . range . ip_choosing_utils import ( <EOL> calculate_filters , label_value_maker , calc_template_ranges , <EOL> integrate_real_ranges , UN <EOL> ) <EOL> from core . range . models import Range <EOL> from core . site . models import Site <EOL> from core . vlan . models import Vlan <EOL> from core . network . models import Network <EOL> from mozdns . ip . models import ipv6_to_longs <EOL> from core . views import CoreDeleteView , CoreDetailView <EOL> from core . views import CoreCreateView , CoreUpdateView , CoreListView <EOL> import ipaddr <EOL> import simplejson as json <EOL> class RangeView ( object ) : <EOL> model = Range <EOL> form_class = RangeForm <EOL> queryset = Range . objects . all ( ) <EOL> class RangeDeleteView ( RangeView , CoreDeleteView ) : <EOL> pass <EOL> class RangeCreateView ( RangeView , CoreCreateView ) : <EOL> def get_form ( self , form_class ) : <EOL> if not self . request . POST : <EOL> initial = { } <EOL> initial . update ( dict ( self . request . GET . items ( ) ) ) <EOL> return form_class ( initial = initial ) <EOL> else : <EOL> return super ( RangeCreateView , self ) . get_form ( form_class ) <EOL> class RangeUpdateView ( RangeView , CoreUpdateView ) : <EOL> template_name = "<STR_LIT>" <EOL> class RangeListView ( RangeView , CoreListView ) : <EOL> template_name = "<STR_LIT>" <EOL> class RangeDetailView ( RangeView , CoreDetailView ) : <EOL> template_name = '<STR_LIT>' <EOL> def get_context_data ( self , ** kwargs ) : <EOL> context = super ( RangeDetailView , self ) . get_context_data ( <EOL> ** kwargs ) <EOL> context [ '<STR_LIT>' ] = "<STR_LIT>" . format ( <EOL> self . form_class . Meta . model . __name__ <EOL> ) <EOL> if self . extra_context : <EOL> context = dict ( context . items ( ) + self . extra_context . items ( ) ) <EOL> return context <EOL> def range_usage_text ( request ) : <EOL> start = request . GET . get ( '<STR_LIT:start>' , None ) <EOL> end = request . GET . get ( '<STR_LIT:end>' , None ) <EOL> format = request . GET . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if not ( start and end ) : <EOL> return HttpResponse ( json . dumps ( { <EOL> '<STR_LIT:success>' : False , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) ) <EOL> get_objects = request . GET . get ( '<STR_LIT>' , False ) <EOL> if start . find ( '<STR_LIT::>' ) > - <NUM_LIT:1> : <EOL> ip_type = '<STR_LIT>' <EOL> else : <EOL> ip_type = '<STR_LIT:4>' <EOL> try : <EOL> usage_data = range_usage ( start , end , ip_type , get_objects ) <EOL> except ( ValidationError , ipaddr . AddressValueError ) , e : <EOL> return HttpResponse ( <EOL> json . dumps ( { <EOL> '<STR_LIT>' : str ( e ) , <EOL> '<STR_LIT:success>' : False <EOL> } ) ) <EOL> if format == '<STR_LIT>' : <EOL> usage_data [ '<STR_LIT>' ] = map ( lambda x : ( int_to_ip ( x [ <NUM_LIT:0> ] , ip_type ) , <EOL> int_to_ip ( x [ <NUM_LIT:1> ] , ip_type ) ) , <EOL> usage_data [ '<STR_LIT>' ] ) <EOL> usage_data [ '<STR_LIT:success>' ] = True <EOL> return HttpResponse ( json . dumps ( usage_data ) ) <EOL> def range_usage_ajax ( request ) : <EOL> start = request . GET . get ( '<STR_LIT:start>' , None ) <EOL> end = request . GET . get ( '<STR_LIT:end>' , None ) <EOL> start_ip_type , _ = resolve_ip_type ( start ) <EOL> end_ip_type , _ = resolve_ip_type ( end ) <EOL> errors = None <EOL> if start_ip_type != end_ip_type or start_ip_type is None : <EOL> errors = "<STR_LIT>" <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : errors , <EOL> } ) <EOL> rusage = range_usage ( start , end , start_ip_type , get_objects = True ) <EOL> def translate_ip ( ip_i , * args ) : <EOL> return int_to_ip ( ip_i , start_ip_type ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : errors , <EOL> '<STR_LIT:start>' : start , <EOL> '<STR_LIT:end>' : end , <EOL> '<STR_LIT>' : int_to_ip ( start , start_ip_type ) , <EOL> '<STR_LIT>' : int_to_ip ( end , end_ip_type ) , <EOL> '<STR_LIT>' : rusage , <EOL> '<STR_LIT>' : translate_ip <EOL> } ) <EOL> def range_detail ( request , range_pk ) : <EOL> mrange = get_object_or_404 ( Range , pk = range_pk ) <EOL> attrs = mrange . keyvalue_set . all ( ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : mrange , <EOL> '<STR_LIT>' : attrs , <EOL> } ) <EOL> def redirect_to_range_from_ip ( request ) : <EOL> ip_str = request . GET . get ( '<STR_LIT>' ) <EOL> ip_type = request . GET . get ( '<STR_LIT>' ) <EOL> if not ( ip_str and ip_type ) : <EOL> return HttpResponse ( json . dumps ( { '<STR_LIT>' : "<STR_LIT>" } ) ) <EOL> if ip_type == '<STR_LIT:4>' : <EOL> try : <EOL> ip_upper , ip_lower = <NUM_LIT:0> , int ( ipaddr . IPv4Address ( ip_str ) ) <EOL> except ipaddr . AddressValueError : <EOL> return HttpResponse ( <EOL> json . dumps ( { '<STR_LIT:success>' : False , '<STR_LIT:message>' : "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( ip_str ) } ) ) <EOL> else : <EOL> try : <EOL> ip_upper , ip_lower = ipv6_to_longs ( ip_str ) <EOL> except ValidationError : <EOL> return HttpResponse ( json . dumps ( { '<STR_LIT:success>' : False , <EOL> '<STR_LIT:message>' : '<STR_LIT>' } ) ) <EOL> range_ = Range . objects . filter ( start_upper__lte = ip_upper , <EOL> start_lower__lte = ip_lower , <EOL> end_upper__gte = ip_upper , <EOL> end_lower__gte = ip_lower ) 
 if not len ( range_ ) == <NUM_LIT:1> : 
 from django . core . exceptions import ValidationError <EOL> from django . shortcuts import get_object_or_404 <EOL> from django . shortcuts import render <EOL> from django . http import HttpResponse <EOL> from core . utils import int_to_ip , resolve_ip_type <EOL> from core . range . forms import RangeForm <EOL> from core . range . utils import range_usage <EOL> from core . range . ip_choosing_utils import ( <EOL> calculate_filters , label_value_maker , calc_template_ranges , <EOL> integrate_real_ranges , UN <EOL> ) <EOL> from core . range . models import Range <EOL> from core . site . models import Site <EOL> from core . vlan . models import Vlan <EOL> from core . network . models import Network <EOL> from mozdns . ip . models import ipv6_to_longs <EOL> from core . views import CoreDeleteView , CoreDetailView <EOL> from core . views import CoreCreateView , CoreUpdateView , CoreListView <EOL> import ipaddr <EOL> import simplejson as json <EOL> class RangeView ( object ) : <EOL> model = Range <EOL> form_class = RangeForm <EOL> queryset = Range . objects . all ( ) <EOL> class RangeDeleteView ( RangeView , CoreDeleteView ) : <EOL> pass <EOL> class RangeCreateView ( RangeView , CoreCreateView ) : <EOL> def get_form ( self , form_class ) : <EOL> if not self . request . POST : <EOL> initial = { } <EOL> initial . update ( dict ( self . request . GET . items ( ) ) ) <EOL> return form_class ( initial = initial ) <EOL> else : <EOL> return super ( RangeCreateView , self ) . get_form ( form_class ) <EOL> class RangeUpdateView ( RangeView , CoreUpdateView ) : <EOL> template_name = "<STR_LIT>" <EOL> class RangeListView ( RangeView , CoreListView ) : <EOL> template_name = "<STR_LIT>" <EOL> class RangeDetailView ( RangeView , CoreDetailView ) : <EOL> template_name = '<STR_LIT>' <EOL> def get_context_data ( self , ** kwargs ) : <EOL> context = super ( RangeDetailView , self ) . get_context_data ( <EOL> ** kwargs ) <EOL> context [ '<STR_LIT>' ] = "<STR_LIT>" . format ( <EOL> self . form_class . Meta . model . __name__ <EOL> ) <EOL> if self . extra_context : <EOL> context = dict ( context . items ( ) + self . extra_context . items ( ) ) <EOL> return context <EOL> def range_usage_text ( request ) : <EOL> start = request . GET . get ( '<STR_LIT:start>' , None ) <EOL> end = request . GET . get ( '<STR_LIT:end>' , None ) <EOL> format = request . GET . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if not ( start and end ) : <EOL> return HttpResponse ( json . dumps ( { <EOL> '<STR_LIT:success>' : False , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) ) <EOL> get_objects = request . GET . get ( '<STR_LIT>' , False ) <EOL> if start . find ( '<STR_LIT::>' ) > - <NUM_LIT:1> : <EOL> ip_type = '<STR_LIT>' <EOL> else : <EOL> ip_type = '<STR_LIT:4>' <EOL> try : <EOL> usage_data = range_usage ( start , end , ip_type , get_objects ) <EOL> except ( ValidationError , ipaddr . AddressValueError ) , e : <EOL> return HttpResponse ( <EOL> json . dumps ( { <EOL> '<STR_LIT>' : str ( e ) , <EOL> '<STR_LIT:success>' : False <EOL> } ) ) <EOL> if format == '<STR_LIT>' : <EOL> usage_data [ '<STR_LIT>' ] = map ( lambda x : ( int_to_ip ( x [ <NUM_LIT:0> ] , ip_type ) , <EOL> int_to_ip ( x [ <NUM_LIT:1> ] , ip_type ) ) , <EOL> usage_data [ '<STR_LIT>' ] ) <EOL> usage_data [ '<STR_LIT:success>' ] = True <EOL> return HttpResponse ( json . dumps ( usage_data ) ) <EOL> def range_usage_ajax ( request ) : <EOL> start = request . GET . get ( '<STR_LIT:start>' , None ) <EOL> end = request . GET . get ( '<STR_LIT:end>' , None ) <EOL> start_ip_type , _ = resolve_ip_type ( start ) <EOL> end_ip_type , _ = resolve_ip_type ( end ) <EOL> errors = None <EOL> if start_ip_type != end_ip_type or start_ip_type is None : <EOL> errors = "<STR_LIT>" <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : errors , <EOL> } ) <EOL> rusage = range_usage ( start , end , start_ip_type , get_objects = True ) <EOL> def translate_ip ( ip_i , * args ) : <EOL> return int_to_ip ( ip_i , start_ip_type ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : errors , <EOL> '<STR_LIT:start>' : start , <EOL> '<STR_LIT:end>' : end , <EOL> '<STR_LIT>' : int_to_ip ( start , start_ip_type ) , <EOL> '<STR_LIT>' : int_to_ip ( end , end_ip_type ) , <EOL> '<STR_LIT>' : rusage , <EOL> '<STR_LIT>' : translate_ip <EOL> } ) <EOL> def range_detail ( request , range_pk ) : <EOL> mrange = get_object_or_404 ( Range , pk = range_pk ) <EOL> attrs = mrange . keyvalue_set . all ( ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : mrange , <EOL> '<STR_LIT>' : attrs , <EOL> } ) <EOL> def redirect_to_range_from_ip ( request ) : <EOL> ip_str = request . GET . get ( '<STR_LIT>' ) <EOL> ip_type = request . GET . get ( '<STR_LIT>' ) <EOL> if not ( ip_str and ip_type ) : <EOL> return HttpResponse ( json . dumps ( { '<STR_LIT>' : "<STR_LIT>" } ) ) <EOL> if ip_type == '<STR_LIT:4>' : <EOL> try : <EOL> ip_upper , ip_lower = <NUM_LIT:0> , int ( ipaddr . IPv4Address ( ip_str ) ) <EOL> except ipaddr . AddressValueError : <EOL> return HttpResponse ( <EOL> json . dumps ( { '<STR_LIT:success>' : False , '<STR_LIT:message>' : "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( ip_str ) } ) ) <EOL> else : <EOL> try : <EOL> ip_upper , ip_lower = ipv6_to_longs ( ip_str ) <EOL> except ValidationError : <EOL> return HttpResponse ( json . dumps ( { '<STR_LIT:success>' : False , <EOL> '<STR_LIT:message>' : '<STR_LIT>' } ) ) <EOL> range_ = Range . objects . filter ( start_upper__lte = ip_upper , <EOL> start_lower__lte = ip_lower , <EOL> end_upper__gte = ip_upper , <EOL> end_lower__gte = ip_lower ) <EOL> if not len ( range_ ) == <NUM_LIT:1> : <EOL> return HttpResponse ( json . dumps ( { '<STR_LIT>' : "<STR_LIT>" } ) ) <EOL> else : <EOL> return HttpResponse ( json . dumps ( <EOL> { '<STR_LIT:success>' : True , <EOL> '<STR_LIT>' : range_ [ <NUM_LIT:0> ] . get_absolute_url ( ) } ) ) <EOL> def get_next_available_ip_by_range ( request , range_id ) : <EOL> range = get_object_or_404 ( Range , id = range_id ) <EOL> ret = { } <EOL> ret_ip = range . get_next_ip ( ) <EOL> display_ip = ret_ip . exploded <EOL> ret [ '<STR_LIT:success>' ] = True <EOL> ret [ '<STR_LIT>' ] = display_ip <EOL> return HttpResponse ( json . dumps ( ret ) ) <EOL> def find_related ( request ) : <EOL> """<STR_LIT>""" <EOL> state = json . loads ( request . raw_post_data ) <EOL> if not state : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if '<STR_LIT>' not in state : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> try : <EOL> choice_type , choice_pk = state [ '<STR_LIT>' ] <EOL> except ValueError : <EOL> raise Exception ( <EOL> "<STR_LIT>" . format ( state [ '<STR_LIT>' ] ) <EOL> ) <EOL> filter_network , filter_site , filter_vlan = calculate_filters ( <EOL> choice_type , choice_pk <EOL> ) <EOL> format_network , format_site , format_vlan = label_value_maker ( ) <EOL> new_state = { <EOL> '<STR_LIT>' : format_site ( filter_site ( state [ '<STR_LIT>' ] ) ) , <EOL> '<STR_LIT>' : format_vlan ( filter_vlan ( state [ '<STR_LIT>' ] ) ) , <EOL> } <EOL> networks = filter_network ( state [ '<STR_LIT>' ] ) <EOL> if len ( networks ) == <NUM_LIT:1> : <EOL> new_state [ '<STR_LIT>' ] = integrate_real_ranges ( <EOL> networks [ <NUM_LIT:0> ] , calc_template_ranges ( networks [ <NUM_LIT:0> ] ) <EOL> ) <EOL> new_state [ '<STR_LIT>' ] = format_network ( networks ) <EOL> return HttpResponse ( json . dumps ( new_state ) ) <EOL> def ajax_find_related ( request ) : <EOL> networks = Network . objects . filter ( UN ) . order_by ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : Site . objects . all ( ) . order_by ( '<STR_LIT:name>' ) , <EOL> '<STR_LIT>' : Vlan . objects . all ( ) . order_by ( '<STR_LIT:name>' ) , <EOL> '<STR_LIT>' : networks <EOL> } ) <EOL> def debug_show_ranges ( request ) : <EOL> """<STR_LIT>""" <EOL> networks = Network . objects . filter ( UN ) . order_by ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : calc_template_ranges , <EOL> '<STR_LIT>' : networks <EOL> } ) </s>
