Browse Source

[desktop] Use User.objects.get_or_create to make sample user

Erick Tryzelaar 10 năm trước cách đây
mục cha
commit
3d6a38b
1 tập tin đã thay đổi với 13 bổ sung13 xóa
  1. 13 13
      apps/useradmin/src/useradmin/models.py

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

@@ -296,18 +296,18 @@ def install_sample_user():
   Setup the de-activated sample user with a certain id. Do not create a user profile.
   """
 
-  try:
-    user = auth_models.User.objects.get(username=SAMPLE_USERNAME)
-  except auth_models.User.DoesNotExist:
-    try:
-      user = auth_models.User.objects.create(username=SAMPLE_USERNAME, password='!', is_active=False, is_superuser=False, id=1100713, pk=1100713)
-      LOG.info('Installed a user called "%s"' % (SAMPLE_USERNAME,))
-    except Exception, e:
-      LOG.info('Sample user race condition: %s' % e)
-      user = auth_models.User.objects.get(username=SAMPLE_USERNAME)
-      LOG.info('Sample user race condition, got: %s' % user)
-
-  fs = cluster.get_hdfs()
-  fs.do_as_user(SAMPLE_USERNAME, fs.create_home_dir)
+  user, created = auth_models.User.objects.get_or_create(
+      username=SAMPLE_USERNAME,
+      password='!',
+      is_active=False,
+      is_superuser=False,
+      id=1100713,
+      pk=1100713)
+
+  if created:
+    LOG.info('Installed a user called "%s"' % (SAMPLE_USERNAME,))
+
+    fs = cluster.get_hdfs()
+    fs.do_as_user(SAMPLE_USERNAME, fs.create_home_dir)
 
   return user