Commits

Mark Lavin committed 09ae5aa

Working on initial values and multi-selects.

  • Participants
  • Parent commits 65c127a

Comments (0)

Files changed (5)

File example/core/views.py

     if request.method == 'POST':
         form = FruitForm(request.POST)
     else:
-        form = FruitForm()
+        if request.GET:
+            form = FruitForm(initial=request.GET)
+        else:
+            form = FruitForm()
 
     return render_to_response('base.html', {'form': form}, context_instance=RequestContext(request))

File selectable/forms/fields.py

         kwargs['widget'] = self.widget(lookup_class)
         super(AutoCompleteSelectMultipleField, self).__init__(*args, **kwargs)
 
-
     def to_python(self, value):
         if value in EMPTY_VALUES:
             return None
-        if isinstance(value, list):
-            # Input comes from an AutoComplete widget. It's two
-            # components: text and id
-            if len(value) != 2:
-                raise ValidationError(self.error_messages['invalid_choice'])
-            lookup =self.lookup_class()
-            if value[1] in EMPTY_VALUES:
-                return None
-            else:
-                ids = value[1]
-                if not isinstance(ids, list):
-                    ids = [ids]
-                value = []
-                for v in ids:
-                    item = lookup.get_item(v)
-                    if item is None:
-                        raise ValidationError(self.error_messages['invalid_choice'])
-                    value.append(item)
-        return value
+        lookup = self.lookup_class()
+        items = []
+        for v in value:
+            if v not in EMPTY_VALUES:
+                item = lookup.get_item(v)
+                if item is None:
+                    raise ValidationError(self.error_messages['invalid_choice'])
+                items.append(item)
+        return items
 
 
 class AutoComboboxSelectMultipleField(AutoCompleteSelectMultipleField):

File selectable/forms/widgets.py

     def decompress(self, value):
         if value:
             lookup = self.lookup_class()
-            item = lookup.get_item(value)
+            model = getattr(self.lookup_class, 'model', None)
+            if model and isinstance(value, model):
+                item = value
+                value = lookup.get_item_id(item)
+            else:
+                item = lookup.get_item(value)
             item_value = lookup.get_item_value(item)
             return [item_value, value]
         return [None, None]
     def decompress(self, value):
         if value:
             lookup = self.lookup_class()
-            item = lookup.get_item(value)
+            model = getattr(self.lookup_class, 'model', None)
+            if model and isinstance(value, model):
+                item = value
+                value = lookup.get_item_id(item)
+            else:
+                item = lookup.get_item(value)
             item_value = lookup.get_item_value(item)
             return [item_value, value]
         return [None, None]
         final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
         id_ = final_attrs.get('id', None)
         inputs = []
+        model = getattr(self.lookup_class, 'model', None)
         for i, v in enumerate(value):
+            item = None
+            if model and isinstance(v, model):
+                item = v
+                v = lookup.get_item_id(item)
             input_attrs = dict(value=force_unicode(v), **final_attrs)
             if id_:
                 # An ID attribute was given. Add a numeric index as a suffix
                 # so that the inputs don't all have the same ID attribute.
                 input_attrs['id'] = '%s_%s' % (id_, i)
             if v:
-                item = lookup.get_item(v)
+                item = item or lookup.get_item(v)
                 input_attrs['title'] = lookup.get_item_value(item)
             inputs.append(u'<input%s />' % flatatt(input_attrs))
         return mark_safe(u'\n'.join(inputs))
         ]
         super(AutoCompleteSelectMultipleWidget, self).__init__(widgets, *args, **kwargs)
 
-    def decompress(self, value):
-        if value and isinstance(value, list) and len(value) == 2 and isinstance(value[1], list):
-            return value
-        if value:
-            if not hasattr(value, '__iter__'):
-                value = [value]
-            return [None, value]
-        return [None, None]
+    def value_from_datadict(self, data, files, name):
+        return self.widgets[1].value_from_datadict(data, files, name + '_1')
 
     def render(self, name, value, attrs=None):
-        value = self.decompress(value)
+        if value and not hasattr(value, '__iter__'):
+            value = [value]
+        value = [u'', value]
         return super(AutoCompleteSelectMultipleWidget, self).render(name, value, attrs)
 
 
         ]
         super(AutoComboboxSelectMultipleWidget, self).__init__(widgets, *args, **kwargs)
 
-    def decompress(self, value):
-        if value and isinstance(value, list) and len(value) == 2 and isinstance(value[1], list):
-            return value
-        if value:
-            if not hasattr(value, '__iter__'):
-                value = [value]
-            return [None, value]
-        return [None, None]
+    def value_from_datadict(self, data, files, name):
+        return self.widgets[1].value_from_datadict(data, files, name + '_1')
 
     def render(self, name, value, attrs=None):
-        value = self.decompress(value)
+        if value and not hasattr(value, '__iter__'):
+            value = [value]
+        value = [u'', value]
         return super(AutoComboboxSelectMultipleWidget, self).render(name, value, attrs)
 

File selectable/static/js/jquery.dj.selectable.js

             var self = $(this),
             input = this.element;
             type = $(input).attr('data-selectable-type');
-            url = $(input).attr('data-selectable-url');
+            
             
             var allowAttr = $(input).attr('data-selectable-allow-new');
             var allowNew = false;

File selectable/tests/fields.py

     def test_clean(self):
         thing = self.create_thing()
         field = self.get_field_instance()
-        value = field.clean([thing.name, thing.id])
+        value = field.clean([thing.id])
         self.assertEqual([thing], value)
 
     def test_clean_multiple(self):
         thing = self.create_thing()
         other_thing = self.create_thing()
         field = self.get_field_instance()
-        names = [thing.name, other_thing.name]
         ids = [thing.id, other_thing.id]
-        value = field.clean([names, ids])
+        value = field.clean(ids)
         self.assertEqual([thing, other_thing], value)
 
 
     def test_clean(self):
         thing = self.create_thing()
         field = self.get_field_instance()
-        value = field.clean([thing.name, thing.id])
+        value = field.clean([thing.id])
         self.assertEqual([thing], value)
 
     def test_clean_multiple(self):
         thing = self.create_thing()
         other_thing = self.create_thing()
         field = self.get_field_instance()
-        names = [thing.name, other_thing.name]
         ids = [thing.id, other_thing.id]
-        value = field.clean([names, ids])
+        value = field.clean(ids)
         self.assertEqual([thing, other_thing], value)