Ticket #13914: 13914.diff
File 13914.diff, 4.4 KB (added by , 14 years ago) |
---|
-
django/contrib/auth/tests/__init__.py
4 4 from django.contrib.auth.tests.forms import UserCreationFormTest, AuthenticationFormTest, SetPasswordFormTest, PasswordChangeFormTest, UserChangeFormTest, PasswordResetFormTest 5 5 from django.contrib.auth.tests.remote_user \ 6 6 import RemoteUserTest, RemoteUserNoCreateTest, RemoteUserCustomTest 7 from django.contrib.auth.tests.models import ProfileTestCase 7 from django.contrib.auth.tests.models import ProfileTestCase, NaturalKeysTestCase 8 8 from django.contrib.auth.tests.tokens import TokenGeneratorTest 9 9 from django.contrib.auth.tests.views \ 10 10 import PasswordResetTest, ChangePasswordTest, LoginTest, LogoutTest -
django/contrib/auth/tests/models.py
1 1 from django.conf import settings 2 2 from django.test import TestCase 3 from django.contrib.auth.models import User, SiteProfileNotAvailable3 from django.contrib.auth.models import Group, User, SiteProfileNotAvailable 4 4 5 5 class ProfileTestCase(TestCase): 6 6 fixtures = ['authtestdata.json'] … … 33 33 # module that doesn't exist 34 34 settings.AUTH_PROFILE_MODULE = 'foo.bar' 35 35 self.assertRaises(SiteProfileNotAvailable, user.get_profile) 36 37 38 class 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
84 84 return (self.codename,) + self.content_type.natural_key() 85 85 natural_key.dependencies = ['contenttypes.contenttype'] 86 86 87 class GroupManager(models.Manager): 88 def get_by_natural_key(self, name): 89 return self.get(name=name) 90 87 91 class Group(models.Model): 88 92 """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. 89 93 … … 93 97 """ 94 98 name = models.CharField(_('name'), max_length=80, unique=True) 95 99 permissions = models.ManyToManyField(Permission, verbose_name=_('permissions'), blank=True) 100 objects = GroupManager() 96 101 97 102 class Meta: 98 103 verbose_name = _('group') … … 101 106 def __unicode__(self): 102 107 return self.name 103 108 109 def natural_key(self): 110 return (self.name,) 111 104 112 class UserManager(models.Manager): 105 113 def create_user(self, username, email, password=None): 106 114 """ … … 140 148 from random import choice 141 149 return ''.join([choice(allowed_chars) for i in range(length)]) 142 150 151 def get_by_natural_key(self, username): 152 return self.get(username=username) 143 153 154 144 155 # A few helper functions for common logic between User and AnonymousUser. 145 156 def _user_get_all_permissions(user, obj): 146 157 permissions = set() … … 384 395 return self._message_set 385 396 message_set = property(_get_message_set) 386 397 398 def natural_key(self): 399 return (self.username,) 400 387 401 class Message(models.Model): 388 402 """ 389 403 The message system is a lightweight way to queue messages for given -
docs/topics/serialization.txt
400 400 This definition ensures that ``ContentType`` models are serialized before 401 401 ``Permission`` models. In turn, any object referencing ``Permission`` will 402 402 be 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