Commits

Anonymous committed 33cbf69

GeoPtPropertyField: add data "coercion" functionality, and associated tests. Closes #34.

  • Participants
  • Parent commits b4f1253

Comments (0)

Files changed (2)

File tests/ext_appengine/tests.py

 
 from wtforms import Form, fields as f, validators
 from wtforms.ext.appengine.db import model_form
+from wtforms.ext.appengine.fields import GeoPtPropertyField
+
+
+class DummyPostData(dict):
+    def getlist(self, key):
+        v = self[key]
+        if not isinstance(v, (list, tuple)):
+            v = [v]
+        return v
 
 
 class Author(db.Model):
         i = 0
         for key, name, value in form.author.iter_choices():
             self.assertEqual(key, keys[i])
-            i += 1
+            i += 1
+
+
+class TestFields(TestCase):
+    class GeoTestForm(Form):
+        geo = GeoPtPropertyField()
+
+    def test_geopt_property(self):
+        form = self.GeoTestForm(DummyPostData(geo='5.0, -7.0'))
+        self.assert_(form.validate())
+        self.assertEquals(form.geo.data, u'5.0,-7.0')
+        form = self.GeoTestForm(DummyPostData(geo='5.0,-f'))
+        self.assert_(not form.validate())

File wtforms/ext/appengine/fields.py

+import decimal
+
 from wtforms import fields, widgets
 
 class ReferencePropertyField(fields.SelectFieldBase):
 
 
 class GeoPtPropertyField(fields.TextField):
-    """For now, no processing or prevalidation is done."""
+
+    def process_formdata(self, valuelist):
+        if valuelist:
+            try:
+                lat, lon = valuelist[0].split(',')
+                self.data = u'%s,%s' % (decimal.Decimal(lat.strip()), decimal.Decimal(lon.strip()),)
+            except (decimal.InvalidOperation, ValueError):
+                raise ValueError(u'Not a valid coordinate location')