<s> from __future__ import unicode_literals <EOL> from datetime import datetime <EOL> from decimal import Decimal <EOL> from django . core . exceptions import ValidationError <EOL> from . . utils import digattr <EOL> try : <EOL> from django . utils . encoding import force_text <EOL> except ImportError : <EOL> from django . utils . encoding import force_unicode as force_text <EOL> class NoDefault ( object ) : <EOL> '''<STR_LIT>''' <EOL> class Field ( object ) : <EOL> type_class = None <EOL> def __init__ ( self , attribute = None , default = NoDefault , readonly = False , <EOL> null = True , virtual = False , ** kwargs ) : <EOL> self . attribute = attribute <EOL> self . default = default <EOL> self . readonly = readonly <EOL> self . null = null <EOL> self . virtual = virtual <EOL> self . kwargs = kwargs <EOL> def _get_attrname ( self , name ) : <EOL> return self . attribute if self . attribute else name <EOL> def reduce ( self , value , ** kwargs ) : <EOL> return value <EOL> def restore ( self , value , ** kwargs ) : <EOL> if self . type_class is not None : <EOL> return self . type_class ( value ) <EOL> return value <EOL> def deflate ( self , name , obj , data , ** kwargs ) : <EOL> src = self . _get_attrname ( name ) <EOL> value = digattr ( obj , src , self . default ) <EOL> if value is NoDefault : <EOL> if self . virtual : <EOL> return <EOL> raise AttributeError ( '<STR_LIT>' % ( src , obj , ) ) <EOL> if value is not None : <EOL> value = self . reduce ( value , ** kwargs ) <EOL> data [ name ] = value <EOL> def inflate ( self , name , data , obj , ** kwargs ) : <EOL> if self . readonly : <EOL> return <EOL> dest = self . _get_attrname ( name ) <EOL> try : <EOL> value = data [ name ] <EOL> except KeyError : <EOL> if self . default is not NoDefault and '<STR_LIT>' not in kwargs : <EOL> obj [ dest ] = self . default <EOL> return <EOL> if value is not None : <EOL> try : <EOL> value = self . restore ( value , ** kwargs ) <EOL> except ValueError : <EOL> raise ValidationError ( <EOL> "<STR_LIT>" % ( <EOL> name , self . __class__ . __name__ , value <EOL> ) <EOL> ) <EOL> elif not self . null : <EOL> raise ValidationError ( "<STR_LIT>" % name ) <EOL> obj [ dest ] = value <EOL> class BooleanField ( Field ) : <EOL> type_class = bool <EOL> class IntegerField ( Field ) : <EOL> type_class = int <EOL> class DecimalField ( Field ) : <EOL> type_class = Decimal <EOL> def reduce ( self , value , ** kwargs ) : <EOL> return float ( value ) <EOL> class DateTimeField ( Field ) : <EOL> def reduce ( self , value , ** kwargs ) : <EOL> return value . replace ( microsecond = <NUM_LIT:0> ) . isoformat ( str ( '<STR_LIT:U+0020>' ) ) <EOL> def restore ( self , value , ** kwargs ) : <EOL> return datetime . strptime ( value , '<STR_LIT>' ) <EOL> class DateField ( Field ) : <EOL> def reduce ( self , value , ** kwargs ) : <EOL> return value . isoformat ( ) <EOL> def restore ( self , value , ** kwargs ) : <EOL> return datetime . strptime ( value , '<STR_LIT>' ) . date ( ) <EOL> class TimeField ( Field ) : <EOL> def reduce ( self , value , ** kwargs ) : <EOL> return value . isoformat ( ) <EOL> def restore ( self , value , ** kwargs ) : <EOL> return datetime . strptime ( value , '<STR_LIT>' ) . time ( ) <EOL> class SerialiserField ( Field ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( SerialiserField , self ) . __init__ ( * args , ** kwargs ) <EOL> self . serialiser = kwargs [ '<STR_LIT>' ] <EOL> def reduce ( self , value , ** kwargs ) : <EOL> return self . serialiser . object_deflate ( value , ** kwargs ) <EOL> def restore ( self , value , ** kwargs ) : <EOL> return self . serialiser . object_inflate ( value , ** kwargs ) <EOL> class ManySerialiserField ( Field ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( ManySerialiserField , self ) . __init__ ( * args , ** kwargs ) <EOL> self . serialiser = kwargs [ '<STR_LIT>' ] <EOL> def reduce ( self , value , ** kwargs ) : <EOL> return self . serialiser . list_deflate ( value , ** kwargs ) <EOL> def restore ( self , value , ** kwargs ) : <EOL> return self . serialiser . list_inflate ( value , ** kwargs ) <EOL> class FileField ( Field ) : <EOL> def reduce ( self , value , ** kwargs ) : <EOL> return value . url <EOL> def restore ( self , value , ** kwargs ) : <EOL> pass <EOL> class StringField ( Field ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> kwargs . setdefault ( '<STR_LIT>' , True ) <EOL> super ( StringField , self ) . __init__ ( * args , ** kwargs ) <EOL> def reduce ( self , value , ** kwargs ) : <EOL> return force_text ( value ) </s>
