Browse Source

[hive] Fix escaping of table samples

We now display as text in the template
Romain Rigaux 10 years ago
parent
commit
d6a14673a9

+ 2 - 1
apps/beeswax/src/beeswax/api.py

@@ -32,6 +32,7 @@ from desktop.lib.exceptions_renderable import PopupException
 from desktop.lib.i18n import force_unicode
 from desktop.lib.parameterization import substitute_variables
 from metastore import parser
+from notebook.models import escape_rows
 
 import beeswax.models
 
@@ -672,7 +673,7 @@ def get_sample_data(request, database, table):
     if sample_data:
       response['status'] = 0
       response['headers'] = sample_data.cols()
-      response['rows'] = list(sample_data.rows())
+      response['rows'] = escape_rows(sample_data.rows(), nulls_only=True)
     else:
       response['error_message'] = _('Sample data took too long to be generated')
   except Exception, ex:

+ 1 - 1
apps/beeswax/src/beeswax/tests.py

@@ -1477,7 +1477,7 @@ for x in sys.stdin:
     rows = json.loads(resp.content)['rows']
     flat_rows = sum(rows, [])
     assert_true("nada" in flat_rows, flat_rows)
-    assert_true("sp ace" in flat_rows, flat_rows)
+    assert_true("sp ace" in flat_rows, flat_rows)
 
     # Test table creation and data loading and removing header
     resp = self.client.post('/beeswax/create/import_wizard/%s' % self.db_name, {

+ 0 - 81
apps/metastore/src/metastore/templates/sample.mako

@@ -1,81 +0,0 @@
-## Licensed to Cloudera, Inc. under one
-## or more contributor license agreements.  See the NOTICE file
-## distributed with this work for additional information
-## regarding copyright ownership.  Cloudera, Inc. licenses this file
-## to you under the Apache License, Version 2.0 (the
-## "License"); you may not use this file except in compliance
-## with the License.  You may obtain a copy of the License at
-##
-##     http://www.apache.org/licenses/LICENSE-2.0
-##
-## Unless required by applicable law or agreed to in writing, software
-## distributed under the License is distributed on an "AS IS" BASIS,
-## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-## See the License for the specific language governing permissions and
-## limitations under the License.
-<%!
-from desktop.lib.i18n import smart_unicode
-from django.utils.translation import ugettext as _
-%>
-
-% if sample is not None:
-  % if error_message:
-    <div class="alert alert-error">
-      <h3>${ _('Error!') }</h3>
-      <pre>${ error_message | h }</pre>
-    </div>
-  % else:
-    % if app_name == 'impala' and table.has_complex:
-      <p class="alert">${ _('Impala does not support selecting complex types. These columns have been hidden from the sample results.') }</p>
-    % endif
-  <table class="table table-striped table-condensed sampleTable">
-    <thead>
-      <tr>
-        <th style="width: 10px"></th>
-        % for col in sample.cols():
-          <th>${ col }</th>
-        % endfor
-      </tr>
-    </thead>
-    <tbody>
-      % for i, row in enumerate(sample_rows):
-      <tr>
-        <td>${ i }</td>
-        % for item in row:
-        <td>
-          % if item is None:
-            NULL
-          % else:
-            ${ escape(smart_unicode(item, errors='ignore')).replace(' ', '&nbsp;') | n,unicode }
-          % endif
-        </td>
-        % endfor
-      </tr>
-      % endfor
-    </tbody>
-  </table>
-  % endif
-% endif
-
-<style type="text/css">
-  .sampleTable td, .sampleTable th {
-    white-space: nowrap;
-  }
-</style>
-
-% if len(sample.cols()) < 1000:
-<script type="text/javascript" charset="utf-8">
-  $(document).ready(function () {
-    $(".sampleTable").dataTable({
-      "bPaginate": false,
-      "bLengthChange": false,
-      "bInfo": false,
-      "bFilter": false,
-      "oLanguage": {
-        "sEmptyTable": "${_('No data available')}",
-        "sZeroRecords": "${_('No matching records')}",
-      }
-    });
-  });
-</script>
-% endif

+ 4 - 3
desktop/libs/notebook/src/notebook/models.py

@@ -25,7 +25,7 @@ from desktop.lib.i18n import smart_unicode
 
 
 # Materialize and HTML escape results
-def escape_rows(rows):
+def escape_rows(rows, nulls_only=False):
   data = []
 
   for row in rows:
@@ -39,8 +39,9 @@ def escape_rows(rows):
       elif field is None:
         escaped_field = 'NULL'
       else:
-        field = smart_unicode(field, errors='replace') # Prevent error when getting back non utf8 like charset=iso-8859-1
-        escaped_field = escape(field).replace(' ', '&nbsp;')
+        escaped_field = smart_unicode(field, errors='replace') # Prevent error when getting back non utf8 like charset=iso-8859-1
+        if not nulls_only:
+          escaped_field = escape(escaped_field).replace(' ', '&nbsp;')
       escaped_row.append(escaped_field)
     data.append(escaped_row)