Browse Source

HUE-3287 [core] Django 1.11 upgrade
- [core] Move util location_to_url to desktop lib utils

Romain Rigaux 7 years ago
parent
commit
9ea1b8357d

+ 1 - 1
apps/beeswax/src/beeswax/server/dbms.py

@@ -28,7 +28,7 @@ from desktop.lib.django_util import format_preserving_redirect
 from desktop.lib.exceptions_renderable import PopupException
 from desktop.lib.parameterization import substitute_variables
 from desktop.models import Cluster, ANALYTIC_DB
-from filebrowser.views import location_to_url
+from desktop.lib.view_util import location_to_url
 from indexer.file_format import HiveFormat
 
 from beeswax import hive_site

+ 1 - 1
apps/filebrowser/src/filebrowser/views_test.py

@@ -38,7 +38,7 @@ from desktop.lib.test_utils import grant_access, add_to_group, add_permission, r
 from hadoop import pseudo_hdfs4
 from hadoop.conf import UPLOAD_CHUNK_SIZE
 from filebrowser.conf import ENABLE_EXTRACT_UPLOADED_ARCHIVE
-from filebrowser.views import location_to_url
+from desktop.lib.view_util import location_to_url
 
 from conf import MAX_SNAPPY_DECOMPRESSION_SIZE
 from lib.rwx import expand_mode

+ 1 - 1
apps/jobbrowser/src/jobbrowser/models.py

@@ -27,7 +27,7 @@ from desktop.lib import i18n
 from django.utils.html import escape
 from django.utils.translation import ugettext as _
 
-from filebrowser.views import location_to_url
+from desktop.lib.view_util import location_to_url
 
 from jobbrowser.conf import DISABLE_KILLING_JOBS
 

+ 1 - 1
apps/jobbrowser/src/jobbrowser/templates/job.mako

@@ -20,7 +20,7 @@
   import os
   from hadoop.fs.exceptions import WebHdfsException
   from jobbrowser.views import format_counter_name
-  from filebrowser.views import location_to_url
+  from desktop.lib.view_util import location_to_url
   from desktop.views import commonheader, commonfooter
   from django.template.defaultfilters import urlencode
   from django.utils.translation import ugettext as _

+ 1 - 1
apps/metastore/src/metastore/views.py

@@ -35,7 +35,7 @@ from beeswax.design import hql_query
 from beeswax.models import SavedQuery
 from beeswax.server import dbms
 from beeswax.server.dbms import get_query_server_config
-from filebrowser.views import location_to_url
+from desktop.lib.view_util import location_to_url
 from urlparse import urlparse
 from metadata.conf import has_optimizer, has_navigator, get_optimizer_url, get_navigator_url
 from notebook.connectors.base import Notebook, QueryError

+ 29 - 0
desktop/core/src/desktop/lib/view_util.py

@@ -19,6 +19,11 @@
 import math
 import datetime
 
+from django.urls import reverse
+
+from hadoop.fs.hadoopfs import Hdfs
+
+
 def big_filesizeformat(bytes):
   if bytes is None or bytes is "":
     return "N/A"
@@ -78,3 +83,27 @@ def format_duration_in_millis(duration=0):
         output.append("%dm" % minutes)
     output.append("%ds" % seconds)
     return ":".join(output)
+
+def location_to_url(location, strict=True, is_embeddable=False):
+    """
+    If possible, returns a file browser URL to the location.
+    Prunes HDFS URI to path.
+    Location is a URI, if strict is True.
+
+    Python doesn't seem to have a readily-available URI-comparison
+    library, so this is quite hacky.
+    """
+    if location is None:
+      return None
+    split_path = Hdfs.urlsplit(location)
+    if strict and not split_path[1] or not split_path[2]:
+      # No netloc not full url or no URL
+      return None
+    path = location
+    if split_path[0] == 'hdfs':
+      path = split_path[2]
+
+    filebrowser_path = reverse("filebrowser.views.view", kwargs=dict(path=path))
+    if is_embeddable and not filebrowser_path.startswith('/hue'):
+        filebrowser_path = '/hue' + filebrowser_path
+    return filebrowser_path