Opened 4 years ago

Closed 4 years ago

#6999 closed defect (fixed)

[Patch] CSV export crash + fix

Reported by: mudry Owned by: coling
Priority: normal Component: WorkLogPlugin
Severity: normal Keywords:
Cc: Trac Release: 0.12


I tried the WorkLog plugin with current trac 0.12 branch. Works fine so far, but CSV export gave me an exception on Content-Length not being set for the request. Here's a fix as patch agains the 0.11 folder. To evade unicode trouble, I've copied the way trac does CSV (but not the RSS and query stuff) - it has the additional benefit of using Python's csv module. Only tested with trac 0.12..

Index: worklog/
--- worklog/	(revision 7854)
+++ worklog/	(working copy)
@@ -1,4 +1,7 @@
 import re
+from StringIO import StringIO
+import codecs
+import csv
 from util import *
 from time import time
 from datetime import tzinfo, timedelta, datetime
@@ -53,19 +56,20 @@
-      req.write(sep.join(fields) + CRLF)
+      content = StringIO()
+      writer = csv.writer(content, delimiter=sep, quoting=csv.QUOTE_MINIMAL)
+      writer.writerow([unicode(c).encode('utf-8') for c in fields])
       # Rows
       for row in log:
-        first = True
+        values=[]
         for field in fields:
-          if not first:
-            req.write(sep)
-          first = False
-          req.write(str(row[field])
-                    .replace(sep, '_').replace('\\', '\\\\')
-                    .replace('\n', '\\n').replace('\r', '\\r'))
-        req.write(CRLF)
+          values.append(unicode(row[field]).encode('utf-8'))
+        writer.writerow(values)
+      req.send_header('Content-Length', content.len)
+      req.write(content.getvalue())
     # IRequestHandler methods
     def match_request(self, req):

Attachments (0)

Change History (3)

comment:1 Changed 4 years ago by mudry

  • Owner changed from colin to coling

comment:2 Changed 4 years ago by rjollos

  • Summary changed from CSV export crash + fix to [Patch] CSV export crash + fix

#6998 closed as a duplicate.

comment:3 Changed 4 years ago by coling

  • Resolution set to fixed
  • Status changed from new to closed

(In [9538]) Fix CSV export regarding utf8 encoding and other issues.

Closes #6999 (thanks for the patch and sorry for the delay)

Add Comment

Modify Ticket

as closed .
The resolution will be deleted. Next status will be 'reopened'.

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.