Browse Source

HUE-950 [core] Examples integration

Romain Rigaux 12 years ago
parent
commit
d963fc5243

+ 8 - 18
apps/beeswax/src/beeswax/management/commands/beeswax_install_examples.py

@@ -17,36 +17,25 @@
 
 import logging
 import os
-import pwd
 import simplejson
 
 from django.core.management.base import NoArgsCommand
 from django.contrib.auth.models import User
 from django.utils.translation import ugettext as _
 
+from desktop.models import Document
+
 import beeswax.conf
-from beeswax.models import SavedQuery
-from beeswax.server.dbms import get_query_server_config, QueryServerException
 
-from beeswax import models
+from beeswax.models import SavedQuery, IMPALA
 from beeswax.design import hql_query
 from beeswax.server import dbms
+from beeswax.server.dbms import get_query_server_config, QueryServerException
 
 
 LOG = logging.getLogger(__name__)
-DEFAULT_INSTALL_USER = 'hue'
-
 
-def get_install_user():
-  """Use the DEFAULT_INSTALL_USER if it exists, else try the current user"""
-  try:
-    pwd.getpwnam(DEFAULT_INSTALL_USER)
-    return DEFAULT_INSTALL_USER
-  except KeyError:
-    pw_struct = pwd.getpwuid(os.geteuid())
-    LOG.info("Default sample installation user '%s' does not exist. Using '%s'"
-             % (DEFAULT_INSTALL_USER, pw_struct.pw_name))
-    return pw_struct.pw_name
+DEFAULT_INSTALL_USER = 'sample'
 
 
 class InstallException(Exception):
@@ -71,7 +60,7 @@ class Command(NoArgsCommand):
     """
     Setup the sample user
     """
-    USERNAME = 'sample'
+    USERNAME = DEFAULT_INSTALL_USER
     try:
       user = User.objects.get(username=USERNAME)
     except User.DoesNotExist:
@@ -100,7 +89,7 @@ class Command(NoArgsCommand):
 
     for design_dict in design_list:
       if app_name == 'impala':
-        design_dict['type'] = models.IMPALA
+        design_dict['type'] = IMPALA
       design = SampleDesign(design_dict)
       try:
         design.install(django_user)
@@ -130,6 +119,7 @@ class SampleTable(object):
   def install(self, django_user):
     self.create(django_user)
     self.load(django_user)
