Ticket #21832: 21832.diff

File 21832.diff, 5.7 KB (added by ANUBHAV JOSHI, 10 years ago)
  • django/contrib/auth/management/commands/createsuperuser.py

    diff --git a/django/contrib/auth/management/commands/createsuperuser.py b/django/contrib/auth/management/commands/createsuperuser.py
    index de0b467..b352ae8 100644
    a b class Command(BaseCommand):  
    8888                verbose_field_name = self.username_field.verbose_name
    8989                while username is None:
    9090                    if not username:
    91                         input_msg = capfirst(verbose_field_name)
     91                        input_msg = capfirst(verbose_field_name) + ' (%s.%s)' % (self.username_field.rel.to._meta.object_name, self.username_field.rel.field_name) if self.username_field.rel else ''
    9292                        if default_username:
    9393                            input_msg = "%s (leave blank to use '%s')" % (
    9494                                input_msg, default_username)
  • django/contrib/auth/tests/custom_user.py

    diff --git a/django/contrib/auth/tests/custom_user.py b/django/contrib/auth/tests/custom_user.py
    index cfa998a..77e8477 100644
    a b class CustomUserManager(BaseUserManager):  
    4040
    4141class CustomUserWithFKManager(BaseUserManager):
    4242    def create_superuser(self, username, email, group, password):
    43         user = self.model(username=username, email_id=email, group_id=group)
     43        user = self.model(username_id=username, email_id=email, group_id=group)
    4444        user.set_password(password)
    4545        user.save(using=self._db)
    4646        return user
    class CustomUser(AbstractBaseUser):  
    9696
    9797
    9898class CustomUserWithFK(AbstractBaseUser):
    99     username = models.CharField(max_length=30, unique=True)
    100     email = models.ForeignKey(Email, to_field='email')
     99    username = models.ForeignKey(Email, related_name='primary')
     100    email = models.ForeignKey(Email, to_field='email', related_name='secondary')
    101101    group = models.ForeignKey(Group)
    102102
    103103    custom_objects = CustomUserWithFKManager()
  • django/contrib/auth/tests/test_management.py

    diff --git a/django/contrib/auth/tests/test_management.py b/django/contrib/auth/tests/test_management.py
    index e774d3a..1fe8cee 100644
    a b class CreatesuperuserManagementCommandTestCase(TestCase):  
    350350        self.assertIs(command.stdin, sys.stdin)
    351351
    352352    @override_settings(AUTH_USER_MODEL='auth.CustomUserWithFK')
    353     def test_required_field_with_fk(self):
     353    def test_fields_with_fk(self):
    354354        new_io = six.StringIO()
    355355        group = Group.objects.create(name='mygroup')
    356356        email = Email.objects.create(email='mymail@gmail.com')
    357357        call_command(
    358358            'createsuperuser',
    359359            interactive=False,
    360             username='user',
    361             email='mymail@gmail.com',
     360            username=email.pk,
     361            email=email.email,
    362362            group=group.pk,
    363363            stdout=new_io,
    364364            skip_checks=True,
    class CreatesuperuserManagementCommandTestCase(TestCase):  
    366366        command_output = new_io.getvalue().strip()
    367367        self.assertEqual(command_output, 'Superuser created successfully.')
    368368        u = CustomUserWithFK._default_manager.get(email=email)
    369         self.assertEqual(u.username, "user")
     369        self.assertEqual(u.username, email)
    370370        self.assertEqual(u.group, group)
    371371
    372372        non_existent_email = 'mymail2@gmail.com'
    class CreatesuperuserManagementCommandTestCase(TestCase):  
    375375            call_command(
    376376                'createsuperuser',
    377377                interactive=False,
    378                 username='user',
     378                username=email.pk,
    379379                email=non_existent_email,
    380380                stdout=new_io,
    381381                skip_checks=True,
    382382            )
    383383
    384384    @override_settings(AUTH_USER_MODEL='auth.CustomUserWithFK')
    385     def test_required_fields_with_fk_interactive(self):
     385    def test_fields_with_fk_interactive(self):
    386386        new_io = six.StringIO()
    387387        group = Group.objects.create(name='mygroup')
    388388        email = Email.objects.create(email='mymail@gmail.com')
    389389
    390         @mock_inputs({'password': "nopasswd", 'username': "user", 'email': "mymail@gmail.com", 'group': group.pk})
     390        @mock_inputs({'password': "nopasswd", 'username': email.pk, 'email': email.email, 'group': group.pk})
    391391        def test(self):
    392392            call_command(
    393393                'createsuperuser',
    class CreatesuperuserManagementCommandTestCase(TestCase):  
    400400            command_output = new_io.getvalue().strip()
    401401            self.assertEqual(command_output, 'Superuser created successfully.')
    402402            u = CustomUserWithFK._default_manager.get(email=email)
    403             self.assertEqual(u.username, 'user')
     403            self.assertEqual(u.username, email)
    404404            self.assertEqual(u.group, group)
    405405
    406406        test(self)
    class CustomUserModelValidationTestCase(TestCase):  
    460460        ]
    461461        self.assertEqual(errors, expected)
    462462
     463    @override_settings(AUTH_USER_MODEL='auth.CustomUserWithFK')
     464    @override_system_checks([check_user_model])
     465    def test_username_with_fk_non_unique(self):
     466        "A non-unique USERNAME_FIELD should raise a model validation error."
     467
     468        errors = checks.run_checks()
     469        expected = [
     470            checks.Error(
     471                ("'CustomUserWithFK.username' must be "
     472                 "unique because it is named as the 'USERNAME_FIELD'."),
     473                hint=None,
     474                obj=CustomUserWithFK,
     475                id='auth.E003',
     476            ),
     477        ]
     478        self.assertEqual(errors, expected)
     479
    463480    @override_settings(AUTH_USER_MODEL='auth.CustomUserNonUniqueUsername',
    464481                       AUTHENTICATION_BACKENDS=[
    465482                           'my.custom.backend',
Back to Top