You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by GitBox <gi...@apache.org> on 2018/09/05 00:04:30 UTC

[GitHub] tinylambda closed pull request #5806: When export to CSV on a Windows OS, use a independent encoding settin…

tinylambda closed pull request #5806: When export to CSV on a Windows OS, use a independent encoding settin…
URL: https://github.com/apache/incubator-superset/pull/5806
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/superset/config.py b/superset/config.py
index a5e4f2988c..4ce310f958 100644
--- a/superset/config.py
+++ b/superset/config.py
@@ -201,6 +201,7 @@
 CSV_EXPORT = {
     'encoding': 'utf-8',
 }
+CSV_WINDOWS_ENCODING = 'gbk'
 
 # ---------------------------------------------------
 # Time grain configurations
diff --git a/superset/views/core.py b/superset/views/core.py
index 97a7da97c5..6cf94680e3 100755
--- a/superset/views/core.py
+++ b/superset/views/core.py
@@ -1058,7 +1058,7 @@ def get_query_string_response(self, viz_obj):
             mimetype='application/json')
 
     def generate_json(self, datasource_type, datasource_id, form_data,
-                      csv=False, query=False, force=False):
+                      csv=False, query=False, force=False, is_windows=False):
         try:
             viz_obj = self.get_viz(
                 datasource_type=datasource_type,
@@ -1080,7 +1080,7 @@ def generate_json(self, datasource_type, datasource_id, form_data,
 
         if csv:
             return CsvResponse(
-                viz_obj.get_csv(),
+                viz_obj.get_csv(is_windows=is_windows),
                 status=200,
                 headers=generate_download_headers('csv'),
                 mimetype='application/csv')
@@ -1166,12 +1166,18 @@ def explore_json(self, datasource_type=None, datasource_id=None):
             return json_error_response(
                 utils.error_msg_from_exception(e),
                 stacktrace=traceback.format_exc())
+        is_windows = False
+        user_agent = request.headers.get('User-Agent', None)
+        if isinstance(user_agent, str):
+            user_agent = user_agent.lower()
+            is_windows = user_agent.find('windows') >= 0
         return self.generate_json(datasource_type=datasource_type,
                                   datasource_id=datasource_id,
                                   form_data=form_data,
                                   csv=csv,
                                   query=query,
-                                  force=force)
+                                  force=force,
+                                  is_windows=is_windows)
 
     @log_this
     @has_access
diff --git a/superset/viz.py b/superset/viz.py
index 6a18dfab17..21ebc5672d 100644
--- a/superset/viz.py
+++ b/superset/viz.py
@@ -463,10 +463,13 @@ def data(self):
         }
         return content
 
-    def get_csv(self):
+    def get_csv(self, is_windows=False):
         df = self.get_df()
         include_index = not isinstance(df.index, pd.RangeIndex)
-        return df.to_csv(index=include_index, **config.get('CSV_EXPORT'))
+        to_csv_kwargs = copy.copy(config.get('CSV_EXPORT'))
+        if is_windows:
+            to_csv_kwargs['encoding'] = config.get('CSV_WINDOWS_ENCODING')
+        return df.to_csv(index=include_index, **to_csv_kwargs)
 
     def get_data(self, df):
         return self.get_df().to_dict(orient='records')


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org