Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #20541 -- don't raise db signals twice when creating superuser

  • Loading branch information...
commit cab333cb16656cbb7d2bcfb06b2f7aeab9bac7af 1 parent 5005303
@bak1an bak1an authored timgraham committed
View
21 django/contrib/auth/models.py
@@ -170,7 +170,8 @@ def get_by_natural_key(self, username):
class UserManager(BaseUserManager):
- def create_user(self, username, email=None, password=None, **extra_fields):
+ def _create_user(self, username, email, password,
+ is_staff, is_superuser, **extra_fields):
"""
Creates and saves a User with the given username, email and password.
"""
@@ -179,20 +180,20 @@ def create_user(self, username, email=None, password=None, **extra_fields):
raise ValueError('The given username must be set')
email = self.normalize_email(email)
user = self.model(username=username, email=email,
- is_staff=False, is_active=True, is_superuser=False,
- last_login=now, date_joined=now, **extra_fields)
-
+ is_staff=is_staff, is_active=True,
+ is_superuser=is_superuser, last_login=now,
+ date_joined=now, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
+ def create_user(self, username, email=None, password=None, **extra_fields):
+ return self._create_user(username, email, password, False, False,
+ **extra_fields)
+
def create_superuser(self, username, email, password, **extra_fields):
- u = self.create_user(username, email, password, **extra_fields)
- u.is_staff = True
- u.is_active = True
- u.is_superuser = True
- u.save(using=self._db)
- return u
+ return self._create_user(username, email, password, True, True,
+ **extra_fields)
@python_2_unicode_compatible
View
25 django/contrib/auth/tests/test_models.py
@@ -5,6 +5,7 @@
from django.contrib.auth.models import (Group, User, SiteProfileNotAvailable,
UserManager)
from django.contrib.auth.tests.utils import skipIfCustomUser
+from django.db.models.signals import post_save
from django.test import TestCase
from django.test.utils import override_settings
from django.utils import six
@@ -140,3 +141,27 @@ def test_is_active_field_default(self):
user_fetched = UserModel._default_manager.get(pk=user.pk)
# the attribute is always true for newly retrieved instance
self.assertEqual(user_fetched.is_active, True)
+
+
+@skipIfCustomUser
+class TestCreateSuperUserSignals(TestCase):
+ """
+ Simple test case for ticket #20541
+ """
+ def post_save_listener(self, *args, **kwargs):
+ self.signals_count += 1
+
+ def setUp(self):
+ self.signals_count = 0
+ post_save.connect(self.post_save_listener, sender=User)
+
+ def tearDown(self):
+ post_save.disconnect(self.post_save_listener, sender=User)
+
+ def test_create_user(self):
+ User.objects.create_user("JohnDoe")
+ self.assertEqual(self.signals_count, 1)
+
+ def test_create_superuser(self):
+ User.objects.create_superuser("JohnDoe", "mail@example.com", "1")
+ self.assertEqual(self.signals_count, 1)
Please sign in to comment.
Something went wrong with that request. Please try again.