Subject: Fix for csv download issue [RM2253] [RM2214] [pgadmin4]

From: Harshal Dhumal <[email-masked]>
Date: Mon, 8 May 2017 14:30:55 +0530

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 ([email-masked])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgadmin-hackers

---

From: Murtuza Zabuawala <[email-masked]>
Date: Mon, 8 May 2017 14:51:01 +0530

Please hold on on this patch, I am also working on same type of issue.
I think my patch would cover RM#2253 & RM#2360

--
Regards,
Murtuza Zabuawala
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

On Mon, May 8, 2017 at 2:30 PM, Harshal Dhumal <
[email-masked]> wrote:

> 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*
>