Permalink
Browse files

Merge PR 309

  • Loading branch information...
2 parents 49d0bd3 + 07d124e commit 9e5dab451d96abd97e2328d7e0ab5357abffc6f4 @miohtama miohtama committed Nov 19, 2016
Showing with 51 additions and 2 deletions.
  1. +2 −0 CHANGES.txt
  2. +2 −0 CONTRIBUTORS.txt
  3. +19 −0 deform/tests/test_widget.py
  4. +28 −2 deform/widget.py
View
@@ -5,6 +5,8 @@
- Added CSS class ``deform-form-buttons`` to style form button group
+- Add CSV options to ``TextAreaCSVWidget``
+
2.0.2 (2016-11-14)
------------------
View
@@ -127,3 +127,5 @@ Contributors
- Mikko Ohtamaa 2016/06/01
- Martin Peeters 2016/10/22
- Mikko Kolehmainen 2016/11/02
+- Gasper Vozel, 2016/09/15
+
@@ -1941,6 +1941,17 @@ def test_serialize_not_None_readonly(self):
self.assertEqual(renderer.kw['field'], field)
self.assertEqual(renderer.kw['cstruct'], 'a,1\r\n')
+ def test_serialize_csv_delimiter(self):
+ widget = self._makeOne(delimiter=';')
+ renderer = DummyRenderer()
+ schema = DummySchema()
+ field = DummyField(schema, renderer=renderer)
+ cstruct = [('a', '1')]
+ widget.serialize(field, cstruct)
+ self.assertEqual(renderer.template, widget.template)
+ self.assertEqual(renderer.kw['field'], field)
+ self.assertEqual(renderer.kw['cstruct'], 'a;1\r\n')
+
def test_deserialize(self):
widget = self._makeOne(strip=False)
field = DummyField()
@@ -2000,6 +2011,14 @@ def test_handle_error_children_have_msgs(self):
widget.handle_error(field, error)
self.assertEqual(field.error.msg, 'line 1: Invalid\nline 2: Invalid')
+ def test_deserialize_csv_delimiter(self):
+ widget = self._makeOne(delimiter=';')
+ field = DummyField()
+ pstruct = 'a;1\r\n'
+ result = widget.deserialize(field, pstruct)
+ self.assertEqual(result, [['a', '1']])
+
+
class TestTextInputCSVWidget(unittest.TestCase):
def _makeOne(self, **kw):
from deform.widget import TextInputCSVWidget
View
@@ -1801,6 +1801,7 @@ def deserialize(self, field, pstruct):
return result
+
class TextAreaCSVWidget(Widget):
"""
Widget used for a sequence of tuples of scalars; allows for
@@ -1826,11 +1827,26 @@ class TextAreaCSVWidget(Widget):
readonly_template
The template name used to render the widget in read-only mode.
Default: ``readonly/textarea``.
+
+ delimiter
+ The csv module delimiter character.
+ Default: ``,``.
+
+ quotechar
+ The csv module quoting character.
+ Default: ``"``.
+
+ quoting
+ The csv module quoting dialect.
+ Default: ``csv.QUOTE_MINIMAL``.
"""
template = 'textarea'
readonly_template = 'readonly/textarea'
cols = None
rows = None
+ delimiter = ','
+ quotechar = '"'
+ quoting = csv.QUOTE_MINIMAL
def serialize(self, field, cstruct, **kw):
# XXX make cols and rows overrideable
@@ -1839,7 +1855,12 @@ def serialize(self, field, cstruct, **kw):
textrows = getattr(field, 'unparseable', None)
if textrows is None:
outfile = StringIO()
- writer = csv.writer(outfile)
+ writer = csv.writer(
+ outfile,
+ delimiter=self.delimiter,
+ quotechar=self.quotechar,
+ quoting=self.quoting,
+ )
writer.writerows(cstruct)
textrows = outfile.getvalue()
readonly = kw.get('readonly', self.readonly)
@@ -1859,7 +1880,12 @@ def deserialize(self, field, pstruct):
return null
try:
infile = StringIO(pstruct)
- reader = csv.reader(infile)
+ reader = csv.reader(
+ infile,
+ delimiter=self.delimiter,
+ quotechar=self.quotechar,
+ quoting=self.quoting,
+ )
rows = list(reader)
except Exception as e:
field.unparseable = pstruct

0 comments on commit 9e5dab4

Please sign in to comment.