# Path: dockit/forms/fields.py
# class HiddenJSONField(Field):
#     widget = HiddenInput
#     
#     def to_python(self, value):
#         if value and isinstance(value, basestring):
#             try:
#                 return json.loads(value)
#             except ValueError:
#                 raise ValidationError("Invalid JSON")#TODO more descriptive error?
#         return None
# 
#     def validate(self, value):
#         return Field.validate(self, value)
#     
#     def prepare_value(self, value):
#         if hasattr(value, 'to_primitive'):
#             return json.dumps(type(value).to_primitive(value))
#         return Field.prepare_value(self, value)


from django.forms import widgets
from django.utils.encoding import force_unicode
from django.forms.util import flatatt
from django.utils.safestring import mark_safe
from django.utils.html import escape
from django.utils import simplejson as json
from dockit.forms.fields import HiddenJSONField
from urllib import urlencode
            return escape(force_unicode(value))
        return ''
    
    def render(self, name, value, attrs=None):
        path_parts = list()
        if self.dotpath:
            path_parts.append(self.dotpath)
        path_parts.append(name) #TODO consider this will break if there is a prefix!
        dotpath = '.'.join(path_parts)
        json_value = self.prep_value(value)
        
        data_attrs = self.build_attrs(attrs, type='hidden', name=name, value=json_value)
        data_html = mark_safe(u'<input%s />' % flatatt(data_attrs))
        
        if isinstance(value, list):
            rows = list()
            index = -1
            for index, item in enumerate(value):
                item_dotpath = '%s.%s' % (dotpath, index)
                butn_html = self.render_button(item_dotpath, edit=True)
                rows.append('<td>%s</td><td>%s</td>' % (self.get_label(item_dotpath, item), butn_html))
            item_dotpath = '%s.%s' % (dotpath, index+1)
            butn_html = self.render_button(item_dotpath)
            rows.append('<td>%s</td><td>%s</td>' % (self.get_label(item_dotpath), butn_html))
            return mark_safe('%s<table><tr>%s</tr></table>' % (data_html, '</tr><tr>'.join(rows)))
        else:
            butn_html = self.render_button(dotpath, edit=bool(value))
            desc_html = self.get_label(dotpath, value)
            return mark_safe(''.join((data_html, desc_html, butn_html)))


# Path: dockit/forms/fields.py
# class HiddenJSONField(Field):
#     widget = HiddenInput
#     
#     def to_python(self, value):
#         if value and isinstance(value, basestring):
#             try:
#                 return json.loads(value)
#             except ValueError:
#                 raise ValidationError("Invalid JSON")#TODO more descriptive error?
#         return None
# 
#     def validate(self, value):
#         return Field.validate(self, value)
#     
#     def prepare_value(self, value):
#         if hasattr(value, 'to_primitive'):
#             return json.dumps(type(value).to_primitive(value))
#         return Field.prepare_value(self, value)


# Path: dockit/admin/fields.py
from django.forms import widgets
from django.utils.encoding import force_unicode
from django.forms.util import flatatt
from django.utils.safestring import mark_safe
from django.utils.html import escape
from django.utils import simplejson as json
from dockit.forms.fields import HiddenJSONField
from urllib import urlencode

            return escape(force_unicode(value))
        return ''
    
    def render(self, name, value, attrs=None):
        path_parts = list()
        if self.dotpath:
            path_parts.append(self.dotpath)
        path_parts.append(name) #TODO consider this will break if there is a prefix!
        dotpath = '.'.join(path_parts)
        json_value = self.prep_value(value)
        
        data_attrs = self.build_attrs(attrs, type='hidden', name=name, value=json_value)
        data_html = mark_safe(u'<input%s />' % flatatt(data_attrs))
        
        if isinstance(value, list):
            rows = list()
            index = -1
            for index, item in enumerate(value):
                item_dotpath = '%s.%s' % (dotpath, index)
                butn_html = self.render_button(item_dotpath, edit=True)
                rows.append('<td>%s</td><td>%s</td>' % (self.get_label(item_dotpath, item), butn_html))
            item_dotpath = '%s.%s' % (dotpath, index+1)
            butn_html = self.render_button(item_dotpath)
            rows.append('<td>%s</td><td>%s</td>' % (self.get_label(item_dotpath), butn_html))
            return mark_safe('%s<table><tr>%s</tr></table>' % (data_html, '</tr><tr>'.join(rows)))
        else:
            butn_html = self.render_button(dotpath, edit=bool(value))
            desc_html = self.get_label(dotpath, value)
            return mark_safe(''.join((data_html, desc_html, butn_html)))


class DotPathField(HiddenJSONField):