Bläddra i källkod

HUE-624. [jobbrowser] Non-ascii character in job name causes error

* File chooser needs WebHdfsStat to be json serializable
bc Wong 13 år sedan
förälder
incheckning
c70950d5fb

+ 3 - 5
apps/filebrowser/src/filebrowser/views.py

@@ -339,11 +339,9 @@ def listdir(request, path, chooser):
 def chooser(request, path):
     """
     Returns the html to JFrame that will display a file prompt.
-    """
-    """return view(request, path)"""
-
-    """Dispatches viewing of a path to either index() or fileview(), depending on type."""
 
+    Dispatches viewing of a path to either index() or fileview(), depending on type.
+    """
     # default_to_home is set in bootstrap.js
     home_dir_path = request.user.get_home_directory()
     if request.GET.get('default_to_home') and request.fs.isdir(home_dir_path):
@@ -367,7 +365,7 @@ def _massage_stats(request, stats):
     return {
         'path': normalized,
         'name': posixpath.basename(path),
-        'stats': stats,
+        'stats': stats.to_json_dict(),
         'type': filetype(stats['mode']),
         'rwx': rwx(stats['mode']),
         'url': make_absolute(request, "view", dict(path=urlquote(normalized))),

+ 19 - 0
apps/filebrowser/src/filebrowser/views_test.py

@@ -24,6 +24,12 @@ from hadoop import pseudo_hdfs4
 from avro import schema, datafile, io
 from desktop.lib.django_test_util import make_logged_in_client
 from nose.tools import assert_true, assert_false, assert_equal
+
+try:
+  import json
+except ImportError:
+  import simplejson as json
+
 import logging
 
 LOG = logging.getLogger(__name__)
@@ -105,6 +111,19 @@ def test_listdir():
     except:
       pass      # Don't let cleanup errors mask earlier failures
 
+@attr('requires_hadoop')
+def test_chooser():
+  cluster = pseudo_hdfs4.shared_cluster()
+  c = make_logged_in_client()
+
+  # Note that the trailing slash is important. We ask for the root dir.
+  resp = c.get('/filebrowser/chooser/?format=json')
+  # We should get a json response
+  dic = json.loads(resp.content)
+  assert_equal('/', dic['current_dir_path'])
+  assert_equal('/', dic['path'])
+
+
 @attr('requires_hadoop')
 def test_view_avro():
   cluster = pseudo_hdfs4.shared_cluster()

+ 4 - 3
apps/jobbrowser/src/jobbrowser/models.py

@@ -16,6 +16,7 @@
 # limitations under the License.
 
 from desktop.lib.view_util import format_time_diff
+from desktop.lib import i18n
 from hadoop import job_tracker
 from hadoop import confparse
 from urlparse import urlparse, urlunparse
@@ -145,9 +146,9 @@ class Job(JobLinkage):
     return self._full_job_conf
 
   def _init_attributes(self):
-    self.queueName = self.job.profile.queueName
-    self.jobName = self.job.profile.name
-    self.user = self.job.profile.user
+    self.queueName = i18n.smart_unicode(self.job.profile.queueName)
+    self.jobName = i18n.smart_unicode(self.job.profile.name)
+    self.user = i18n.smart_unicode(self.job.profile.user)
     self.mapProgress = self.job.status.mapProgress
     self.reduceProgress = self.job.status.reduceProgress
     self.setupProgress = self.job.status.setupProgress

+ 25 - 0
desktop/core/src/desktop/lib/i18n.py

@@ -25,6 +25,7 @@ import os
 import re
 
 import desktop.conf
+import django.utils.encoding
 
 SITE_ENCODING = None
 REPLACEMENT_CHAR = u'\ufffd'
@@ -53,6 +54,30 @@ def validate_encoding(encoding):
   except LookupError:
     return False
 
+def smart_unicode(s, strings_only=False, errors='strict'):
+  """
+  Wrapper around Django's version, while supplying our configured encoding.
+  Decode char array to unicode.
+  """
+  return django.utils.encoding.smart_unicode(
+        s, get_site_encoding(), strings_only, errors)
+
+def force_unicode(s, strings_only=False, errors='strict'):
+  """
+  Wrapper around Django's version, while supplying our configured encoding.
+  Decode char array to unicode.
+  """
+  return django.utils.encoding.force_unicode(
+        s, get_site_encoding(), strings_only, errors)
+
+def smart_str(s, strings_only=False, errors='strict'):
+  """
+  Wrapper around Django's version, while supplying our configured encoding.
+  Encode unicode into char array.
+  """
+  return django.utils.encoding.smart_str(
+        s, get_site_encoding(), strings_only, errors)
+
 
 _CACHED_ENV = None
 

+ 9 - 0
desktop/libs/hadoop/src/hadoop/fs/webhdfs_types.py

@@ -65,6 +65,15 @@ class WebHdfsStat(object):
   def __setitem__(self, key, value):
     setattr(self, key, value)
 
+  def to_json_dict(self):
+    """Returns a dictionary for easy serialization"""
+    KEYS = ('path', 'size', 'atime', 'mtime', 'mode', 'user', 'group',
+            'blockSize', 'replication')
+    res = { }
+    for k in KEYS:
+      res[k] = getattr(self, k)
+    return res
+
 
 class WebHdfsContentSummary(object):
   """