Code

- Fix StringIO issue2550713: io.StringIO in newer versions of python
authorschlatterbeck <schlatterbeck@57a73879-2fb5-44c3-a270-3262357dd7e2>
Fri, 15 Jul 2011 12:36:47 +0000 (12:36 +0000)
committerschlatterbeck <schlatterbeck@57a73879-2fb5-44c3-a270-3262357dd7e2>
Fri, 15 Jul 2011 12:36:47 +0000 (12:36 +0000)
  returns unicode strings and expects a unicode string in the
  constructor. Unfortunately csv  doesn't handle unicode (yet). So we
  need to use a BytesIO which gets the utf-8 string from the
  web-interface. Compatibility for old versions by using
  Stringio.Stringio for emulating a io.BytesIO also works.
- We didn't have a regression test for the EditCSVAction

git-svn-id: http://svn.roundup-tracker.org/svnroot/roundup/roundup/trunk@4633 57a73879-2fb5-44c3-a270-3262357dd7e2

roundup/anypy/io_.py
roundup/cgi/actions.py
test/test_cgi.py

index 86f8023d83da471c02f5d9684f27c298b8aeda2b..5d6e52e2512031fe456286c25250146c292572cc 100644 (file)
@@ -1,6 +1,7 @@
 
 try:
-    from io import StringIO
+    from io import StringIO, BytesIO
 except:
     from StringIO import StringIO
+    BytesIO = StringIO
 
index d48fb595eb75514bd47c74a4bd8abbbfefec8466..86bd4f1f112a5e2e82eb936ad6d0a185da6033bd 100755 (executable)
@@ -297,7 +297,7 @@ class EditCSVAction(Action):
         props = ['id'] + props_without_id
 
         # do the edit
-        rows = io_.StringIO(self.form['rows'].value)
+        rows = io_.BytesIO(self.form['rows'].value)
         reader = csv.reader(rows)
         found = {}
         line = 0
index 7fbe89ce442440091c434b23a51bff5a84ffe755..c99d9e8cfa51f386e8cd403a7a9e8f8bcf8a3141 100644 (file)
@@ -878,6 +878,22 @@ class FormTestCase(unittest.TestCase):
         h = HTMLRequest(cl)
         self.assertEqual([x.id for x in h.batch()],['1', '2', '3'])
 
+    def testEditCSV(self):
+        form = dict(rows='id,name\n1,newkey')
+        cl = self._make_client(form, userid='1', classname='keyword')
+        cl.ok_message = []
+        actions.EditCSVAction(cl).handle()
+        self.assertEqual(cl.ok_message, ['Items edited OK'])
+        k = self.db.keyword.getnode('1')
+        self.assertEqual(k.name, 'newkey')
+        form = dict(rows=u'id,name\n1,\xe4\xf6\xfc'.encode('utf-8'))
+        cl = self._make_client(form, userid='1', classname='keyword')
+        cl.ok_message = []
+        actions.EditCSVAction(cl).handle()
+        self.assertEqual(cl.ok_message, ['Items edited OK'])
+        k = self.db.keyword.getnode('1')
+        self.assertEqual(k.name, u'\xe4\xf6\xfc'.encode('utf-8'))
+
     def testRoles(self):
         cl = self._make_client({})
         self.db.user.set('1', roles='aDmin,    uSer')