Ticket #16611: 16611.2.diff

File 16611.2.diff, 4.3 KB (added by Aymeric Augustin, 13 years ago)
  • docs/topics/auth.txt

     
    308308            * ``o``, ``O``, and ``0`` (uppercase letter o, lowercase letter o,
    309309              and zero)
    310310
     311    .. method:: models.UserManager.active()
     312
     313        Returns ``QuerySet`` containing only active users (i.e. those, which have :attr:`~models.User.is_active` set to ``True``).
     314
     315    .. method:: models.UserManager.inactive()
     316
     317        Returns ``QuerySet`` containing only inactive users (i.e. those, which have :attr:`~models.User.is_active` set to ``False``).
     318
    311319Basic usage
    312320-----------
    313321
  • django/contrib/auth/tests/__init__.py

     
    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, UserManagerTestCase
    1414from django.contrib.auth.tests.signals import SignalTestCase
    1515from django.contrib.auth.tests.tokens import TokenGeneratorTest
    1616from django.contrib.auth.tests.views import (PasswordResetTest,
  • django/contrib/auth/tests/models.py

     
    3333        # module that doesn't exist
    3434        settings.AUTH_PROFILE_MODULE = 'foo.bar'
    3535        self.assertRaises(SiteProfileNotAvailable, user.get_profile)
     36
     37
     38class UserManagerTestCase(TestCase):
     39    def setUp(self):
     40        User.objects.create_user('active1', 'active1@example.com', 'active1')
     41        User.objects.create_user('active2', 'active2@example.com', 'active2')
     42
     43        User.objects.create_user('inactive1', 'inactive1@example.com', 'inactive1')
     44        User.objects.create_user('inactive2', 'inactive2@example.com', 'inactive2')
     45        User.objects.create_user('inactive3', 'inactive3@example.com', 'inactive3')
     46
     47        User.objects.filter(username__startswith='inactive').update(is_active=False)
     48
     49    def test_active_inactive_methods(self):
     50        active_usernames = list(User.objects.active().order_by('username').values_list('username', flat=True))
     51        self.assertEqual([u'active1', u'active2'], active_usernames)
     52
     53        inactive_usernames = list(User.objects.inactive().order_by('username').values_list('username', flat=True))
     54        self.assertEqual([u'inactive1', u'inactive2', u'inactive3'], inactive_usernames)
  • django/contrib/auth/models.py

     
    128128        # that look like it -- just to avoid confusion.
    129129        return get_random_string(length, allowed_chars)
    130130
     131    def active(self):
     132        return self.filter(is_active=True)
    131133
     134    def inactive(self):
     135        return self.filter(is_active=False)
     136
     137
    132138# A few helper functions for common logic between User and AnonymousUser.
    133139def _user_get_all_permissions(user, obj):
    134140    permissions = set()
  • django/contrib/auth/forms.py

     
    116116        Validates that an active user exists with the given email address.
    117117        """
    118118        email = self.cleaned_data["email"]
    119         self.users_cache = User.objects.filter(
    120                                 email__iexact=email,
    121                                 is_active=True)
     119        self.users_cache = User.objects.active().filter(email__iexact=email)
    122120        if not len(self.users_cache):
    123121            raise forms.ValidationError(_("That e-mail address doesn't have an associated user account. Are you sure you've registered?"))
    124122        if any((user.password == UNUSABLE_PASSWORD) for user in self.users_cache):
Back to Top