Переглянути джерело

HUE-5015 [core] Install examples get "column username is not unique" if "hue" user exists

Jenny Kim 9 роки тому
батько
коміт
f08b421
1 змінених файлів з 20 додано та 13 видалено
  1. 20 13
      apps/useradmin/src/useradmin/models.py

+ 20 - 13
apps/useradmin/src/useradmin/models.py

@@ -300,25 +300,32 @@ def install_sample_user():
   Setup the de-activated sample user with a certain id. Do not create a user profile.
   """
   user = None
+
   try:
-    user = auth_models.User.objects.get(id=SAMPLE_USER_ID)
-    LOG.info('Sample user found: %s' % user.username)
+    if auth_models.User.objects.filter(id=SAMPLE_USER_ID).exists():
+      user = auth_models.User.objects.get(id=SAMPLE_USER_ID)
+      LOG.info('Sample user found with username "%s" and User ID: %s' % (user.username, user.id))
+    elif auth_models.User.objects.filter(username=SAMPLE_USER_INSTALL).exists():
+      user = auth_models.User.objects.get(username=SAMPLE_USER_INSTALL)
+      LOG.info('Sample user found: %s' % user.username)
+    else:
+      user, created = auth_models.User.objects.get_or_create(
+        username=SAMPLE_USER_INSTALL,
+        password='!',
+        is_active=False,
+        is_superuser=False,
+        id=SAMPLE_USER_ID,
+        pk=SAMPLE_USER_ID)
+
+      if created:
+        LOG.info('Installed a user called "%s"' % SAMPLE_USER_INSTALL)
+
     if user.username != SAMPLE_USER_INSTALL:
+      LOG.warn('Sample user does not have username "%s", will attempt to modify the username.' % SAMPLE_USER_INSTALL)
       with transaction.atomic():
         user = auth_models.User.objects.get(id=SAMPLE_USER_ID)
         user.username = SAMPLE_USER_INSTALL
         user.save()
-  except auth_models.User.DoesNotExist:
-    user, created = auth_models.User.objects.get_or_create(
-      username=SAMPLE_USER_INSTALL,
-      password='!',
-      is_active=False,
-      is_superuser=False,
-      id=SAMPLE_USER_ID,
-      pk=SAMPLE_USER_ID)
-
-    if created:
-      LOG.info('Installed a user called "%s"' % SAMPLE_USER_INSTALL)
   except Exception, ex:
     LOG.exception('Failed to get or create sample user')