<s> import re <EOL> import netaddr <EOL> from django . core . exceptions import ValidationError <EOL> from django . core import urlresolvers <EOL> from django . forms import fields <EOL> from django . forms . util import flatatt <EOL> from django . forms import widgets <EOL> from django . utils . encoding import force_text <EOL> from django . utils . functional import Promise <EOL> from django . utils import html <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> ip_allowed_symbols_re = re . compile ( r'<STR_LIT>' ) <EOL> IPv4 = <NUM_LIT:1> <EOL> IPv6 = <NUM_LIT:2> <EOL> class IPField ( fields . Field ) : <EOL> """<STR_LIT>""" <EOL> invalid_format_message = _ ( "<STR_LIT>" ) <EOL> invalid_version_message = _ ( "<STR_LIT>" ) <EOL> invalid_mask_message = _ ( "<STR_LIT>" ) <EOL> max_v4_mask = <NUM_LIT:32> <EOL> max_v6_mask = <NUM_LIT> <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . mask = kwargs . pop ( "<STR_LIT>" , None ) <EOL> self . min_mask = kwargs . pop ( "<STR_LIT>" , <NUM_LIT:0> ) <EOL> self . version = kwargs . pop ( '<STR_LIT:version>' , IPv4 ) <EOL> super ( IPField , self ) . __init__ ( * args , ** kwargs ) <EOL> def validate ( self , value ) : <EOL> super ( IPField , self ) . validate ( value ) <EOL> if not value and not self . required : <EOL> return <EOL> try : <EOL> if self . mask : <EOL> self . ip = netaddr . IPNetwork ( value ) <EOL> else : <EOL> self . ip = netaddr . IPAddress ( value ) <EOL> except Exception : <EOL> raise ValidationError ( self . invalid_format_message ) <EOL> if not any ( [ self . version & IPv4 > <NUM_LIT:0> and self . ip . version == <NUM_LIT:4> , <EOL> self . version & IPv6 > <NUM_LIT:0> and self . ip . version == <NUM_LIT:6> ] ) : <EOL> raise ValidationError ( self . invalid_version_message ) <EOL> if self . mask : <EOL> if self . ip . version == <NUM_LIT:4> and not self . min_mask <= self . ip . prefixlen <= self . max_v4_mask : <EOL> raise ValidationError ( self . invalid_mask_message ) <EOL> if self . ip . version == <NUM_LIT:6> and not self . min_mask <= self . ip . prefixlen <= self . max_v6_mask : <EOL> raise ValidationError ( self . invalid_mask_message ) <EOL> def clean ( self , value ) : <EOL> super ( IPField , self ) . clean ( value ) <EOL> return str ( getattr ( self , "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> class MultiIPField ( IPField ) : <EOL> """<STR_LIT>""" <EOL> def validate ( self , value ) : <EOL> self . addresses = [ ] <EOL> if value : <EOL> addresses = value . split ( '<STR_LIT:U+002C>' ) <EOL> for ip in addresses : <EOL> super ( MultiIPField , self ) . validate ( ip ) <EOL> self . addresses . append ( ip ) <EOL> else : <EOL> super ( MultiIPField , self ) . validate ( value ) <EOL> def clean ( self , value ) : <EOL> super ( MultiIPField , self ) . clean ( value ) <EOL> return str ( '<STR_LIT:U+002C>' . join ( getattr ( self , "<STR_LIT>" , [ ] ) ) ) <EOL> class SelectWidget ( widgets . Select ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , attrs = None , choices = ( ) , data_attrs = ( ) , transform = None , <EOL> transform_html_attrs = None ) : <EOL> self . data_attrs = data_attrs <EOL> self . transform = transform <EOL> self . transform_html_attrs = transform_html_attrs <EOL> super ( SelectWidget , self ) . __init__ ( attrs , choices ) <EOL> def render_option ( self , selected_choices , option_value , option_label ) : <EOL> option_value = force_text ( option_value ) <EOL> other_html = ( u'<STR_LIT>' <EOL> if option_value in selected_choices else '<STR_LIT>' ) <EOL> if callable ( self . transform_html_attrs ) : <EOL> html_attrs = self . transform_html_attrs ( option_label ) <EOL> other_html += flatatt ( html_attrs ) <EOL> if not isinstance ( option_label , ( basestring , Promise ) ) : <EOL> for data_attr in self . data_attrs : <EOL> data_value = html . conditional_escape ( <EOL> force_text ( getattr ( option_label , <EOL> data_attr , "<STR_LIT>" ) ) ) <EOL> other_html += '<STR_LIT>' % ( data_attr , data_value ) <EOL> if callable ( self . transform ) : <EOL> option_label = self . transform ( option_label ) <EOL> return u'<STR_LIT>' % ( <EOL> html . escape ( option_value ) , other_html , <EOL> html . conditional_escape ( force_text ( option_label ) ) ) <EOL> class DynamicSelectWidget ( widgets . Select ) : <EOL> """<STR_LIT>""" <EOL> _data_add_url_attr = "<STR_LIT>" <EOL> def render ( self , * args , ** kwargs ) : <EOL> add_item_url = self . get_add_item_url ( ) <EOL> if add_item_url is not None : <EOL> self . attrs [ self . _data_add_url_attr ] = add_item_url <EOL> return super ( DynamicSelectWidget , self ) . render ( * args , ** kwargs ) <EOL> def get_add_item_url ( self ) : <EOL> if callable ( self . add_item_link ) : <EOL> return self . add_item_link ( ) <EOL> try : <EOL> if self . add_item_link_args : <EOL> return urlresolvers . reverse ( self . add_item_link , <EOL> args = self . add_item_link_args ) <EOL> else : <EOL> return urlresolvers . reverse ( self . add_item_link ) <EOL> except urlresolvers . NoReverseMatch : <EOL> return self . add_item_link <EOL> class DynamicChoiceField ( fields . ChoiceField ) : <EOL> """<STR_LIT>""" <EOL> widget = DynamicSelectWidget <EOL> def __init__ ( self , <EOL> add_item_link = None , <EOL> add_item_link_args = None , <EOL> * args , <EOL> ** kwargs ) : <EOL> super ( DynamicChoiceField , self ) . __init__ ( * args , ** kwargs ) <EOL> self . widget . add_item_link = add_item_link <EOL> self . widget . add_item_link_args = add_item_link_args <EOL> class DynamicTypedChoiceField ( DynamicChoiceField , fields . TypedChoiceField ) : <EOL> """<STR_LIT>""" <EOL> pass </s>
