# Path: dockit/forms/widgets.py
# class PrimitiveListWidget(Widget):
#     '''
#     Wraps around a subfield
#     The widget receives the subfield and is responsible for rendering multiple iterations of the subfield and collecting of data
#     '''
#     def __init__(self, subfield, attrs=None):
#         self.subfield = subfield
#         super(PrimitiveListWidget, self).__init__(attrs)
#     
#     def render(self, name, value, attrs=None):
#         if not isinstance(value, list):
#             value = self.decompress(value)
#         
#         field_count = len(value)
#         final_attrs = self.build_attrs(attrs)
#         
#         field = self.subfield
#         widget = field.widget
#         
#         parts = ['<div class="list-row form-row"></div>' % widget.render(name, value[i], None) for i in range(field_count)]
#         output = u'<fieldset%s style="float: left;" class="primitivelistfield" name="%s">%s</fieldset>' % (flatatt(final_attrs), name, u''.join(parts))
#         return mark_safe(output)
#     
#     def value_from_datadict(self, data, files, name):
#         value = list()
#         if hasattr(data, 'getlist'):
#             source = data.getlist(name)
#         else:
#             source = data.get(name, [])
#         for i, entry in enumerate(source):
#             val = dict()
#             val['value'] = entry
#             val['ORDER'] = i
#             value.append(val)
#         return value
#     
#     def _has_changed(self, initial, data):
#         if initial is None:
#             initial = [u'' for x in range(0, len(data))]
#         else:
#             if not isinstance(initial, list):
#                 initial = self.decompress(initial)
#         #for widget, initial, data in zip(self.widgets, initial, data):
#         #    if widget._has_changed(initial, data):
#         #        return True
#         return True #CONSIDER where is my name?
#         return False
#     
#     def decompress(self, value):
#         """
#         Returns a list of decompressed values for the given compressed value.
#         The given value can be assumed to be valid, but not necessarily
#         non-empty.
#         """
#         raise NotImplementedError('Subclasses must implement this method.')
# 
#     def _get_media(self):
#         "Media for a multiwidget is the combination of all media of the subwidgets"
#         media = Media()
#         media += self.subfield.media
#         definition = getattr(self, 'Media', None)
#         if definition:
#             media += Media(definition)
#         return media
#     media = property(_get_media)
#     
#     def __deepcopy__(self, memo):
#         obj = super(PrimitiveListWidget, self).__deepcopy__(memo)
#         obj.subfield = copy.deepcopy(self.subfield)
#         return obj


from django.forms.fields import ChoiceField, Field, EMPTY_VALUES
from django.forms.widgets import HiddenInput, SelectMultiple, MultipleHiddenInput
from django.forms import ValidationError
from django.utils.translation import ugettext_lazy as _
from django.utils.encoding import force_unicode, smart_unicode
from django.utils import simplejson as json
from dockit.forms.widgets import PrimitiveListWidget
import inspect
    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)

class HiddenSchemaField(HiddenJSONField):
    pass

class HiddenListField(HiddenJSONField):
    pass

class HiddenDictField(HiddenJSONField):
    pass

class PrimitiveListField(Field):
    '''
    Wraps around a subfield
    The widget receives the subfield and is responsible for rendering multiple iterations of the subfield and collecting of data
    '''

# Path: dockit/forms/widgets.py
# class PrimitiveListWidget(Widget):
#     '''
#     Wraps around a subfield
#     The widget receives the subfield and is responsible for rendering multiple iterations of the subfield and collecting of data
#     '''
#     def __init__(self, subfield, attrs=None):
#         self.subfield = subfield
#         super(PrimitiveListWidget, self).__init__(attrs)
#     
#     def render(self, name, value, attrs=None):
#         if not isinstance(value, list):
#             value = self.decompress(value)
#         
#         field_count = len(value)
#         final_attrs = self.build_attrs(attrs)
#         
#         field = self.subfield
#         widget = field.widget
#         
#         parts = ['<div class="list-row form-row"></div>' % widget.render(name, value[i], None) for i in range(field_count)]
#         output = u'<fieldset%s style="float: left;" class="primitivelistfield" name="%s">%s</fieldset>' % (flatatt(final_attrs), name, u''.join(parts))
#         return mark_safe(output)
#     
#     def value_from_datadict(self, data, files, name):
#         value = list()
#         if hasattr(data, 'getlist'):
#             source = data.getlist(name)
#         else:
#             source = data.get(name, [])
#         for i, entry in enumerate(source):
#             val = dict()
#             val['value'] = entry
#             val['ORDER'] = i
#             value.append(val)
#         return value
#     
#     def _has_changed(self, initial, data):
#         if initial is None:
#             initial = [u'' for x in range(0, len(data))]
#         else:
#             if not isinstance(initial, list):
#                 initial = self.decompress(initial)
#         #for widget, initial, data in zip(self.widgets, initial, data):
#         #    if widget._has_changed(initial, data):
#         #        return True
#         return True #CONSIDER where is my name?
#         return False
#     
#     def decompress(self, value):
#         """
#         Returns a list of decompressed values for the given compressed value.
#         The given value can be assumed to be valid, but not necessarily
#         non-empty.
#         """
#         raise NotImplementedError('Subclasses must implement this method.')
# 
#     def _get_media(self):
#         "Media for a multiwidget is the combination of all media of the subwidgets"
#         media = Media()
#         media += self.subfield.media
#         definition = getattr(self, 'Media', None)
#         if definition:
#             media += Media(definition)
#         return media
#     media = property(_get_media)
#     
#     def __deepcopy__(self, memo):
#         obj = super(PrimitiveListWidget, self).__deepcopy__(memo)
#         obj.subfield = copy.deepcopy(self.subfield)
#         return obj


# Path: dockit/forms/fields.py
from django.forms.fields import ChoiceField, Field, EMPTY_VALUES
from django.forms.widgets import HiddenInput, SelectMultiple, MultipleHiddenInput
from django.forms import ValidationError
from django.utils.translation import ugettext_lazy as _
from django.utils.encoding import force_unicode, smart_unicode
from django.utils import simplejson as json
from dockit.forms.widgets import PrimitiveListWidget
import inspect

    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)

class HiddenSchemaField(HiddenJSONField):
    pass

class HiddenListField(HiddenJSONField):
    pass

class HiddenDictField(HiddenJSONField):
    pass

class PrimitiveListField(Field):
    '''
    Wraps around a subfield
    The widget receives the subfield and is responsible for rendering multiple iterations of the subfield and collecting of data
    '''

    widget = PrimitiveListWidget