<s> from django . db import models <EOL> from django . core . exceptions import ValidationError <EOL> from mozdns . domain . models import name_to_domain <EOL> from mozdns . ip . utils import ip_to_domain_name , nibbilize <EOL> import ipaddr <EOL> class Ip ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> IP_TYPE_CHOICES = ( ( '<STR_LIT:4>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> ip_str = models . CharField ( max_length = <NUM_LIT> , editable = True , verbose_name = '<STR_LIT>' , <EOL> help_text = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> ip_upper = models . BigIntegerField ( null = True , blank = True ) <EOL> ip_lower = models . BigIntegerField ( null = True , blank = True ) <EOL> ip_type = models . CharField ( max_length = <NUM_LIT:1> , choices = IP_TYPE_CHOICES , <EOL> editable = True , <EOL> help_text = '<STR_LIT>' ) <EOL> class Meta : <EOL> abstract = True <EOL> def clean_ip ( self , update_reverse_domain = True ) : <EOL> """<STR_LIT>""" <EOL> self . validate_ip_str ( ) <EOL> if self . ip_type == '<STR_LIT:4>' : <EOL> Klass = ipaddr . IPv4Address <EOL> elif self . ip_type == '<STR_LIT>' : <EOL> Klass = ipaddr . IPv6Address <EOL> else : <EOL> raise ValidationError ( "<STR_LIT>" . format ( self . ip_type ) ) <EOL> try : <EOL> ip = Klass ( self . ip_str ) <EOL> self . ip_str = str ( ip ) <EOL> except ipaddr . AddressValueError : <EOL> raise ValidationError ( "<STR_LIT>" . format ( self . ip_str ) ) <EOL> if self . ip_type == '<STR_LIT:4>' : <EOL> self . ip_upper , self . ip_lower = <NUM_LIT:0> , int ( ip ) <EOL> else : <EOL> self . ip_upper , self . ip_lower = ipv6_to_longs ( int ( ip ) ) <EOL> def update_reverse_domain ( self ) : <EOL> rvname = nibbilize ( self . ip_str ) if self . ip_type == '<STR_LIT>' else self . ip_str <EOL> rvname = ip_to_domain_name ( rvname , ip_type = self . ip_type ) <EOL> self . reverse_domain = name_to_domain ( rvname ) <EOL> if ( self . reverse_domain is None or self . reverse_domain . name in <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> raise ValidationError ( "<STR_LIT>" <EOL> . format ( self . ip_str ) ) <EOL> def __int__ ( self ) : <EOL> if self . ip_type == '<STR_LIT:4>' : <EOL> return self . ip_lower <EOL> return ( self . ip_upper * ( <NUM_LIT:2> ** <NUM_LIT:64> ) ) + self . ip_lower <EOL> def validate_ip_str ( self ) : <EOL> if not isinstance ( self . ip_str , basestring ) : <EOL> raise ValidationError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def ipv6_to_longs ( addr ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> ip = ipaddr . IPv6Address ( addr ) <EOL> except ipaddr . AddressValueError : <EOL> raise ValidationError ( "<STR_LIT>" . <EOL> format ( addr ) ) <EOL> ip_upper = ip . _ip >> <NUM_LIT:64> <EOL> ip_lower = ip . _ip & ( <NUM_LIT:1> << <NUM_LIT:64> ) - <NUM_LIT:1> <EOL> return ( ip_upper , ip_lower ) </s>