+    Document.objects.sync()
 
   def create(self, django_user):
     """

+ 2 - 0
apps/oozie/src/oozie/management/commands/oozie_setup.py

@@ -29,6 +29,7 @@ from hadoop import cluster
 from oozie.conf import LOCAL_SAMPLE_DATA_DIR, LOCAL_SAMPLE_DIR,\
   REMOTE_SAMPLE_DIR
 from liboozie.submittion import create_directories
+from desktop.models import Document
 
 
 LOG = logging.getLogger(__name__)
@@ -62,3 +63,4 @@ class Command(NoArgsCommand):
     except User.DoesNotExist:
       sample_user = User.objects.create(username=USERNAME, password='!', is_active=False, is_superuser=False, id=1100713, pk=1100713)
     management.call_command('loaddata', 'initial_oozie_examples.json', verbosity=2)
+    Document.objects.sync()

+ 2 - 0
apps/pig/src/pig/management/commands/pig_setup.py

@@ -29,6 +29,7 @@ from hadoop import cluster
 from pig.conf import LOCAL_SAMPLE_DIR, REMOTE_SAMPLE_DIR
 from liboozie.submittion import create_directories
 from desktop.lib import paths
+from desktop.models import Document
 
 LOG = logging.getLogger(__name__)
 
@@ -61,3 +62,4 @@ class Command(NoArgsCommand):
     except User.DoesNotExist:
       sample_user = User.objects.create(username=USERNAME, password='!', is_active=False, is_superuser=False, id=1100713, pk=1100713)
     management.call_command('loaddata', 'initial_pig_examples.json', verbosity=2)
+    Document.objects.sync()

+ 25 - 11
desktop/core/src/desktop/models.py

@@ -130,7 +130,7 @@ class DocumentTag(models.Model):
   DEFAULT = 'default' # Always there
   TRASH = 'trash' # There when the document is trashed
   HISTORY = 'history' # There when the document is a submission history
-  
+
   RESERVED = (DEFAULT, TRASH, HISTORY)
 
   objects = DocumentTagManager()
@@ -148,7 +148,7 @@ class DocumentManager(models.Manager):
 
   def get_docs(self, user, model_class=None, extra=None):
     docs = Document.objects.documents(user).exclude(name='pig-app-hue-script')
-    
+
     if model_class is not None:
       ct = ContentType.objects.get_for_model(model_class)
       docs = docs.filter(content_type=ct)
@@ -175,7 +175,7 @@ class DocumentManager(models.Manager):
     trash = DocumentTag.objects.get_trash_tag(user=user)
     history = DocumentTag.objects.get_history_tag(user=user)
 
-    return Document.objects.get_docs(user, model_class).exclude(tags__in=[trash, history]).order_by('-last_modified')  
+    return Document.objects.get_docs(user, model_class).exclude(tags__in=[trash, history]).order_by('-last_modified')
 
   def available(self, model_class, user):
     docs = self.available_docs(model_class, user)
@@ -213,7 +213,7 @@ class DocumentManager(models.Manager):
                 description=description,
                 extra=extra
             )
-  
+
       tag = DocumentTag.objects.get_default_tag(user=owner)
       doc.tags.add(tag)
       return doc
@@ -233,47 +233,58 @@ class DocumentManager(models.Manager):
 
         if not job.doc.exists():
           doc = Document.objects.link(job, owner=job.owner, name=job.name, description=job.description)
-          tag = DocumentTag.objects.get_default_tag(owner=job.owner)
+          tag = DocumentTag.objects.get_default_tag(user=job.owner)
           doc.tags.add(tag)
           if job.is_trashed:
             doc.send_to_trash()
           if job.is_shared:
-            DocumentPermission.objects.share_to_default(doc)
+            doc.share_to_default()
           if hasattr(job, 'managed'):
             if not job.managed:
               doc.extra = 'jobsub'
-              doc.save()              
+              doc.save()
+        if job.owner.username == 'sample':
+          job.doc.get().share_to_default()
     except Exception, e:
+      print e
       LOG.warn(force_unicode(e))
 
     try:
+      from beeswax.models import SavedQuery
+
       for job in SavedQuery.objects.all():
-        LOG.warn('Deleting duplicate document %s for %s' % (job.doc.all(), job))
         if job.doc.count() > 1:
+          LOG.warn('Deleting duplicate document %s for %s' % (job.doc.all(), job))
           job.doc.all().delete()
 
         if not job.doc.exists():
           doc = Document.objects.link(job, owner=job.owner, name=job.name, description=job.desc, extra=job.type)
-          tag = DocumentTag.objects.get_default_tag(owner=job.owner)
+          tag = DocumentTag.objects.get_default_tag(user=job.owner)
           doc.tags.add(tag)
           if job.is_trashed:
             doc.send_to_trash()
+        if job.owner.username == 'sample':
+          job.doc.get().share_to_default()
     except Exception, e:
+      print e
       LOG.warn(force_unicode(e))
 
     try:
       from pig.models import PigScript
 
       for job in PigScript.objects.all():
-        LOG.warn('Deleting duplicate document %s for %s' % (job.doc.all(), job))
         if job.doc.count() > 1:
+          LOG.warn('Deleting duplicate document %s for %s' % (job.doc.all(), job))
           job.doc.all().delete()
 
         if not job.doc.exists():
           doc = Document.objects.link(job, owner=job.owner, name=job.dict['name'], description='')
-          tag = DocumentTag.objects.get_default_tag(owner=job.owner)
+          tag = DocumentTag.objects.get_default_tag(user=job.owner)
           doc.tags.add(tag)
+        if job.owner.username == 'sample':
+          job.doc.get().share_to_default()
     except Exception, e:
+      print e
       LOG.warn(force_unicode(e))
 
 
@@ -327,6 +338,9 @@ class Document(models.Model):
     tag = DocumentTag.objects.get_history_tag(user=self.owner)
     self.tags.add(tag)
 
+  def share_to_default(self):
+    DocumentPermission.objects.share_to_default(self)
+
   def is_accessible(self, user):
     return user.is_superuser or self.owner == user or Document.objects.get_doc(self.id, user)