Ticket #17046: validate_user_save.diff

File validate_user_save.diff, 2.8 KB (added by nerdap, 13 years ago)

Does validation in the User.save() method, and raises a validation error if the username is empty.

  • django/contrib/auth/models.py

    diff --git a/django/contrib/auth/models.py b/django/contrib/auth/models.py
    index 8064a4e..12debbe 100644
    a b from django.contrib.auth.hashers import (  
    1515    check_password, make_password, is_password_usable, UNUSABLE_PASSWORD)
    1616from django.contrib.auth.signals import user_logged_in
    1717from django.contrib.contenttypes.models import ContentType
     18from django.core.exceptions import ValidationError
    1819
    1920
    2021def update_last_login(sender, user, **kwargs):
    class User(models.Model):  
    284285    def has_usable_password(self):
    285286        return is_password_usable(self.password)
    286287
     288    # The save method is overriden to raise an error when the username is blank.
     289    # See ticket #17046
     290    def save(self, *args, **kwargs):
     291        if self.username == '':
     292            raise ValidationError(_('username cannot be an empty string.'))
     293        super(User, self).save(*args, **kwargs)
     294
    287295    def get_group_permissions(self, obj=None):
    288296        """
    289297        Returns a list of permission strings that this user has through his/her
  • django/contrib/auth/tests/__init__.py

    diff --git a/django/contrib/auth/tests/__init__.py b/django/contrib/auth/tests/__init__.py
    index 883e4c9..0eacfe8 100644
    a b from django.contrib.auth.tests.forms import (UserCreationFormTest,  
    1010from django.contrib.auth.tests.remote_user import (RemoteUserTest,
    1111    RemoteUserNoCreateTest, RemoteUserCustomTest)
    1212from django.contrib.auth.tests.management import GetDefaultUsernameTestCase
    13 from django.contrib.auth.tests.models import ProfileTestCase
     13from django.contrib.auth.tests.models import ProfileTestCase, UserEmptyUsernameCase
    1414from django.contrib.auth.tests.hashers import TestUtilsHashPass
    1515from django.contrib.auth.tests.signals import SignalTestCase
    1616from django.contrib.auth.tests.tokens import TokenGeneratorTest
  • django/contrib/auth/tests/models.py

    diff --git a/django/contrib/auth/tests/models.py b/django/contrib/auth/tests/models.py
    index 754c6db..2fec66e 100644
    a b  
    11from django.conf import settings
    22from django.test import TestCase
    33from django.contrib.auth.models import User, SiteProfileNotAvailable
     4from django.core.exceptions import ValidationError
    45
    56class ProfileTestCase(TestCase):
    67    fixtures = ['authtestdata.json']
    class ProfileTestCase(TestCase):  
    3334        # module that doesn't exist
    3435        settings.AUTH_PROFILE_MODULE = 'foo.bar'
    3536        self.assertRaises(SiteProfileNotAvailable, user.get_profile)
     37
     38
     39class UserEmptyUsernameCase(TestCase):
     40   
     41    def test_empty_username(self):
     42        with self.assertRaises(ValidationError):
     43            User.objects.create_user(username='', password='password', email='e@e.com')
Back to Top