Repository: wagtail/wagtail

Issue:
Formbuilder number fields do not work in 0.8
Steps to reproduce:

1) Create a form and give it a field type set to 'number'.
2) Submit the form with a number in the number field.

Result:

`Traceback (most recent call last):
  File "/usr/local/django/virtualenvs/bhfnsdwagtail/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/django/virtualenvs/bhfnsdwagtail/local/lib/python2.7/site-packages/wagtail/wagtailcore/views.py", line 27, in serve
    return page.serve(request, *args, **kwargs)
  File "/usr/local/django/virtualenvs/bhfnsdwagtail/local/lib/python2.7/site-packages/wagtail/wagtailforms/models.py", line 148, in serve
    self.process_form_submission(form)
  File "/usr/local/django/virtualenvs/bhfnsdwagtail/local/lib/python2.7/site-packages/wagtail/wagtailforms/models.py", line 190, in process_form_submission
    super(AbstractEmailForm, self).process_form_submission(form)
  File "/usr/local/django/virtualenvs/bhfnsdwagtail/local/lib/python2.7/site-packages/wagtail/wagtailforms/models.py", line 135, in process_form_submission
    form_data=json.dumps(form.cleaned_data),
  File "/usr/lib/python2.7/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python2.7/json/encoder.py", line 200, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python2.7/json/encoder.py", line 263, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python2.7/json/encoder.py", line 177, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: Decimal('5421515526') is not JSON serializable`



Discussion:

Resolving patch:
diff --git a/wagtail/wagtailforms/models.py b/wagtail/wagtailforms/models.py
--- a/wagtail/wagtailforms/models.py
+++ b/wagtail/wagtailforms/models.py
@@ -10,6 +10,7 @@
 from django.utils.translation import ugettext_lazy as _
 from django.utils.text import slugify
 from django.utils.encoding import python_2_unicode_compatible
+from django.core.serializers.json import DjangoJSONEncoder
 
 from wagtail.wagtailcore.models import Page, Orderable, UserPagePermissionsProxy, get_page_types
 from wagtail.wagtailadmin.edit_handlers import FieldPanel
@@ -132,7 +133,7 @@ def get_form_parameters(self):
 
     def process_form_submission(self, form):
         FormSubmission.objects.create(
-            form_data=json.dumps(form.cleaned_data),
+            form_data=json.dumps(form.cleaned_data, cls=DjangoJSONEncoder),
             page=self,
         )
 


Test patch:
diff --git a/wagtail/wagtailforms/tests.py b/wagtail/wagtailforms/tests.py
--- a/wagtail/wagtailforms/tests.py
+++ b/wagtail/wagtailforms/tests.py
@@ -8,7 +8,7 @@
 from wagtail.wagtailcore.models import Page
 from wagtail.wagtailforms.models import FormSubmission
 from wagtail.wagtailforms.forms import FormBuilder
-from wagtail.tests.models import FormPage
+from wagtail.tests.models import FormPage, FormField
 
 
 class TestFormSubmission(TestCase):
@@ -285,3 +285,32 @@ def test_list_submissions_csv_export(self):
         self.assertEqual(response.status_code, 200)
         data_line = response.content.decode().split("\n")[1]
         self.assertIn('new@example.com', data_line)
+
+
+class TestIssue798(TestCase):
+    fixtures = ['test.json']
+
+    def setUp(self):
+        self.client.login(username='siteeditor', password='password')
+        self.form_page = Page.objects.get(url_path='/home/contact-us/').specific
+
+        # Add a number field to the page
+        FormField.objects.create(
+            page=self.form_page,
+            label="Your favourite number",
+            field_type='number',
+        )
+
+    def test_post(self):
+        response = self.client.post('/contact-us/', {
+            'your-email': 'bob@example.com',
+            'your-message': 'hello world',
+            'your-choices': {'foo': '', 'bar': '', 'baz': ''},
+            'your-favourite-number': '7.3',
+        })
+
+        # Check response
+        self.assertTemplateUsed(response, 'tests/form_page_landing.html')
+
+        # Check that form submission was saved correctly
+        self.assertTrue(FormSubmission.objects.filter(page=self.form_page, form_data__contains='7.3').exists())