Browse Source

[impala] Saved queries page should only list Impala queries

Romain Rigaux 13 years ago
parent
commit
2a3bd89095
2 changed files with 49 additions and 7 deletions
  1. 13 5
      apps/beeswax/src/beeswax/views.py
  2. 36 2
      apps/impala/src/impala/tests.py

+ 13 - 5
apps/beeswax/src/beeswax/views.py

@@ -182,12 +182,20 @@ def list_designs(request):
   specified in ``filterargs``).
   specified in ``filterargs``).
   """
   """
   DEFAULT_PAGE_SIZE = 10
   DEFAULT_PAGE_SIZE = 10
+  app_name= get_app_name(request)
 
 
   if conf.SHARE_SAVED_QUERIES.get() or request.user.is_superuser:
   if conf.SHARE_SAVED_QUERIES.get() or request.user.is_superuser:
     user = None
     user = None
   else:
   else:
     user = request.user
     user = request.user
-  page, filter_params = _list_designs(request.GET, DEFAULT_PAGE_SIZE, user=user)
+
+  # Extract the saved query list.
+  prefix = 'q-'
+  querydict_query = _copy_prefix(prefix, request.GET)
+  # Manually limit up the user filter.
+  querydict_query[ prefix + 'user' ] = user
+  querydict_query[ prefix + 'type' ] = app_name
+  page, filter_params = _list_designs(querydict_query, DEFAULT_PAGE_SIZE, prefix)
 
 
   return render('list_designs.mako', request, {
   return render('list_designs.mako', request, {
     'page': page,
     'page': page,
@@ -949,7 +957,7 @@ def query_done_cb(request, server_id):
     if design:
     if design:
       subject += ": %s" % (design.name,)
       subject += ": %s" % (design.name,)
 
 
-    link = "%s/#launch=Beeswax:%s" % \
+    link = "%s%s" % \
               (get_desktop_uri_prefix(),
               (get_desktop_uri_prefix(),
                reverse(get_app_name(request) + ':watch_query', kwargs={'id': query_history.id}))
                reverse(get_app_name(request) + ':watch_query', kwargs={'id': query_history.id}))
     body = _("%(subject)s. You may see the results here: %(link)s\n\nQuery:\n%(query)s") % {
     body = _("%(subject)s. You may see the results here: %(link)s\n\nQuery:\n%(query)s") % {
@@ -1171,7 +1179,7 @@ def execute_directly(request, query, query_server=None, design=None, tablename=N
   return format_preserving_redirect(request, watch_url, get_dict)
   return format_preserving_redirect(request, watch_url, get_dict)
 
 
 
 
-def _list_designs(querydict, page_size, prefix="", user=None):
+def _list_designs(querydict, page_size, prefix=""):
   """
   """
   _list_designs(querydict, page_size, prefix, user) -> (page, filter_param)
   _list_designs(querydict, page_size, prefix, user) -> (page, filter_param)
 
 
@@ -1191,8 +1199,8 @@ def _list_designs(querydict, page_size, prefix="", user=None):
 
 
   # Filtering. Only display designs explicitly saved.
   # Filtering. Only display designs explicitly saved.
   db_queryset = models.SavedQuery.objects.filter(is_auto=False)
   db_queryset = models.SavedQuery.objects.filter(is_auto=False)
-  if user is None:
-    user = querydict.get(prefix + 'user')
+
+  user = querydict.get(prefix + 'user')
   if user is not None:
   if user is not None:
     db_queryset = db_queryset.filter(owner__username=user)
     db_queryset = db_queryset.filter(owner__username=user)
 
 

+ 36 - 2
apps/impala/src/impala/tests.py

@@ -18,9 +18,11 @@
 import re
 import re
 
 
 from nose.tools import assert_true, assert_equal, assert_false
 from nose.tools import assert_true, assert_equal, assert_false
+from django.contrib.auth.models import User
 
 
-from beeswax.server import dbms
 from desktop.lib.django_test_util import make_logged_in_client
 from desktop.lib.django_test_util import make_logged_in_client
+from beeswax.models import SavedQuery
+from beeswax.server import dbms
 
 
 
 
 class MockDbms:
 class MockDbms:
@@ -37,7 +39,7 @@ class TestImpala:
       # Mock DB calls as we don't need the real ones
       # Mock DB calls as we don't need the real ones
       self.prev_dbms = dbms.get
       self.prev_dbms = dbms.get
       dbms.get = lambda a, b: MockDbms()
       dbms.get = lambda a, b: MockDbms()
-  
+
       response = self.client.get("/impala/")
       response = self.client.get("/impala/")
       assert_true(re.search('<li id="impalaIcon"\W+class="active', response.content), response.content)
       assert_true(re.search('<li id="impalaIcon"\W+class="active', response.content), response.content)
       assert_true('Query Editor' in response.content)
       assert_true('Query Editor' in response.content)
@@ -46,3 +48,35 @@ class TestImpala:
       assert_true('Query Editor' in response.content)
       assert_true('Query Editor' in response.content)
     finally:
     finally:
       dbms.get = self.prev_dbms
       dbms.get = self.prev_dbms
+
+  def test_saved_queries(self):
+    user = User.objects.get(username='test')
+
+    response = self.client.get("/impala/list_designs")
+    assert_equal(len(response.context['page'].object_list), 0)
+
+    try:
+      beewax_query = create_saved_query('beeswax', user)
+      response = self.client.get("/impala/list_designs")
+      assert_equal(len(response.context['page'].object_list), 0)
+
+      impala_query = create_saved_query('impala', user)
+      response = self.client.get("/impala/list_designs")
+      assert_equal(len(response.context['page'].object_list), 1)
+    finally:
+      if beewax_query is not None:
+        beewax_query.delete()
+      if impala_query is not None:
+        impala_query.delete()
+
+
+# Can be refactored with SavedQuery.create_empty() in Hue 2.3
+def create_saved_query(app_name, owner):
+    query_type = SavedQuery.TYPES_MAPPING[app_name]
+    design = SavedQuery(owner=owner, type=query_type)
+    design.name = SavedQuery.DEFAULT_NEW_DESIGN_NAME
+    design.desc = ''
+    design.data = ''
+    design.is_auto = False
+    design.save()
+    return design