Browse Source

HUE-8758 [connectors] Avoid 500 on page opening when not in dev mode

Desktop libs do not automatically have an app name.
If not we get a:
  Exception: no app!
Romain 5 years ago
parent
commit
6231742eb0
2 changed files with 18 additions and 12 deletions
  1. 9 4
      desktop/core/src/desktop/auth/backend.py
  2. 9 8
      desktop/core/src/desktop/lib/apputil.py

+ 9 - 4
desktop/core/src/desktop/auth/backend.py

@@ -75,6 +75,7 @@ def load_augmentation_class():
     LOG.exception('failed to augment class')
     raise ImproperlyConfigured("Could not find user_augmentation_class: %s" % (class_name,))
 
+
 _user_augmentation_class = None
 def get_user_augmentation_class():
   global _user_augmentation_class
@@ -83,11 +84,11 @@ def get_user_augmentation_class():
     _user_augmentation_class = load_augmentation_class()
   return _user_augmentation_class
 
+
 def rewrite_user(user):
   """
   Rewrites the user according to the augmentation class.
-  We currently only re-write specific attributes,
-  though this could be generalized.
+  We currently only re-write specific attributes, though this could be generalized.
   """
   if user is None:
     LOG.warn('Failed to rewrite user, user is None.')
@@ -97,6 +98,7 @@ def rewrite_user(user):
       setattr(user, attr, getattr(augment, attr))
   return user
 
+
 def is_admin(user):
   is_admin = False
   if hasattr(user, 'is_superuser'):
@@ -105,10 +107,11 @@ def is_admin(user):
     try:
       user = rewrite_user(user)
       is_admin = user.has_hue_permission(action="superuser", app="useradmin")
-    except Exception as e:
-      LOG.exception("Could not validate if %s is a superuser assuming False." % user)
+    except Exception:
+      LOG.exception("Could not validate if %s is a superuser, assuming False." % user)
   return is_admin
 
+
 class DefaultUserAugmentor(object):
   def __init__(self, parent):
     self._parent = parent
@@ -125,6 +128,7 @@ class DefaultUserAugmentor(object):
   def has_hue_permission(self, action, app):
     return self._get_profile().has_hue_permission(action=action, app=app)
 
+
 def find_user(username):
   lookup = {'email': username} if ENABLE_ORGANIZATIONS.get() else {'username': username}
 
@@ -135,6 +139,7 @@ def find_user(username):
     user = None
   return user
 
+
 def create_user(username, password, is_superuser=True):
   if ENABLE_ORGANIZATIONS.get():
     organization = get_organization(email=username)

+ 9 - 8
desktop/core/src/desktop/lib/apputil.py

@@ -15,8 +15,8 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-
 import inspect
+
 from django.conf import settings
 
 
@@ -28,8 +28,7 @@ INFO_LEVEL_CALL_DURATION_MS = 1000
 
 def get_current_app(frame=None):
   """
-  Return the name of the app from INSTALLED_APPS that is most recently
-  present on the call stack.
+  Return the name of the app from INSTALLED_APPS that is most recently present on the call stack.
   """
   if frame == None:
     frame = inspect.currentframe().f_back
@@ -37,22 +36,24 @@ def get_current_app(frame=None):
   while frame:
     module = inspect.getmodule(frame.f_code)
     if not module:
-      raise Exception(("No module for code %s (frame %s). Perhaps you have an old " +
-                       ".pyc file hanging around?") % (repr(frame.f_code), repr(frame)))
+      raise Exception((
+          "No module for code %s (frame %s). Perhaps you have an old .pyc file hanging around?") %
+          (repr(frame.f_code), repr(frame))
+      )
     app = get_app_for_module(module)
     if app:
       return app
     frame = frame.f_back
 
-  # did not find any app
+  # Did not find any app
   return None
 
 def get_app_for_module(module):
   for app in settings.INSTALLED_APPS:
-    # TODO(philip): This is quite hacky.  If desktop becomes a more
-    # full application, we'll want to separate this out more cleanly.
     if module.__name__.startswith('desktop.lib.metrics.views'):
       return app
+    if module.__name__.startswith('desktop.lib.connectors.views'):
+      return app
     if module.__name__.startswith(app) and not module.__name__.startswith("desktop.lib"):
       return app
   return None