<s> """<STR_LIT>""" <EOL> import copy <EOL> import datetime <EOL> import os <EOL> import re <EOL> import time <EOL> import urlparse <EOL> try : <EOL> from cStringIO import StringIO <EOL> except ImportError : <EOL> from StringIO import StringIO <EOL> try : <EOL> from decimal import Decimal , DecimalException <EOL> except ImportError : <EOL> from django . utils . _decimal import Decimal , DecimalException <EOL> try : <EOL> set <EOL> except NameError : <EOL> from sets import Set as set <EOL> import django . core . exceptions <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . utils . encoding import smart_unicode , smart_str <EOL> from util import ErrorList , ValidationError <EOL> from widgets import TextInput , PasswordInput , HiddenInput , MultipleHiddenInput , FileInput , CheckboxInput , Select , NullBooleanSelect , SelectMultiple , DateInput , DateTimeInput , TimeInput , SplitDateTimeWidget , SplitHiddenDateTimeWidget <EOL> from django . core . files . uploadedfile import SimpleUploadedFile as UploadedFile <EOL> __all__ = ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ) <EOL> EMPTY_VALUES = ( None , '<STR_LIT>' ) <EOL> class Field ( object ) : <EOL> widget = TextInput <EOL> hidden_widget = HiddenInput <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( u'<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( u'<STR_LIT>' ) , <EOL> } <EOL> creation_counter = <NUM_LIT:0> <EOL> def __init__ ( self , required = True , widget = None , label = None , initial = None , <EOL> help_text = None , error_messages = None , show_hidden_initial = False ) : <EOL> if label is not None : <EOL> label = smart_unicode ( label ) <EOL> self . required , self . label , self . initial = required , label , initial <EOL> self . show_hidden_initial = show_hidden_initial <EOL> if help_text is None : <EOL> self . help_text = u'<STR_LIT>' <EOL> else : <EOL> self . help_text = smart_unicode ( help_text ) <EOL> widget = widget or self . widget <EOL> if isinstance ( widget , type ) : <EOL> widget = widget ( ) <EOL> extra_attrs = self . widget_attrs ( widget ) <EOL> if extra_attrs : <EOL> widget . attrs . update ( extra_attrs ) <EOL> self . widget = widget <EOL> self . creation_counter = Field . creation_counter <EOL> Field . creation_counter += <NUM_LIT:1> <EOL> def set_class_error_messages ( messages , klass ) : <EOL> for base_class in klass . __bases__ : <EOL> set_class_error_messages ( messages , base_class ) <EOL> messages . update ( getattr ( klass , '<STR_LIT>' , { } ) ) <EOL> messages = { } <EOL> set_class_error_messages ( messages , self . __class__ ) <EOL> messages . update ( error_messages or { } ) <EOL> self . error_messages = messages <EOL> def clean ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if self . required and value in EMPTY_VALUES : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> return value <EOL> def widget_attrs ( self , widget ) : <EOL> """<STR_LIT>""" <EOL> return { } <EOL> def __deepcopy__ ( self , memo ) : <EOL> result = copy . copy ( self ) <EOL> memo [ id ( self ) ] = result <EOL> result . widget = copy . deepcopy ( self . widget , memo ) <EOL> return result <EOL> class CharField ( Field ) : <EOL> default_error_messages = { <EOL> '<STR_LIT:max_length>' : _ ( u'<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( u'<STR_LIT>' ) , <EOL> } <EOL> def __init__ ( self , max_length = None , min_length = None , * args , ** kwargs ) : <EOL> self . max_length , self . min_length = max_length , min_length <EOL> super ( CharField , self ) . __init__ ( * args , ** kwargs ) <EOL> def clean ( self , value ) : <EOL> "<STR_LIT>" <EOL> super ( CharField , self ) . clean ( value ) <EOL> if value in EMPTY_VALUES : <EOL> return u'<STR_LIT>' <EOL> value = smart_unicode ( value ) <EOL> value_length = len ( value ) <EOL> if self . max_length is not None and value_length > self . max_length : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT:max_length>' ] % { '<STR_LIT>' : self . max_length , '<STR_LIT>' : value_length } ) <EOL> if self . min_length is not None and value_length < self . min_length : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] % { '<STR_LIT>' : self . min_length , '<STR_LIT>' : value_length } ) <EOL> return value <EOL> def widget_attrs ( self , widget ) : <EOL> if self . max_length is not None and isinstance ( widget , ( TextInput , PasswordInput ) ) : <EOL> return { '<STR_LIT>' : str ( self . max_length ) } <EOL> class IntegerField ( Field ) : <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( u'<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( u'<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( u'<STR_LIT>' ) , <EOL> } <EOL> def __init__ ( self , max_value = None , min_value = None , * args , ** kwargs ) : <EOL> self . max_value , self . min_value = max_value , min_value <EOL> super ( IntegerField , self ) . __init__ ( * args , ** kwargs ) <EOL> def clean ( self , value ) : <EOL> """<STR_LIT>""" <EOL> super ( IntegerField , self ) . clean ( value ) <EOL> if value in EMPTY_VALUES : <EOL> return None <EOL> try : <EOL> value = int ( str ( value ) ) <EOL> except ( ValueError , TypeError ) : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> if self . max_value is not None and value > self . max_value : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] % self . max_value ) <EOL> if self . min_value is not None and value < self . min_value : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] % self . min_value ) <EOL> return value <EOL> class FloatField ( Field ) : <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( u'<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( u'<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( u'<STR_LIT>' ) , <EOL> } <EOL> def __init__ ( self , max_value = None , min_value = None , * args , ** kwargs ) : <EOL> self . max_value , self . min_value = max_value , min_value <EOL> Field . __init__ ( self , * args , ** kwargs ) <EOL> def clean ( self , value ) : <EOL> """<STR_LIT>""" <EOL> super ( FloatField , self ) . clean ( value ) <EOL> if not self . required and value in EMPTY_VALUES : <EOL> return None <EOL> try : <EOL> value = float ( value ) <EOL> except ( ValueError , TypeError ) : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> if self . max_value is not None and value > self . max_value : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] % self . max_value ) <EOL> if self . min_value is not None and value < self . min_value : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] % self . min_value ) <EOL> return value <EOL> class DecimalField ( Field ) : <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( u'<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( u'<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( u'<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) <EOL> } <EOL> def __init__ ( self , max_value = None , min_value = None , max_digits = None , decimal_places = None , * args , ** kwargs ) : <EOL> self . max_value , self . min_value = max_value , min_value <EOL> self . max_digits , self . decimal_places = max_digits , decimal_places <EOL> Field . __init__ ( self , * args , ** kwargs ) <EOL> def clean ( self , value ) : <EOL> """<STR_LIT>""" <EOL> super ( DecimalField , self ) . clean ( value ) <EOL> if not self . required and value in EMPTY_VALUES : <EOL> return None <EOL> value = smart_str ( value ) . strip ( ) <EOL> try : <EOL> value = Decimal ( value ) <EOL> except DecimalException : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> sign , digittuple , exponent = value . as_tuple ( ) <EOL> decimals = abs ( exponent ) <EOL> digits = len ( digittuple ) <EOL> if decimals > digits : <EOL> digits = decimals <EOL> whole_digits = digits - decimals <EOL> if self . max_value is not None and value > self . max_value : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] % self . max_value ) <EOL> if self . min_value is not None and value < self . min_value : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] % self . min_value ) <EOL> if self . max_digits is not None and digits > self . max_digits : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] % self . max_digits ) <EOL> if self . decimal_places is not None and decimals > self . decimal_places : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] % self . decimal_places ) <EOL> if self . max_digits is not None and self . decimal_places is not None and whole_digits > ( self . max_digits - self . decimal_places ) : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] % ( self . max_digits - self . decimal_places ) ) <EOL> return value <EOL> DEFAULT_DATE_INPUT_FORMATS = ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> ) <EOL> class DateField ( Field ) : <EOL> widget = DateInput <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( u'<STR_LIT>' ) , <EOL> } <EOL> def __init__ ( self , input_formats = None , * args , ** kwargs ) : <EOL> super ( DateField , self ) . __init__ ( * args , ** kwargs ) <EOL> self . input_formats = input_formats or DEFAULT_DATE_INPUT_FORMATS <EOL> def clean ( self , value ) : <EOL> """<STR_LIT>""" <EOL> super ( DateField , self ) . clean ( value ) <EOL> if value in EMPTY_VALUES : <EOL> return None <EOL> if isinstance ( value , datetime . datetime ) : <EOL> return value . date ( ) <EOL> if isinstance ( value , datetime . date ) : <EOL> return value <EOL> for format in self . input_formats : <EOL> try : <EOL> return datetime . date ( * time . strptime ( value , format ) [ : <NUM_LIT:3> ] ) <EOL> except ValueError : <EOL> continue <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> DEFAULT_TIME_INPUT_FORMATS = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> class TimeField ( Field ) : <EOL> widget = TimeInput <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( u'<STR_LIT>' ) <EOL> } <EOL> def __init__ ( self , input_formats = None , * args , ** kwargs ) : <EOL> super ( TimeField , self ) . __init__ ( * args , ** kwargs ) <EOL> self . input_formats = input_formats or DEFAULT_TIME_INPUT_FORMATS <EOL> def clean ( self , value ) : <EOL> """<STR_LIT>""" <EOL> super ( TimeField , self ) . clean ( value ) <EOL> if value in EMPTY_VALUES : <EOL> return None <EOL> if isinstance ( value , datetime . time ) : <EOL> return value <EOL> for format in self . input_formats : <EOL> try : <EOL> return datetime . time ( * time . strptime ( value , format ) [ <NUM_LIT:3> : <NUM_LIT:6> ] ) <EOL> except ValueError : <EOL> continue <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> DEFAULT_DATETIME_INPUT_FORMATS = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> class DateTimeField ( Field ) : <EOL> widget = DateTimeInput <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( u'<STR_LIT>' ) , <EOL> } <EOL> def __init__ ( self , input_formats = None , * args , ** kwargs ) : <EOL> super ( DateTimeField , self ) . __init__ ( * args , ** kwargs ) <EOL> self . input_formats = input_formats or DEFAULT_DATETIME_INPUT_FORMATS <EOL> def clean ( self , value ) : <EOL> """<STR_LIT>""" <EOL> super ( DateTimeField , self ) . clean ( value ) <EOL> if value in EMPTY_VALUES : <EOL> return None <EOL> if isinstance ( value , datetime . datetime ) : <EOL> return value <EOL> if isinstance ( value , datetime . date ) : <EOL> return datetime . datetime ( value . year , value . month , value . day ) <EOL> if isinstance ( value , list ) : <EOL> if len ( value ) != <NUM_LIT:2> : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> value = '<STR_LIT>' % tuple ( value ) <EOL> for format in self . input_formats : <EOL> try : <EOL> return datetime . datetime ( * time . strptime ( value , format ) [ : <NUM_LIT:6> ] ) <EOL> except ValueError : <EOL> continue <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> class RegexField ( CharField ) : <EOL> def __init__ ( self , regex , max_length = None , min_length = None , error_message = None , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if error_message : <EOL> error_messages = kwargs . get ( '<STR_LIT>' ) or { } <EOL> error_messages [ '<STR_LIT>' ] = error_message <EOL> kwargs [ '<STR_LIT>' ] = error_messages <EOL> super ( RegexField , self ) . __init__ ( max_length , min_length , * args , ** kwargs ) <EOL> if isinstance ( regex , basestring ) : <EOL> regex = re . compile ( regex ) <EOL> self . regex = regex <EOL> def clean ( self , value ) : <EOL> """<STR_LIT>""" <EOL> value = super ( RegexField , self ) . clean ( value ) <EOL> if value == u'<STR_LIT>' : <EOL> return value <EOL> if not self . regex . search ( value ) : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> return value <EOL> email_re = re . compile ( <EOL> r"<STR_LIT>" <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' , re . IGNORECASE ) <EOL> class EmailField ( RegexField ) : <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( u'<STR_LIT>' ) , <EOL> } <EOL> def __init__ ( self , max_length = None , min_length = None , * args , ** kwargs ) : <EOL> RegexField . __init__ ( self , email_re , max_length , min_length , * args , <EOL> ** kwargs ) <EOL> try : <EOL> from django . conf import settings <EOL> URL_VALIDATOR_USER_AGENT = settings . URL_VALIDATOR_USER_AGENT <EOL> except ImportError : <EOL> URL_VALIDATOR_USER_AGENT = '<STR_LIT>' <EOL> class FileField ( Field ) : <EOL> widget = FileInput <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( u"<STR_LIT>" ) , <EOL> '<STR_LIT>' : _ ( u"<STR_LIT>" ) , <EOL> '<STR_LIT>' : _ ( u"<STR_LIT>" ) , <EOL> '<STR_LIT:max_length>' : _ ( u'<STR_LIT>' ) , <EOL> } <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . max_length = kwargs . pop ( '<STR_LIT:max_length>' , None ) <EOL> super ( FileField , self ) . __init__ ( * args , ** kwargs ) <EOL> def clean ( self , data , initial = None ) : <EOL> super ( FileField , self ) . clean ( initial or data ) <EOL> if not self . required and data in EMPTY_VALUES : <EOL> return None <EOL> elif not data and initial : <EOL> return initial <EOL> try : <EOL> file_name = data . name <EOL> file_size = data . size <EOL> except AttributeError : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> if self . max_length is not None and len ( file_name ) > self . max_length : <EOL> error_values = { '<STR_LIT>' : self . max_length , '<STR_LIT>' : len ( file_name ) } <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT:max_length>' ] % error_values ) <EOL> if not file_name : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> if not file_size : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> return data <EOL> class ImageField ( FileField ) : <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( u"<STR_LIT>" ) , <EOL> } <EOL> def clean ( self , data , initial = None ) : <EOL> """<STR_LIT>""" <EOL> f = super ( ImageField , self ) . clean ( data , initial ) <EOL> if f is None : <EOL> return None <EOL> elif not data and initial : <EOL> return initial <EOL> from PIL import Image <EOL> if hasattr ( data , '<STR_LIT>' ) : <EOL> file = data . temporary_file_path ( ) <EOL> else : <EOL> if hasattr ( data , '<STR_LIT>' ) : <EOL> file = StringIO ( data . read ( ) ) <EOL> else : <EOL> file = StringIO ( data [ '<STR_LIT:content>' ] ) <EOL> try : <EOL> trial_image = Image . open ( file ) <EOL> trial_image . load ( ) <EOL> if hasattr ( file , '<STR_LIT>' ) : <EOL> file . reset ( ) <EOL> trial_image = Image . open ( file ) <EOL> trial_image . verify ( ) <EOL> except ImportError : <EOL> raise <EOL> except Exception : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> if hasattr ( f , '<STR_LIT>' ) and callable ( f . seek ) : <EOL> f . seek ( <NUM_LIT:0> ) <EOL> return f <EOL> url_re = re . compile ( <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' , re . IGNORECASE ) <EOL> class URLField ( RegexField ) : <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( u'<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( u'<STR_LIT>' ) , <EOL> } <EOL> def __init__ ( self , max_length = None , min_length = None , verify_exists = False , <EOL> validator_user_agent = URL_VALIDATOR_USER_AGENT , * args , ** kwargs ) : <EOL> super ( URLField , self ) . __init__ ( url_re , max_length , min_length , * args , <EOL> ** kwargs ) <EOL> self . verify_exists = verify_exists <EOL> self . user_agent = validator_user_agent <EOL> def clean ( self , value ) : <EOL> if value and '<STR_LIT>' not in value : <EOL> value = u'<STR_LIT>' % value <EOL> if value and not urlparse . urlsplit ( value ) [ <NUM_LIT:2> ] : <EOL> value += '<STR_LIT:/>' <EOL> value = super ( URLField , self ) . clean ( value ) <EOL> if value == u'<STR_LIT>' : <EOL> return value <EOL> if self . verify_exists : <EOL> import urllib2 <EOL> headers = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : self . user_agent , <EOL> } <EOL> try : <EOL> req = urllib2 . Request ( value , None , headers ) <EOL> u = urllib2 . urlopen ( req ) <EOL> except ValueError : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> except : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> return value <EOL> class BooleanField ( Field ) : <EOL> widget = CheckboxInput <EOL> def clean ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if value in ( '<STR_LIT:False>' , '<STR_LIT:0>' ) : <EOL> value = False <EOL> else : <EOL> value = bool ( value ) <EOL> super ( BooleanField , self ) . clean ( value ) <EOL> if not value and self . required : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> return value <EOL> class NullBooleanField ( BooleanField ) : <EOL> """<STR_LIT>""" <EOL> widget = NullBooleanSelect <EOL> def clean ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if value in ( True , '<STR_LIT:True>' , '<STR_LIT:1>' ) : <EOL> return True <EOL> elif value in ( False , '<STR_LIT:False>' , '<STR_LIT:0>' ) : <EOL> return False <EOL> else : <EOL> return None <EOL> class ChoiceField ( Field ) : <EOL> widget = Select <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( u'<STR_LIT>' ) , <EOL> } <EOL> def __init__ ( self , choices = ( ) , required = True , widget = None , label = None , <EOL> initial = None , help_text = None , * args , ** kwargs ) : <EOL> super ( ChoiceField , self ) . __init__ ( required , widget , label , initial , <EOL> help_text , * args , ** kwargs ) <EOL> self . choices = choices <EOL> def _get_choices ( self ) : <EOL> return self . _choices <EOL> def _set_choices ( self , value ) : <EOL> self . _choices = self . widget . choices = list ( value ) <EOL> choices = property ( _get_choices , _set_choices ) <EOL> def clean ( self , value ) : <EOL> """<STR_LIT>""" <EOL> value = super ( ChoiceField , self ) . clean ( value ) <EOL> if value in EMPTY_VALUES : <EOL> value = u'<STR_LIT>' <EOL> value = smart_unicode ( value ) <EOL> if value == u'<STR_LIT>' : <EOL> return value <EOL> if not self . valid_value ( value ) : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] % { '<STR_LIT:value>' : value } ) <EOL> return value <EOL> def valid_value ( self , value ) : <EOL> "<STR_LIT>" <EOL> for k , v in self . choices : <EOL> if type ( v ) in ( tuple , list ) : <EOL> for k2 , v2 in v : <EOL> if value == smart_unicode ( k2 ) : <EOL> return True <EOL> else : <EOL> if value == smart_unicode ( k ) : <EOL> return True <EOL> return False <EOL> class TypedChoiceField ( ChoiceField ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . coerce = kwargs . pop ( '<STR_LIT>' , lambda val : val ) <EOL> self . empty_value = kwargs . pop ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> super ( TypedChoiceField , self ) . __init__ ( * args , ** kwargs ) <EOL> def clean ( self , value ) : <EOL> """<STR_LIT>""" <EOL> value = super ( TypedChoiceField , self ) . clean ( value ) <EOL> if value == self . empty_value or value in EMPTY_VALUES : <EOL> return self . empty_value <EOL> try : <EOL> value = self . coerce ( value ) <EOL> except ( ValueError , TypeError , django . core . exceptions . ValidationError ) : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] % { '<STR_LIT:value>' : value } ) <EOL> return value <EOL> class MultipleChoiceField ( ChoiceField ) : <EOL> hidden_widget = MultipleHiddenInput <EOL> widget = SelectMultiple <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( u'<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( u'<STR_LIT>' ) , <EOL> } <EOL> def clean ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if self . required and not value : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> elif not self . required and not value : <EOL> return [ ] <EOL> if not isinstance ( value , ( list , tuple ) ) : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> new_value = [ smart_unicode ( val ) for val in value ] <EOL> for val in new_value : <EOL> if not self . valid_value ( val ) : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] % { '<STR_LIT:value>' : val } ) <EOL> return new_value <EOL> class ComboField ( Field ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , fields = ( ) , * args , ** kwargs ) : <EOL> super ( ComboField , self ) . __init__ ( * args , ** kwargs ) <EOL> for f in fields : <EOL> f . required = False <EOL> self . fields = fields <EOL> def clean ( self , value ) : <EOL> """<STR_LIT>""" <EOL> super ( ComboField , self ) . clean ( value ) <EOL> for field in self . fields : <EOL> value = field . clean ( value ) <EOL> return value <EOL> class MultiValueField ( Field ) : <EOL> """<STR_LIT>""" <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( u'<STR_LIT>' ) , <EOL> } <EOL> def __init__ ( self , fields = ( ) , * args , ** kwargs ) : <EOL> super ( MultiValueField , self ) . __init__ ( * args , ** kwargs ) <EOL> for f in fields : <EOL> f . required = False <EOL> self . fields = fields <EOL> def clean ( self , value ) : <EOL> """<STR_LIT>""" <EOL> clean_data = [ ] <EOL> errors = ErrorList ( ) <EOL> if not value or isinstance ( value , ( list , tuple ) ) : <EOL> if not value or not [ v for v in value if v not in EMPTY_VALUES ] : <EOL> if self . required : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> else : <EOL> return self . compress ( [ ] ) <EOL> else : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> for i , field in enumerate ( self . fields ) : <EOL> try : <EOL> field_value = value [ i ] <EOL> except IndexError : <EOL> field_value = None <EOL> if self . required and field_value in EMPTY_VALUES : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> try : <EOL> clean_data . append ( field . clean ( field_value ) ) <EOL> except ValidationError , e : <EOL> errors . extend ( e . messages ) <EOL> if errors : <EOL> raise ValidationError ( errors ) <EOL> return self . compress ( clean_data ) <EOL> def compress ( self , data_list ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> class FilePathField ( ChoiceField ) : <EOL> def __init__ ( self , path , match = None , recursive = False , required = True , <EOL> widget = None , label = None , initial = None , help_text = None , <EOL> * args , ** kwargs ) : <EOL> self . path , self . match , self . recursive = path , match , recursive <EOL> super ( FilePathField , self ) . __init__ ( choices = ( ) , required = required , <EOL> widget = widget , label = label , initial = initial , help_text = help_text , <EOL> * args , ** kwargs ) <EOL> if self . required : <EOL> self . choices = [ ] <EOL> else : <EOL> self . choices = [ ( "<STR_LIT>" , "<STR_LIT>" ) ] <EOL> if self . match is not None : <EOL> self . match_re = re . compile ( self . match ) <EOL> if recursive : <EOL> for root , dirs , files in os . walk ( self . path ) : <EOL> for f in files : <EOL> if self . match is None or self . match_re . search ( f ) : <EOL> f = os . path . join ( root , f ) <EOL> self . choices . append ( ( f , f . replace ( path , "<STR_LIT>" , <NUM_LIT:1> ) ) ) <EOL> else : <EOL> try : <EOL> for f in os . listdir ( self . path ) : <EOL> full_file = os . path . join ( self . path , f ) <EOL> if os . path . isfile ( full_file ) and ( self . match is None or self . match_re . search ( f ) ) : <EOL> self . choices . append ( ( full_file , f ) ) <EOL> except OSError : <EOL> pass <EOL> self . widget . choices = self . choices <EOL> class SplitDateTimeField ( MultiValueField ) : <EOL> widget = SplitDateTimeWidget <EOL> hidden_widget = SplitHiddenDateTimeWidget <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( u'<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( u'<STR_LIT>' ) , <EOL> } <EOL> def __init__ ( self , input_date_formats = None , input_time_formats = None , * args , ** kwargs ) : <EOL> errors = self . default_error_messages . copy ( ) <EOL> if '<STR_LIT>' in kwargs : <EOL> errors . update ( kwargs [ '<STR_LIT>' ] ) <EOL> fields = ( <EOL> DateField ( input_formats = input_date_formats , error_messages = { '<STR_LIT>' : errors [ '<STR_LIT>' ] } ) , <EOL> TimeField ( input_formats = input_time_formats , error_messages = { '<STR_LIT>' : errors [ '<STR_LIT>' ] } ) , <EOL> ) <EOL> super ( SplitDateTimeField , self ) . __init__ ( fields , * args , ** kwargs ) <EOL> def compress ( self , data_list ) : <EOL> if data_list : <EOL> if data_list [ <NUM_LIT:0> ] in EMPTY_VALUES : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> if data_list [ <NUM_LIT:1> ] in EMPTY_VALUES : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> return datetime . datetime . combine ( * data_list ) <EOL> return None <EOL> ipv4_re = re . compile ( r'<STR_LIT>' ) <EOL> class IPAddressField ( RegexField ) : <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( u'<STR_LIT>' ) , <EOL> } <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( IPAddressField , self ) . __init__ ( ipv4_re , * args , ** kwargs ) <EOL> slug_re = re . compile ( r'<STR_LIT>' ) <EOL> class SlugField ( RegexField ) : <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( u"<STR_LIT>" <EOL> u"<STR_LIT>" ) , <EOL> } <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( SlugField , self ) . __init__ ( slug_re , * args , ** kwargs ) </s>
