<s> from __future__ import unicode_literals <EOL> from . import widgets <EOL> from django . core . exceptions import ObjectDoesNotExist <EOL> from django . db . models . manager import Manager <EOL> from django . db . models . fields import NOT_PROVIDED <EOL> class Field ( object ) : <EOL> """<STR_LIT>""" <EOL> empty_values = [ None , '<STR_LIT>' ] <EOL> def __init__ ( self , attribute = None , column_name = None , widget = None , <EOL> default = NOT_PROVIDED , readonly = False ) : <EOL> self . attribute = attribute <EOL> self . default = default <EOL> self . column_name = column_name <EOL> if not widget : <EOL> widget = widgets . Widget ( ) <EOL> self . widget = widget <EOL> self . readonly = readonly <EOL> def __repr__ ( self ) : <EOL> """<STR_LIT>""" <EOL> path = '<STR_LIT>' % ( self . __class__ . __module__ , self . __class__ . __name__ ) <EOL> column_name = getattr ( self , '<STR_LIT>' , None ) <EOL> if column_name is not None : <EOL> return '<STR_LIT>' % ( path , column_name ) <EOL> return '<STR_LIT>' % path <EOL> def clean ( self , data ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> value = data [ self . column_name ] <EOL> except KeyError : <EOL> raise KeyError ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( self . column_name , <EOL> list ( data . keys ( ) ) ) ) <EOL> try : <EOL> value = self . widget . clean ( value ) <EOL> except ValueError as e : <EOL> raise ValueError ( "<STR_LIT>" % ( self . column_name , e ) ) <EOL> if value in self . empty_values and self . default != NOT_PROVIDED : <EOL> if callable ( self . default ) : <EOL> return self . default ( ) <EOL> return self . default <EOL> return value <EOL> def get_value ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> if self . attribute is None : <EOL> return None <EOL> attrs = self . attribute . split ( '<STR_LIT>' ) <EOL> value = obj <EOL> for attr in attrs : <EOL> try : <EOL> value = getattr ( value , attr , None ) <EOL> except ( ValueError , ObjectDoesNotExist ) : <EOL> return None <EOL> if value is None : <EOL> return None <EOL> if callable ( value ) and not isinstance ( value , Manager ) : <EOL> value = value ( ) <EOL> return value <EOL> def save ( self , obj , data ) : <EOL> """<STR_LIT>""" <EOL> if not self . readonly : <EOL> attrs = self . attribute . split ( '<STR_LIT>' ) <EOL> for attr in attrs [ : - <NUM_LIT:1> ] : <EOL> obj = getattr ( obj , attr , None ) <EOL> setattr ( obj , attrs [ - <NUM_LIT:1> ] , self . clean ( data ) ) <EOL> def export ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> value = self . get_value ( obj ) <EOL> if value is None : <EOL> return "<STR_LIT>" <EOL> return self . widget . render ( value ) </s>
