<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> __copyright__ = '<STR_LIT>' <EOL> __version__ = '<STR_LIT>' <EOL> import json <EOL> from django import forms <EOL> from django . forms . widgets import flatatt <EOL> from django . utils . encoding import force_unicode <EOL> from django . utils . safestring import mark_safe <EOL> class JsonPairWidget ( forms . Widget ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> key_attrs = { } <EOL> val_attrs = { } <EOL> if "<STR_LIT>" in kwargs : <EOL> key_attrs = kwargs . pop ( "<STR_LIT>" ) <EOL> if "<STR_LIT>" in kwargs : <EOL> val_attrs = kwargs . pop ( "<STR_LIT>" ) <EOL> if "<STR_LIT:class>" not in key_attrs : <EOL> key_attrs [ '<STR_LIT:class>' ] = '<STR_LIT>' <EOL> if "<STR_LIT:class>" not in val_attrs : <EOL> val_attrs [ '<STR_LIT:class>' ] = '<STR_LIT>' <EOL> key_attrs [ '<STR_LIT:class>' ] = '<STR_LIT:U+0020>' . join ( [ '<STR_LIT>' , key_attrs [ '<STR_LIT:class>' ] ] ) <EOL> val_attrs [ '<STR_LIT:class>' ] = '<STR_LIT:U+0020>' . join ( [ '<STR_LIT>' , val_attrs [ '<STR_LIT:class>' ] ] ) <EOL> self . attrs = { '<STR_LIT>' : key_attrs , '<STR_LIT>' : val_attrs } <EOL> super ( forms . Widget , self ) . __init__ ( * args , ** kwargs ) <EOL> def render ( self , name , value , attrs = None ) : <EOL> try : <EOL> data = json . loads ( force_unicode ( value ) ) <EOL> except : <EOL> data = { } <EOL> output = '<STR_LIT>' <EOL> for k , v in data . items ( ) : <EOL> output += self . render_pair ( k , v , name ) <EOL> output += self . render_pair ( '<STR_LIT>' , '<STR_LIT>' , name ) <EOL> return mark_safe ( output ) <EOL> def render_pair ( self , key , value , name ) : <EOL> ctx = { <EOL> '<STR_LIT:key>' : key , <EOL> '<STR_LIT:value>' : value , <EOL> '<STR_LIT>' : name , <EOL> '<STR_LIT>' : flatatt ( self . attrs [ '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' : flatatt ( self . attrs [ '<STR_LIT>' ] ) <EOL> } <EOL> return '<STR_LIT>' % ctx <EOL> def value_from_datadict ( self , data , files , name ) : <EOL> jsontext = "<STR_LIT>" <EOL> if data . has_key ( '<STR_LIT>' % name ) and data . has_key ( '<STR_LIT>' % name ) : <EOL> keys = data . getlist ( "<STR_LIT>" % name ) <EOL> values = data . getlist ( "<STR_LIT>" % name ) <EOL> data = { } <EOL> for key , value in zip ( keys , values ) : <EOL> if len ( key ) > <NUM_LIT:0> : <EOL> data [ key ] = value <EOL> jsontext = json . dumps ( data ) <EOL> return jsontext </s>
