| | 160 | class JsonInputs(Widget): |
| | 161 | def render(self, name, value, attrs=None): |
| | 162 | import simplejson |
| | 163 | if value is None: value = '{}' |
| | 164 | value = simplejson.loads(force_unicode(value)) |
| | 165 | ret = '' |
| | 166 | if value and len(value) > 0: |
| | 167 | for key in value.keys(): |
| | 168 | ret += '<input type="text" name="json_key[]" value="%s"> <input type="text" name="json_value[]" value="%s"><br />' % (key, value[key]) |
| | 169 | ret += '<input type="text" name="json_key[]"> <input type="text" name="json_value[]">' |
| | 170 | return mark_safe(ret) |
| | 171 | def value_from_datadict(self, data, files, name): |
| | 172 | json = data.copy() |
| | 173 | if json.has_key('json_key[]') and json.has_key('json_value[]'): |
| | 174 | keys = json.getlist("json_key[]") |
| | 175 | values = json.getlist("json_value[]") |
| | 176 | jsonDict = {} |
| | 177 | for (key, value) in map(None, keys, values): |
| | 178 | if len(key) > 0: |
| | 179 | jsonDict[key] = value |
| | 180 | import simplejson |
| | 181 | text = simplejson.dumps(jsonDict) |
| | 182 | json['text'] = text |
| | 183 | return super(JsonInputs, self).value_from_datadict(json, files, name) |
| | 184 | |