Quellcode durchsuchen

[doc2] Install beeswax and impala examples as Doc2 queries

Installs the queries into a shared examples directory
Jenny Kim vor 9 Jahren
Ursprung
Commit
72b47a7

+ 47 - 15
apps/beeswax/src/beeswax/management/commands/beeswax_install_examples.py

@@ -24,14 +24,12 @@ from django.core.management.base import BaseCommand
 from django.contrib.auth.models import User
 from django.utils.translation import ugettext as _
 
-from hadoop import cluster
-
 from desktop.lib.exceptions_renderable import PopupException
-from useradmin.models import install_sample_user
-from desktop.models import Document
+from desktop.models import Directory, Document, Document2, Document2Permission, import_saved_beeswax_query
+from hadoop import cluster
+from useradmin.models import get_default_user_group, install_sample_user
 
 import beeswax.conf
-
 from beeswax.models import SavedQuery, HQL, IMPALA
 from beeswax.design import hql_query
 from beeswax.server import dbms
@@ -107,7 +105,7 @@ class Command(BaseCommand):
     design_list = filter(lambda d: int(d['type']) == app_type, design_list)
 
     for design_dict in design_list:
-      design = SampleDesign(design_dict)
+      design = SampleQuery(design_dict)
       try:
         design.install(django_user)
       except Exception, ex:
@@ -280,7 +278,8 @@ class SampleTable(object):
       raise InstallException(msg)
 
 
-class SampleDesign(object):
+class SampleQuery(object):
+
   """Represents a query loaded from the designs.json file"""
   def __init__(self, data_dict):
     self.name = data_dict['name']
@@ -288,6 +287,7 @@ class SampleDesign(object):
     self.type = int(data_dict['type'])
     self.data = data_dict['data']
 
+
   def install(self, django_user):
     """
     Install queries. Raise InstallException on failure.
@@ -295,13 +295,45 @@ class SampleDesign(object):
     LOG.info('Installing sample query: %s' % (self.name,))
     try:
       # Don't overwrite
-      model = SavedQuery.objects.get(owner=django_user, name=self.name, type=self.type)
-    except SavedQuery.DoesNotExist:
-      model = SavedQuery(owner=django_user, name=self.name)
-      model.type = self.type
+      doc2 = Document2.objects.get(owner=django_user, name=self.name, type=self._document_type(self.type))
+    except Document2.DoesNotExist:
+      query = SavedQuery(owner=django_user, name=self.name, type=self.type, desc=self.desc)
       # The data field needs to be a string. The sample file writes it
       # as json (without encoding into a string) for readability.
-      model.data = json.dumps(self.data)
-      model.desc = self.desc
-      model.save()
-      LOG.info('Successfully installed sample design: %s' % (self.name,))
+      query.data = json.dumps(self.data)
+
+      # Create document from saved query
+      notebook = import_saved_beeswax_query(query)
+      data = notebook.get_json()
+
+      # Get or create sample user directories
+      home_dir = Directory.objects.get_home_directory(django_user)
+      examples_dir, created = Directory.objects.get_or_create(
+        parent_directory=home_dir,
+        owner=django_user,
+        name=Document2.EXAMPLES_DIR
+      )
+
+      doc2 = Document2.objects.create(
+        owner=django_user,
+        parent_directory=examples_dir,
+        name=self.name,
+        type=self._document_type(self.type),
+        description=self.desc,
+        data=data
+      )
+
+      # Share with default group
+      examples_dir.share(django_user, Document2Permission.READ_PERM, groups=[get_default_user_group()])
+      doc2.save()
+
+      LOG.info('Successfully installed sample query: %s' % (self.name,))
+
+
+  def _document_type(self, type):
+    if type == HQL:
+      return 'query-hive'
+    elif type == IMPALA:
+      return 'query-impala'
+    else:
+      return None

+ 14 - 6
apps/beeswax/src/beeswax/tests.py

@@ -1215,12 +1215,20 @@ for x in sys.stdin:
     assert_true(data['rows'], data)
     resp = self.client.get(reverse('beeswax:get_sample_data', kwargs={'database': 'default', 'table': 'customers'}))
 
-    # New designs exists
-    resp = self.client.get('/beeswax/list_designs')
-    assert_true('Sample: Job loss' in resp.content, resp.content)
-    assert_true('Sample: Salary growth' in resp.content)
-    assert_true('Sample: Top salary' in resp.content)
-    assert_true('Sample: Customers' in resp.content)
+    # New queries exist
+    resp = self.client.get('/desktop/api2/docs/shared')
+    data = json.loads(resp.content)
+    doc_names = [doc['name'] for doc in data['documents']]
+    assert_true('examples' in doc_names, data)
+    uuid = next((doc['uuid'] for doc in data['documents'] if doc['name'] == 'examples'), None)
+
+    resp = self.client.get('/desktop/api2/docs/', {'uuid': uuid})
+    data = json.loads(resp.content)
+    doc_names = [doc['name'] for doc in data['children']]
+    assert_true('Sample: Job loss' in doc_names, data)
+    assert_true('Sample: Salary growth' in doc_names, data)
+    assert_true('Sample: Top salary' in doc_names, data)
+    assert_true('Sample: Customers' in doc_names, data)
 
     # Now install it a second time, and no error
     resp = self.client.post('/beeswax/install_examples')

+ 8 - 6
desktop/core/src/desktop/api2.py

@@ -120,7 +120,7 @@ def get_shared_documents(request):
   """
 
   response = {
-    'documents': [],
+    'documents': []
   }
 
   documents = Document2.objects.get_shared_documents(request.user, flatten=False)
@@ -132,8 +132,10 @@ def get_shared_documents(request):
   response.update(_paginate(request, queryset=response['documents']))
 
   # Serialize results
-  if response['documents']:
+  if response['documents'] and response['documents'].count() > 0:
     response['documents'] = [doc.to_dict() for doc in response['documents']]
+  else:
+    response['documents'] = []
 
   return JsonResponse(response)
 
@@ -363,10 +365,10 @@ def _filter_documents(request, queryset):
   search_text = request.GET.get('text', None)
 
   documents = Document2.objects.refine_documents(
-    documents=queryset,
-    types=type_filters,
-    search_text=search_text,
-    order_by=sort)
+      documents=queryset,
+      types=type_filters,
+      search_text=search_text,
+      order_by=sort)
 
   count = documents.count()
 

+ 1 - 0
desktop/core/src/desktop/models.py

@@ -880,6 +880,7 @@ class Document2Manager(models.Manager):
 class Document2(models.Model):
 
   TRASH_DIR = '.Trash'
+  EXAMPLES_DIR = 'examples'
 
   owner = models.ForeignKey(auth_models.User, db_index=True, verbose_name=_t('Owner'), help_text=_t('Creator.'), related_name='doc2_owner')
   name = models.CharField(default='', max_length=255)