Hi,

Pls find attached patch for csv download issue when data contains non ASCII
characters. And also when table name contains non ASCII characters

-- 
*Harshal Dhumal*
*Sr. Software Engineer*

EnterpriseDB India: http://www.enterprisedb.com
The Enterprise PostgreSQL Company
diff --git a/web/pgadmin/tools/sqleditor/__init__.py b/web/pgadmin/tools/sqleditor/__init__.py
index d114988..cdd790e 100644
--- a/web/pgadmin/tools/sqleditor/__init__.py
+++ b/web/pgadmin/tools/sqleditor/__init__.py
@@ -12,6 +12,7 @@ import simplejson as json
 import os
 import pickle
 import random
+import time
 
 from flask import Response, url_for, render_template, session, request
 from flask_babel import gettext
@@ -1372,9 +1373,12 @@ def start_query_download_tool(trans_id):
                 r = Response(gen(), mimetype='text/csv')
 
                 if 'filename' in data and data['filename'] != "":
-                    filename = data['filename']
+                    # Response headers only support latin-1 compatible strings.
+                    try:
+                        filename = data['filename'].encode('latin-1', 'strict')
+                    except UnicodeEncodeError:
+                        filename = str(int(time.time())) + ".csv"
                 else:
-                    import time
                     filename = str(int(time.time())) + ".csv"
 
                 r.headers["Content-Disposition"] = "attachment;filename={0}".format(filename)
diff --git a/web/pgadmin/utils/driver/psycopg2/__init__.py b/web/pgadmin/utils/driver/psycopg2/__init__.py
index e474817..ff74eb7 100644
--- a/web/pgadmin/utils/driver/psycopg2/__init__.py
+++ b/web/pgadmin/utils/driver/psycopg2/__init__.py
@@ -648,7 +648,10 @@ WHERE
                 res_io, fieldnames=header, delimiter=str(','), quoting=csv.QUOTE_NONNUMERIC
             )
             csv_writer.writeheader()
-            csv_writer.writerows(results)
+            # convert unicode records to utf-8 as csv writer do not support
+            # writing unicode data
+            csv_writer.writerows([{k: v.encode('utf-8') for k, v in row.items()}
+                                  for row in results])
 
             yield res_io.getvalue()
 
-- 
Sent via pgadmin-hackers mailing list (pgadmin-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgadmin-hackers

Reply via email to