<s> from django . forms import Field <EOL> from django . contrib . admin . widgets import AdminTextareaWidget <EOL> from django . core . exceptions import ValidationError <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from . import util <EOL> from . widgets import KeyValueWidget <EOL> import json <EOL> class JsonMixin ( object ) : <EOL> def to_python ( self , value ) : <EOL> try : <EOL> if value is not None : <EOL> return json . loads ( value ) <EOL> except TypeError : <EOL> raise ValidationError ( _ ( u'<STR_LIT>' ) ) <EOL> except ValueError : <EOL> raise ValidationError ( _ ( u'<STR_LIT>' ) ) <EOL> def value_from_datadict ( self , data , files , name ) : <EOL> value = data . get ( name , None ) <EOL> try : <EOL> value = json . dumps ( json . loads ( value ) , sort_keys = True ) <EOL> except ( TypeError , ValueError ) : <EOL> pass <EOL> return value <EOL> class DictionaryFieldWidget ( JsonMixin , AdminTextareaWidget ) : <EOL> def render ( self , name , value , attrs = None ) : <EOL> if value : <EOL> try : <EOL> value = json . dumps ( json . loads ( value ) , sort_keys = True , indent = <NUM_LIT:2> ) <EOL> except ValueError : <EOL> pass <EOL> return super ( JsonMixin , self ) . render ( name , value , attrs ) <EOL> class ReferencesFieldWidget ( JsonMixin , AdminTextareaWidget ) : <EOL> def render ( self , name , value , attrs = None ) : <EOL> value = util . serialize_references ( value ) <EOL> return super ( ReferencesFieldWidget , self ) . render ( name , value , attrs ) <EOL> class DictionaryField ( JsonMixin , Field ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ** params ) : <EOL> defaults = { <EOL> '<STR_LIT>' : KeyValueWidget , <EOL> '<STR_LIT>' : u'<STR_LIT:{}>' , <EOL> } <EOL> defaults . update ( params ) <EOL> super ( DictionaryField , self ) . __init__ ( ** defaults ) <EOL> class ReferencesField ( JsonMixin , Field ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ** params ) : <EOL> params [ '<STR_LIT>' ] = ReferencesFieldWidget <EOL> super ( ReferencesField , self ) . __init__ ( ** params ) <EOL> def to_python ( self , value ) : <EOL> value = super ( ReferencesField , self ) . to_python ( value ) <EOL> return util . unserialize_references ( value ) </s>
