Ticket #13914: 13914.diff

File 13914.diff, 4.4 KB (added by Juarez Bochi, 14 years ago)
  • django/contrib/auth/tests/__init__.py

     
    44from django.contrib.auth.tests.forms import UserCreationFormTest, AuthenticationFormTest, SetPasswordFormTest, PasswordChangeFormTest, UserChangeFormTest, PasswordResetFormTest
    55from django.contrib.auth.tests.remote_user \
    66        import RemoteUserTest, RemoteUserNoCreateTest, RemoteUserCustomTest
    7 from django.contrib.auth.tests.models import ProfileTestCase
     7from django.contrib.auth.tests.models import ProfileTestCase, NaturalKeysTestCase
    88from django.contrib.auth.tests.tokens import TokenGeneratorTest
    99from django.contrib.auth.tests.views \
    1010        import PasswordResetTest, ChangePasswordTest, LoginTest, LogoutTest
  • django/contrib/auth/tests/models.py

     
    11from django.conf import settings
    22from django.test import TestCase
    3 from django.contrib.auth.models import User, SiteProfileNotAvailable
     3from django.contrib.auth.models import Group, User, SiteProfileNotAvailable
    44
    55class ProfileTestCase(TestCase):
    66    fixtures = ['authtestdata.json']
     
    3333        # module that doesn't exist
    3434        settings.AUTH_PROFILE_MODULE = 'foo.bar'
    3535        self.assertRaises(SiteProfileNotAvailable, user.get_profile)
     36
     37
     38class NaturalKeysTestCase(TestCase):
     39    fixtures = ['authtestdata.json']
     40    def test_user_natural_key(self):
     41        staff_user = User.objects.get(username='staff')
     42        self.assertEquals(User.objects.get_by_natural_key('staff'), staff_user)
     43        self.assertEquals(staff_user.natural_key(), ('staff',))
     44
     45    def test_group_natural_key(self):
     46        users_group = Group.objects.create(name='users')
     47        self.assertEquals(Group.objects.get_by_natural_key('users'), users_group)
     48        self.assertEquals(users_group.natural_key(), ('users',))
  • django/contrib/auth/models.py

     
    8484        return (self.codename,) + self.content_type.natural_key()
    8585    natural_key.dependencies = ['contenttypes.contenttype']
    8686
     87class GroupManager(models.Manager):
     88    def get_by_natural_key(self, name):
     89        return self.get(name=name)
     90
    8791class Group(models.Model):
    8892    """Groups are a generic way of categorizing users to apply permissions, or some other label, to those users. A user can belong to any number of groups.
    8993
     
    9397    """
    9498    name = models.CharField(_('name'), max_length=80, unique=True)
    9599    permissions = models.ManyToManyField(Permission, verbose_name=_('permissions'), blank=True)
     100    objects = GroupManager()
    96101
    97102    class Meta:
    98103        verbose_name = _('group')
     
    101106    def __unicode__(self):
    102107        return self.name
    103108
     109    def natural_key(self):
     110        return (self.name,)
     111
    104112class UserManager(models.Manager):
    105113    def create_user(self, username, email, password=None):
    106114        """
     
    140148        from random import choice
    141149        return ''.join([choice(allowed_chars) for i in range(length)])
    142150
     151    def get_by_natural_key(self, username):
     152        return self.get(username=username)
    143153
     154
    144155# A few helper functions for common logic between User and AnonymousUser.
    145156def _user_get_all_permissions(user, obj):
    146157    permissions = set()
     
    384395        return self._message_set
    385396    message_set = property(_get_message_set)
    386397
     398    def natural_key(self):
     399        return (self.username,)
     400
    387401class Message(models.Model):
    388402    """
    389403    The message system is a lightweight way to queue messages for given
  • docs/topics/serialization.txt

     
    400400This definition ensures that ``ContentType`` models are serialized before
    401401``Permission`` models. In turn, any object referencing ``Permission`` will
    402402be serialized after both ``ContentType`` and ``Permission``.
     403
     404.. note::
     405
     406    Besides the ``Permission`` model, both ``User`` and ``Group`` models
     407    in ``contrib.auth`` support natural keys.
     408
Back to Top