瀏覽代碼

HUE-5211 [metadata] Support utf8 in search

Could not find a way without smart_str for non interactive API for
some unknown reason.
Romain Rigaux 9 年之前
父節點
當前提交
3031b2d

+ 1 - 1
desktop/core/src/desktop/static/desktop/js/apiHelper.js

@@ -1315,7 +1315,7 @@ var ApiHelper = (function () {
   ApiHelper.prototype.navSearchAutocomplete = function (options) {
     var self = this;
     $.post('/metadata/api/navigator/search_entities_interactive', {
-      query_s: options.query,
+      query_s: ko.mapping.toJSON(options.query),
       limit: 10,
       sources: '["' + options.source + '"]'
     }).done(function (data) {

+ 1 - 1
desktop/core/src/desktop/templates/assist.mako

@@ -1853,7 +1853,7 @@ from metadata.conf import has_navigator
           };
 
           $.post('/metadata/api/navigator/search_entities', {
-            query_s: self.searchInput(),
+            query_s: ko.mapping.toJSON(self.searchInput()),
             limit: 25,
             sources: ko.mapping.toJSON([self.visiblePanel().type])
           }, function (data) {

+ 1 - 1
desktop/core/src/desktop/templates/nav_components.mako

@@ -28,7 +28,7 @@ from django.utils.translation import ugettext as _
      <!-- /ko -->
      <div style="width: 100%" data-bind="ifnot: loading">
        <textarea style="width: 100%" data-bind="tagEditor: {
-          placeholder: '${_ko('No tags.')}',
+          placeholder: '${_ko('No tags')}',
           readOnly: '${ readOnly }' === 'True',
           setTags: currentTags,
           onSave: onSave,

+ 8 - 5
desktop/libs/metadata/src/metadata/navigator_api.py

@@ -1,4 +1,5 @@
 #!/usr/bin/env python
+# -- coding: utf-8 --
 # Licensed to Cloudera, Inc. under one
 # or more contributor license agreements.  See the NOTICE file
 # distributed with this work for additional information
@@ -30,7 +31,7 @@ from django.utils.translation import ugettext as _
 from django.views.decorators.http import require_POST
 
 from desktop.lib.django_util import JsonResponse
-from desktop.lib.i18n import force_unicode
+from desktop.lib.i18n import force_unicode, smart_str
 
 from metadata.conf import has_navigator
 from metadata.navigator_client import NavigatorApi
@@ -54,7 +55,7 @@ def error_handler(view_fn):
       raise e
     except Exception, e:
       status = 500
-      message = str(e)
+      message = force_unicode(e)
       LOG.exception(message)
 
       if 'Could not find' in message:
@@ -62,7 +63,7 @@ def error_handler(view_fn):
 
       response = {
         'status': -1,
-        'message': force_unicode(message)
+        'message': message
       }
     return JsonResponse(response, status=status)
   return decorator
@@ -75,7 +76,9 @@ def search_entities(request):
   """
   api = NavigatorApi()
 
-  query_s = request.POST.get('query_s', '')
+  query_s = json.loads(request.POST.get('query_s', ''))
+  query_s = smart_str(query_s)
+
   offset = request.POST.get('offset', 0)
   limit = request.POST.get('limit', 100)
   sources = json.loads(request.POST.get('sources')) or []
@@ -105,7 +108,7 @@ def search_entities_interactive(request):
   """
   api = NavigatorApi()
 
-  query_s = request.POST.get('query_s', '')
+  query_s = json.loads(request.POST.get('query_s', ''))
   prefix = request.POST.get('prefix')
   offset = request.POST.get('offset', 0)
   limit = request.POST.get('limit', 25)

+ 0 - 1
desktop/libs/metadata/src/metadata/navigator_client.py

@@ -84,7 +84,6 @@ class NavigatorApi(object):
     :param query_s: a query string of search terms (e.g. - sales quarterly);
       Currently the search will perform an OR boolean search for all terms (split on whitespace), against a whitelist
       of search_fields.
-      TODO: support smarter boolean searching with arbitrary ordering and precedence of conditionals
     """
     search_fields = ('originalName', 'originalDescription', 'name', 'description', 'tags')
 

+ 12 - 4
desktop/libs/metadata/src/metadata/navigator_tests.py

@@ -1,4 +1,5 @@
 #!/usr/bin/env python
+# -- coding: utf-8 --
 # Licensed to Cloudera, Inc. under one
 # or more contributor license agreements.  See the NOTICE file
 # distributed with this work for additional information
@@ -36,7 +37,7 @@ from metadata.navigator_client import NavigatorApi
 LOG = logging.getLogger(__name__)
 
 
-class TestNavigatorApi(object):
+class TestNavigator(object):
 
   @classmethod
   def setup_class(cls):
@@ -58,9 +59,16 @@ class TestNavigatorApi(object):
     cls.user.save()
 
 
-  def test_search_entities(self):
-    # TODO: write me
-    pass
+  def test_search_entities_view(self):
+    resp = self.client.post(reverse('metadata:search_entities'), {'query_s': json.dumps('châteaux'), 'limit': 25, 'sources': json.dumps(['sql'])})
+    json_resp = json.loads(resp.content)
+    assert_equal(0, json_resp['status'], json_resp)
+
+
+  def test_search_entities_interactive_view(self):
+    resp = self.client.post(reverse('metadata:search_entities_interactive'), {'query_s': json.dumps('châteaux'), 'limit': 10, 'sources': json.dumps(['sql'])})
+    json_resp = json.loads(resp.content)
+    assert_equal(0, json_resp['status'], json_resp)
 
 
   def test_find_entity(self):