Commits

Mark Lavin  committed a1bf7a7

Better error handling for invalid PK submission when using AutoCompleteSelectField and AutoCompleteSelectMultipleField.

  • Participants
  • Parent commits e1f7f89

Comments (0)

Files changed (3)

File selectable/base.py

         item = None
         if value:
             try:
-                item = self.get_queryset().filter(pk=value)[0]
-            except IndexError:
-                pass
+                item = self.get_queryset().get(pk=value)
+            except (ValueError, self.model.DoesNotExist):
+                item = None
         return item
 
     def create_item(self, value):

File selectable/tests/fields.py

         field = self.get_field_instance(widget=widget)
         self.assertTrue(isinstance(field.widget, widgets.AutoComboboxWidget))
 
+    def test_invalid_pk(self):
+        field = self.get_field_instance()
+        value = self.get_random_string()
+        self.assertRaises(forms.ValidationError, field.clean, [value, 'XXX'])
+
 
 class AutoCompleteSelectMultipleFieldTestCase(BaseFieldTestCase):
     field_cls = fields.AutoCompleteSelectMultipleField
         widget = widgets.AutoComboboxSelectMultipleWidget(self.lookup_cls)
         field = self.get_field_instance(widget=widget)
         self.assertTrue(isinstance(field.widget, widgets.AutoComboboxSelectMultipleWidget))
+
+    def test_invalid_pk(self):
+        field = self.get_field_instance()
+        value = self.get_random_string()
+        self.assertRaises(forms.ValidationError, field.clean, ['XXX', ])

File selectable/tests/functests.py

         self.test_thing = self.create_thing()
 
     def test_valid_form(self):
-        "Valid form using an AutoCompleteSelectField."
+        "Valid form using an AutoCompleteSelectWidget."
         data = {
             'name': self.get_random_string(),
             'thing_0': self.test_thing.name, # Text input
         form = SelectWidgetForm(data=data)
         self.assertTrue(form.is_valid(), str(form.errors))
 
+    def test_missing_pk(self):
+        "Invalid form (missing required pk) using an AutoCompleteSelectWidget."
+        data = {
+            'name': self.get_random_string(),
+            'thing_0': self.test_thing.name, # Text input
+            'thing_1': u'', # Hidden input missing
+        }
+        form = SelectWidgetForm(data=data)
+        self.assertFalse(form.is_valid())
+        self.assertTrue('thing' in form.errors)
+
+    def test_invalid_pk(self):
+        "Invalid form (invalid pk value) using an AutoCompleteSelectWidget."
+        data = {
+            'name': self.get_random_string(),
+            'thing_0': self.test_thing.name, # Text input
+            'thing_1': u'XXX', # Hidden input doesn't match a PK
+        }
+        form = SelectWidgetForm(data=data)
+        self.assertFalse(form.is_valid())
+        self.assertTrue('thing' in form.errors)
+
 
 class ComboboxSelectWidgetForm(forms.ModelForm):
 
         self.test_thing = self.create_thing()
 
     def test_valid_form(self):
-        "Valid form using an AutoCompleteSelectField."
+        "Valid form using an AutoComboboxSelectWidget."
         data = {
             'name': self.get_random_string(),
             'thing_0': self.test_thing.name, # Text input
         form = ComboboxSelectWidgetForm(data=data)
         self.assertTrue(form.is_valid(), str(form.errors))
 
+    def test_missing_pk(self):
+        "Invalid form (missing required pk) using an AutoComboboxSelectWidget."
+        data = {
+            'name': self.get_random_string(),
+            'thing_0': self.test_thing.name, # Text input
+            'thing_1': u'', # Hidden input missing
+        }
+        form = SelectWidgetForm(data=data)
+        self.assertFalse(form.is_valid())
+        self.assertTrue('thing' in form.errors)
+
+    def test_invalid_pk(self):
+        "Invalid form (invalid pk value) using an AutoComboboxSelectWidget."
+        data = {
+            'name': self.get_random_string(),
+            'thing_0': self.test_thing.name, # Text input
+            'thing_1': u'XXX', # Hidden input doesn't match a PK
+        }
+        form = SelectWidgetForm(data=data)
+        self.assertFalse(form.is_valid())
+        self.assertTrue('thing' in form.errors)
+
 
 class ManyThingForm(forms.ModelForm):