Fixed #20541 -- don't raise db signals twice when creating superuser
This commit is contained in:
parent
5005303ae7
commit
cab333cb16
|
@ -170,7 +170,8 @@ class BaseUserManager(models.Manager):
|
|||
|
||||
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 @@ class UserManager(BaseUserManager):
|
|||
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
|
||||
|
|
|
@ -5,6 +5,7 @@ from django.contrib.auth import get_user_model
|
|||
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 @@ class IsActiveTestCase(TestCase):
|
|||
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)
|
||||
|
|
Loading…
Reference in New Issue