Forráskód Böngészése

[editor] Refactoring for Auto install examples

Romain Rigaux 4 éve
szülő
commit
bd3b9dd9a2

+ 3 - 1
apps/beeswax/src/beeswax/management/commands/beeswax_install_examples.py

@@ -137,8 +137,10 @@ class Command(BaseCommand):
 
     if app_type == RDBMS:
       design_list = [d for d in design_list if dialect in d['dialects']]
-    if self.queries is None:  # Manual install
+
+    if not self.queries:  # Manual install
       design_list = [d for d in design_list if not d.get('auto_load_only')]
+
     if self.queries:  # Automated install
       design_list = [d for d in design_list if d['name'] in self.queries]
 

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

@@ -322,7 +322,7 @@ def install_sample_user(django_user=None):
 
   if ENABLE_ORGANIZATIONS.get():
     lookup = {'email': django_username}
-    django_username_short = django_user.username_short
+    django_username_short = django_user.username_short if django_user else 'hue'
   else:
     lookup = {'username': django_username}
     django_username_short = django_username

+ 11 - 7
apps/useradmin/src/useradmin/organization.py

@@ -53,14 +53,18 @@ def _fitered_queryset(queryset, by_owner=False):
 
 
 def get_organization(email, is_multi_user=False):
-  domain = email.split('@')[1] if is_multi_user else email
-
-  if domain:
-    organization, created = Organization.objects.get_or_create(name=domain, domain=domain, is_multi_user=is_multi_user)
-    LOG.info("Materializing organization %s in the database, is_multi_user=%s" % (domain, is_multi_user))
+  if email is None:
+    organization = Organization.objects.first()
+    LOG.warn('Returning first organization: %s' % organization)
   else:
-    LOG.warn('No organization domain found for email %s' % email)  # For Backends without emails or when organization enabled by default
-    organization = None
+    domain = email.split('@')[1] if is_multi_user else email
+
+    if domain:
+      organization, created = Organization.objects.get_or_create(name=domain, domain=domain, is_multi_user=is_multi_user)
+      LOG.info("Materializing organization %s in the database, is_multi_user=%s" % (domain, is_multi_user))
+    else:
+      LOG.warn('No organization domain found for email %s' % email)  # For Backends without emails or when organization enabled by default
+      organization = None
 
   return organization
 

+ 3 - 3
desktop/core/src/desktop/models.py

@@ -49,8 +49,8 @@ from useradmin.organization import _fitered_queryset
 
 from desktop import appmanager
 from desktop.auth.backend import is_admin
-from desktop.conf import get_clusters, CLUSTER_ID, IS_MULTICLUSTER_ONLY, IS_K8S_ONLY, ENABLE_ORGANIZATIONS, ENABLE_PROMETHEUS,\
-    has_connectors, TASK_SERVER, ENABLE_GIST, APP_BLACKLIST, ENABLE_SHARING
+from desktop.conf import get_clusters, IS_MULTICLUSTER_ONLY, ENABLE_ORGANIZATIONS, ENABLE_PROMETHEUS, \
+    has_connectors, TASK_SERVER, APP_BLACKLIST, ENABLE_SHARING, ENABLE_CONNECTORS
 from desktop.lib import fsmanager
 from desktop.lib.connectors.api import _get_installed_connectors
 from desktop.lib.connectors.models import Connector
@@ -100,7 +100,7 @@ def _version_from_properties(f):
 
 def get_sample_user_install(user):
   if ENABLE_ORGANIZATIONS.get():
-    organization = get_organization(email=user.email)
+    organization = get_organization(email=user.email if user else None)
     if organization.is_multi_user:
       return SAMPLE_USER_INSTALL + '@' + organization.domain
     else:

+ 1 - 1
desktop/libs/notebook/src/notebook/apps.py

@@ -32,5 +32,5 @@ class NotebookConfig(AppConfig):
     from notebook.models import install_custom_examples
 
     table_names = connection.introspection.table_names()
-    if 'auth_group' in table_names and 'auth_user' in table_names:
+    if 'auth_group' in table_names and ('auth_user' in table_names or 'useradmin_organizationuser' in table_names):
       install_custom_examples()

+ 1 - 1
desktop/libs/notebook/src/notebook/management/commands/samples_setup.py

@@ -65,7 +65,7 @@ class Command(BaseCommand):
 
     for dialect in dialects:
       EditorCommand().handle(
-        app_name=dialect['dialect'],
+        app_name=dialect['dialect'],  # Unused?
         user=user,
         tables=tables,
         dialect=dialect['dialect'],

+ 24 - 14
desktop/libs/notebook/src/notebook/models.py

@@ -35,8 +35,8 @@ from django.urls import reverse
 from django.utils.html import escape
 from django.utils.translation import ugettext as _
 
-
 from desktop.conf import has_connectors, TASK_SERVER
+from desktop.lib.connectors.models import _get_installed_connectors
 from desktop.lib.i18n import smart_unicode
 from desktop.lib.paths import SAFE_CHARACTERS_URI
 from desktop.models import Document2
@@ -713,18 +713,30 @@ def install_custom_examples():
   if EXAMPLES.AUTO_LOAD.get():
     from desktop.auth.backend import rewrite_user
     from beeswax.management.commands import beeswax_install_examples
-    from useradmin.models import get_default_user_group, install_sample_user, User
+    from useradmin.models import install_sample_user
 
     user = rewrite_user(
       install_sample_user()
     )
 
-    dialects = [
-      interpreter['dialect']
-      for interpreter in get_ordered_interpreters(user)
-      # Only for hive/impala currently, would also need to port to Notebook install examples.
-      if interpreter['dialect'] in ('hive', 'impala')
-    ]
+    if has_connectors():
+      interpreters = [
+        {
+          'type': connector['id'],
+          'dialect': connector['dialect']
+        }
+        for connector in _get_installed_connectors(category='editor')
+      ]
+    else:
+      interpreters = [
+        {
+          'type': interpreter['dialect'],
+          'dialect': interpreter['dialect']
+        }
+        for interpreter in get_ordered_interpreters(user)
+        # Only for hive/impala currently, would also need to port to Notebook install examples.
+        if interpreter['dialect'] in ('hive', 'impala')
+      ]
 
     queries = EXAMPLES.QUERIES.get()
     tables = EXAMPLES.TABLES.get()  # No-op. Only for the saved query samples, not the tables currently.
@@ -733,18 +745,16 @@ def install_custom_examples():
       'belonging to user %(user)s' % {
         'queries': queries,
         'tables': tables,
-        'dialects': dialects,
+        'dialects': [interpreter['dialect'] for interpreter in interpreters],
         'user': user
       }
     )
 
     result = []
 
-    for dialect in dialects:
-      interpreter = {'type': dialect, 'dialect': dialect}
-
+    for interpreter in interpreters:
       successes, errors = beeswax_install_examples.Command().handle(
-          dialect=dialect,
+          dialect=interpreter['dialect'],
           user=user,
           interpreter=interpreter,
           queries=queries,
@@ -752,7 +762,7 @@ def install_custom_examples():
           request=None
       )
       LOG.info('Dialect %(dialect)s installed samples: %(successes)s, %(errors)s,' % {
-        'dialect': dialect,
+        'dialect': interpreter['dialect'],
         'successes': successes,
         'errors': errors,
       })