<s> import datetime <EOL> import time <EOL> from decimal import Decimal , InvalidOperation <EOL> from django import forms <EOL> from django . contrib . admin . widgets import AdminDateWidget , AdminIntegerFieldWidget , AdminTimeWidget <EOL> from django . core import exceptions , validators <EOL> from django . core . exceptions import ValidationError <EOL> from django . utils import formats <EOL> from django . utils . safestring import mark_safe <EOL> from django . utils . translation import ugettext as _ <EOL> class AdminSplitDateTime ( forms . SplitDateTimeWidget ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , attrs = None ) : <EOL> widgets = [ <EOL> AdminDateWidget , AdminTimeWidget , AdminDateWidget , AdminTimeWidget <EOL> ] <EOL> forms . MultiWidget . __init__ ( self , widgets , attrs ) <EOL> def format_output ( self , rendered_widgets ) : <EOL> return mark_safe ( u'<STR_LIT>' % ( <EOL> _ ( '<STR_LIT>' ) , rendered_widgets [ <NUM_LIT:0> ] , <EOL> _ ( '<STR_LIT>' ) , rendered_widgets [ <NUM_LIT:1> ] , <EOL> _ ( '<STR_LIT>' ) , rendered_widgets [ <NUM_LIT:2> ] , <EOL> _ ( '<STR_LIT>' ) , rendered_widgets [ <NUM_LIT:3> ] <EOL> ) ) <EOL> class AdminSplitDate ( forms . SplitDateTimeWidget ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , attrs = None ) : <EOL> widgets = [ AdminDateWidget , AdminDateWidget ] <EOL> forms . MultiWidget . __init__ ( self , widgets , attrs ) <EOL> def format_output ( self , rendered_widgets ) : <EOL> return mark_safe ( u'<STR_LIT>' % ( <EOL> _ ( '<STR_LIT>' ) , rendered_widgets [ <NUM_LIT:0> ] , <EOL> _ ( '<STR_LIT>' ) , rendered_widgets [ <NUM_LIT:1> ] ) <EOL> ) <EOL> class AdminSplitTime ( forms . SplitDateTimeWidget ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , attrs = None ) : <EOL> widgets = [ AdminTimeWidget , AdminTimeWidget ] <EOL> forms . MultiWidget . __init__ ( self , widgets , attrs ) <EOL> def format_output ( self , rendered_widgets ) : <EOL> return mark_safe ( u'<STR_LIT>' % ( <EOL> _ ( '<STR_LIT>' ) , rendered_widgets [ <NUM_LIT:0> ] , <EOL> _ ( '<STR_LIT>' ) , rendered_widgets [ <NUM_LIT:1> ] ) <EOL> ) <EOL> class AdminSplitInteger ( forms . SplitDateTimeWidget ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , attrs = None ) : <EOL> widgets = [ AdminIntegerFieldWidget , AdminIntegerFieldWidget ] <EOL> forms . MultiWidget . __init__ ( self , widgets , attrs ) <EOL> def format_output ( self , rendered_widgets ) : <EOL> return mark_safe ( u'<STR_LIT>' % ( <EOL> _ ( '<STR_LIT>' ) , rendered_widgets [ <NUM_LIT:0> ] , _ ( '<STR_LIT>' ) , rendered_widgets [ <NUM_LIT:1> ] <EOL> ) ) <EOL> class BasicTextField ( forms . fields . CharField ) : <EOL> def __init__ ( self , field , * args , ** kwargs ) : <EOL> super ( BasicTextField , self ) . __init__ ( <EOL> required = False , <EOL> help_text = "<STR_LIT>" % ( <EOL> field . label . lower ( ) ) , <EOL> * args , ** kwargs <EOL> ) <EOL> def filter ( self , name , value , queryset ) : <EOL> kwargs = { '<STR_LIT>' % name : value } <EOL> return queryset . filter ( ** kwargs ) <EOL> class BooleanField ( forms . fields . ChoiceField ) : <EOL> def __init__ ( self , field , * args , ** kwargs ) : <EOL> super ( BooleanField , self ) . __init__ ( <EOL> required = False , <EOL> help_text = "<STR_LIT>" % ( <EOL> field . label . lower ( ) ) , <EOL> choices = ( <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( True , "<STR_LIT>" ) , <EOL> ( False , "<STR_LIT>" ) , <EOL> ) , <EOL> * args , <EOL> ** kwargs <EOL> ) <EOL> def filter ( self , name , value , queryset ) : <EOL> if value in ( '<STR_LIT:False>' , '<STR_LIT:0>' ) : <EOL> value = False <EOL> elif value in ( '<STR_LIT:True>' , '<STR_LIT:1>' ) : <EOL> value = True <EOL> else : <EOL> value = None <EOL> kwargs = { name : bool ( value ) } <EOL> return queryset . filter ( ** kwargs ) <EOL> class NullBooleanField ( BooleanField ) : <EOL> pass <EOL> class CharField ( BasicTextField ) : <EOL> pass <EOL> class CommaSeparatedIntegerField ( BasicTextField ) : <EOL> pass <EOL> class DateField ( forms . fields . DateField ) : <EOL> def __init__ ( self , field , * args , ** kwargs ) : <EOL> super ( DateField , self ) . __init__ ( <EOL> required = False , <EOL> widget = AdminSplitDate , <EOL> help_text = "<STR_LIT>" % field . label . lower ( ) , <EOL> * args , ** kwargs <EOL> ) <EOL> def to_python ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if value in validators . 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> if isinstance ( value , list ) : <EOL> if len ( value ) != <NUM_LIT:2> : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> if value [ <NUM_LIT:0> ] in validators . EMPTY_VALUES and value [ <NUM_LIT:1> ] in validators . EMPTY_VALUES : <EOL> return None <EOL> start_value = value [ <NUM_LIT:0> ] <EOL> end_value = value [ <NUM_LIT:1> ] <EOL> start_date = None <EOL> end_date = None <EOL> for format in self . input_formats or formats . get_format ( '<STR_LIT>' ) : <EOL> try : <EOL> start_date = datetime . datetime ( * time . strptime ( start_value , format ) [ : <NUM_LIT:6> ] ) . date ( ) <EOL> except ValueError : <EOL> continue <EOL> for format in self . input_formats or formats . get_format ( '<STR_LIT>' ) : <EOL> try : <EOL> end_date = datetime . datetime ( <EOL> * time . strptime ( end_value , format ) [ : <NUM_LIT:6> ] <EOL> ) . date ( ) <EOL> except ValueError : <EOL> continue <EOL> return ( start_date , end_date ) <EOL> def filter ( self , name , value , queryset ) : <EOL> kwargs = { } <EOL> if value [ <NUM_LIT:0> ] : <EOL> kwargs [ '<STR_LIT>' % name ] = value [ <NUM_LIT:0> ] <EOL> if value [ <NUM_LIT:1> ] : <EOL> kwargs [ '<STR_LIT>' % name ] = value [ <NUM_LIT:1> ] <EOL> return queryset . filter ( ** kwargs ) <EOL> class DateTimeField ( forms . fields . DateTimeField ) : <EOL> def __init__ ( self , field , * args , ** kwargs ) : <EOL> super ( DateTimeField , self ) . __init__ ( <EOL> required = False , <EOL> widget = AdminSplitDateTime , <EOL> help_text = "<STR_LIT>" % field . label . lower ( ) , <EOL> * args , ** kwargs <EOL> ) <EOL> def to_python ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if value in validators . 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:4> : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> if value [ <NUM_LIT:0> ] in validators . EMPTY_VALUES and value [ <NUM_LIT:1> ] in validators . EMPTY_VALUES and value [ <NUM_LIT:2> ] in validators . EMPTY_VALUES and value [ <NUM_LIT:3> ] in validators . EMPTY_VALUES : <EOL> return None <EOL> start_value = '<STR_LIT>' % tuple ( value [ : <NUM_LIT:2> ] ) <EOL> end_value = '<STR_LIT>' % tuple ( value [ <NUM_LIT:2> : ] ) <EOL> start_datetime = None <EOL> end_datetime = None <EOL> for format in self . input_formats or formats . get_format ( '<STR_LIT>' ) : <EOL> try : <EOL> start_datetime = datetime . datetime ( * time . strptime ( start_value , format ) [ : <NUM_LIT:6> ] ) <EOL> except ValueError : <EOL> continue <EOL> for format in self . input_formats or formats . get_format ( '<STR_LIT>' ) : <EOL> try : <EOL> end_datetime = datetime . datetime ( * time . strptime ( end_value , format ) [ : <NUM_LIT:6> ] ) <EOL> except ValueError : <EOL> continue <EOL> return ( start_datetime , end_datetime ) <EOL> def filter ( self , name , value , queryset ) : <EOL> kwargs = { } <EOL> if value [ <NUM_LIT:0> ] : <EOL> kwargs [ '<STR_LIT>' % name ] = value [ <NUM_LIT:0> ] <EOL> if value [ <NUM_LIT:1> ] : <EOL> kwargs [ '<STR_LIT>' % name ] = value [ <NUM_LIT:1> ] <EOL> return queryset . filter ( ** kwargs ) <EOL> class Field ( forms . fields . Field ) : <EOL> def __init__ ( self , field , * args , ** kwargs ) : <EOL> super ( Field , self ) . __init__ ( <EOL> required = False , <EOL> * args , ** kwargs <EOL> ) <EOL> class FileField ( BasicTextField ) : <EOL> pass <EOL> class FilePathField ( BasicTextField ) : <EOL> pass <EOL> class IntegerField ( forms . fields . IntegerField ) : <EOL> def __init__ ( self , field , * args , ** kwargs ) : <EOL> super ( IntegerField , self ) . __init__ ( <EOL> required = False , <EOL> widget = AdminSplitInteger , <EOL> help_text = "<STR_LIT>" % field . label . lower ( ) , <EOL> * args , ** kwargs <EOL> ) <EOL> def to_python ( self , value ) : <EOL> if value in validators . EMPTY_VALUES : <EOL> return None <EOL> if isinstance ( value , list ) : <EOL> if len ( value ) != <NUM_LIT:2> : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> if value [ <NUM_LIT:0> ] in validators . EMPTY_VALUES and value [ <NUM_LIT:1> ] in validators . EMPTY_VALUES : <EOL> return None <EOL> min = None <EOL> max = None <EOL> if value [ <NUM_LIT:0> ] not in validators . EMPTY_VALUES : <EOL> try : <EOL> min = int ( value [ <NUM_LIT:0> ] ) <EOL> except ( TypeError , ValueError ) : <EOL> raise exceptions . ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> if value [ <NUM_LIT:1> ] not in validators . EMPTY_VALUES : <EOL> try : <EOL> max = int ( value [ <NUM_LIT:1> ] ) <EOL> except ( TypeError , ValueError ) : <EOL> raise exceptions . ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> return ( min , max ) <EOL> def filter ( self , name , value , queryset ) : <EOL> kwargs = { } <EOL> if value [ <NUM_LIT:0> ] : <EOL> kwargs [ '<STR_LIT>' % name ] = value [ <NUM_LIT:0> ] <EOL> if value [ <NUM_LIT:1> ] : <EOL> kwargs [ '<STR_LIT>' % name ] = value [ <NUM_LIT:1> ] <EOL> return queryset . filter ( ** kwargs ) <EOL> class FloatField ( forms . fields . FloatField ) : <EOL> def __init__ ( self , field , * args , ** kwargs ) : <EOL> super ( FloatField , self ) . __init__ ( <EOL> required = False , <EOL> widget = AdminSplitInteger , <EOL> help_text = "<STR_LIT>" % field . label . lower ( ) , <EOL> * args , ** kwargs <EOL> ) <EOL> def to_python ( self , value ) : <EOL> if value in validators . EMPTY_VALUES : <EOL> return None <EOL> if isinstance ( value , list ) : <EOL> if len ( value ) != <NUM_LIT:2> : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> if value [ <NUM_LIT:0> ] in validators . EMPTY_VALUES and value [ <NUM_LIT:1> ] in validators . EMPTY_VALUES : <EOL> return None <EOL> min = None <EOL> max = None <EOL> if value [ <NUM_LIT:0> ] not in validators . EMPTY_VALUES : <EOL> try : <EOL> min = float ( value [ <NUM_LIT:0> ] ) <EOL> except ( TypeError , ValueError ) : <EOL> raise exceptions . ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> if value [ <NUM_LIT:1> ] not in validators . EMPTY_VALUES : <EOL> try : <EOL> max = float ( value [ <NUM_LIT:1> ] ) <EOL> except ( TypeError , ValueError ) : <EOL> raise exceptions . ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> return min , max <EOL> def filter ( self , name , value , queryset ) : <EOL> kwargs = { } <EOL> if value [ <NUM_LIT:0> ] : <EOL> kwargs [ '<STR_LIT>' % name ] = value [ <NUM_LIT:0> ] <EOL> if value [ <NUM_LIT:1> ] : <EOL> kwargs [ '<STR_LIT>' % name ] = value [ <NUM_LIT:1> ] <EOL> return queryset . filter ( ** kwargs ) <EOL> class ImageField ( BasicTextField ) : <EOL> pass <EOL> class DecimalField ( forms . fields . DecimalField ) : <EOL> def __init__ ( self , field , * args , ** kwargs ) : <EOL> super ( DecimalField , self ) . __init__ ( <EOL> required = False , <EOL> widget = AdminSplitInteger , <EOL> help_text = "<STR_LIT>" % field . label . lower ( ) , <EOL> * args , ** kwargs <EOL> ) <EOL> def to_python ( self , value ) : <EOL> if value in validators . EMPTY_VALUES : <EOL> return None <EOL> if isinstance ( value , list ) : <EOL> if len ( value ) != <NUM_LIT:2> : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> if value [ <NUM_LIT:0> ] in validators . EMPTY_VALUES and value [ <NUM_LIT:1> ] in validators . EMPTY_VALUES : <EOL> return None <EOL> min = None <EOL> max = None <EOL> if value [ <NUM_LIT:0> ] not in validators . EMPTY_VALUES : <EOL> try : <EOL> min = Decimal ( value [ <NUM_LIT:0> ] ) <EOL> except ( TypeError , ValueError , InvalidOperation ) : <EOL> raise exceptions . ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> if value [ <NUM_LIT:1> ] not in validators . EMPTY_VALUES : <EOL> try : <EOL> max = Decimal ( value [ <NUM_LIT:1> ] ) <EOL> except ( TypeError , ValueError , InvalidOperation ) : <EOL> raise exceptions . ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> return min , max <EOL> def validate ( self , value ) : <EOL> if value in validators . EMPTY_VALUES : <EOL> return <EOL> return ( super ( DecimalField , self ) . validate ( value [ <NUM_LIT:0> ] ) , super ( DecimalField , self ) . validate ( value [ <NUM_LIT:1> ] ) ) <EOL> def filter ( self , name , value , queryset ) : <EOL> kwargs = { } <EOL> if value [ <NUM_LIT:0> ] : <EOL> kwargs [ '<STR_LIT>' % name ] = value [ <NUM_LIT:0> ] <EOL> if value [ <NUM_LIT:1> ] : <EOL> kwargs [ '<STR_LIT>' % name ] = value [ <NUM_LIT:1> ] <EOL> return queryset . filter ( ** kwargs ) <EOL> class AutoField ( IntegerField ) : <EOL> pass <EOL> class BigIntegerField ( IntegerField ) : <EOL> pass <EOL> class PositiveIntegerField ( IntegerField ) : <EOL> pass <EOL> class PositiveSmallIntegerField ( IntegerField ) : <EOL> pass <EOL> class SmallIntegerField ( IntegerField ) : <EOL> pass <EOL> class EmailField ( BasicTextField ) : <EOL> pass <EOL> class IPAddressField ( BasicTextField ) : <EOL> pass <EOL> class ModelMultipleChoiceField ( forms . models . ModelMultipleChoiceField ) : <EOL> def __init__ ( self , field , queryset , * args , ** kwargs ) : <EOL> super ( ModelMultipleChoiceField , self ) . __init__ ( <EOL> queryset = queryset , <EOL> required = False , <EOL> help_text = "<STR_LIT>" % field . label . lower ( ) , <EOL> * args , ** kwargs <EOL> ) <EOL> def filter ( self , name , value , queryset ) : <EOL> kwargs = { '<STR_LIT>' % name : value } <EOL> return queryset . filter ( ** kwargs ) <EOL> class ModelChoiceField ( ModelMultipleChoiceField ) : <EOL> pass <EOL> class OneToOneField ( ModelMultipleChoiceField ) : <EOL> pass <EOL> class ForeignKey ( ModelMultipleChoiceField ) : <EOL> pass <EOL> class ManyToManyField ( ModelMultipleChoiceField ) : <EOL> pass <EOL> class TextField ( BasicTextField ) : <EOL> pass <EOL> class TimeField ( forms . fields . TimeField ) : <EOL> def __init__ ( self , field , * args , ** kwargs ) : <EOL> super ( TimeField , self ) . __init__ ( <EOL> required = False , <EOL> widget = AdminSplitTime , <EOL> help_text = "<STR_LIT>" % field . label . lower ( ) , <EOL> * args , ** kwargs <EOL> ) <EOL> def to_python ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if value in validators . EMPTY_VALUES : <EOL> return None <EOL> if isinstance ( value , datetime . datetime ) : <EOL> return value . time ( ) <EOL> if isinstance ( value , datetime . time ) : <EOL> return value <EOL> if isinstance ( value , list ) : <EOL> if len ( value ) != <NUM_LIT:2> : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> if value [ <NUM_LIT:0> ] in validators . EMPTY_VALUES and value [ <NUM_LIT:1> ] in validators . EMPTY_VALUES : <EOL> return None <EOL> start_value = value [ <NUM_LIT:0> ] <EOL> end_value = value [ <NUM_LIT:1> ] <EOL> start_time = None <EOL> end_time = None <EOL> for format in self . input_formats or formats . get_format ( '<STR_LIT>' ) : <EOL> try : <EOL> start_time = datetime . datetime ( <EOL> * time . strptime ( start_value , format ) [ : <NUM_LIT:6> ] <EOL> ) . time ( ) <EOL> except ValueError : <EOL> if start_time : <EOL> continue <EOL> else : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> for format in self . input_formats or formats . get_format ( '<STR_LIT>' ) : <EOL> try : <EOL> end_time = datetime . datetime ( <EOL> * time . strptime ( end_value , format ) [ : <NUM_LIT:6> ] <EOL> ) . time ( ) <EOL> except ValueError : <EOL> if end_time : <EOL> continue <EOL> else : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> return ( start_time , end_time ) <EOL> def filter ( self , name , value , queryset ) : <EOL> kwargs = { } <EOL> if value [ <NUM_LIT:0> ] : <EOL> kwargs [ '<STR_LIT>' % name ] = value [ <NUM_LIT:0> ] <EOL> if value [ <NUM_LIT:1> ] : <EOL> kwargs [ '<STR_LIT>' % name ] = value [ <NUM_LIT:1> ] <EOL> return queryset . filter ( ** kwargs ) <EOL> class SlugField ( BasicTextField ) : <EOL> pass <EOL> class URLField ( BasicTextField ) : <EOL> pass <EOL> class XMLField ( BasicTextField ) : <EOL> pass </s>
