Ticket #13914: 13914.3.diff
File 13914.3.diff, 7.0 KB (added by , 13 years ago) |
---|
-
docs/topics/serialization.txt
215 215 the primary key of a given content type isn't easy to predict; it will 216 216 depend on how and when :djadmin:`syncdb` was executed. This is true for all 217 217 models which automatically generate objects, notably including 218 :class:`~django.contrib.auth.models.Permission` .218 :class:`~django.contrib.auth.models.Permission`, :class:`~django.contrib.auth.models.Group`, and :class:`~django.contrib.auth.models.User`. 219 219 220 220 .. warning:: 221 221 -
django/contrib/auth/tests/__init__.py
10 10 from django.contrib.auth.tests.remote_user import (RemoteUserTest, 11 11 RemoteUserNoCreateTest, RemoteUserCustomTest) 12 12 from django.contrib.auth.tests.management import GetDefaultUsernameTestCase 13 from django.contrib.auth.tests.models import ProfileTestCase 13 from django.contrib.auth.tests.models import (ProfileTestCase, NaturalKeysTestCase, 14 LoadDataWithoutNaturalKeysTestCase, LoadDataWithNaturalKeysTestCase) 14 15 from django.contrib.auth.tests.hashers import TestUtilsHashPass 15 16 from django.contrib.auth.tests.signals import SignalTestCase 16 17 from django.contrib.auth.tests.tokens import TokenGeneratorTest -
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 49 50 class LoadDataWithoutNaturalKeysTestCase(TestCase): 51 fixtures = ['regular.json'] 52 def test_user_is_created_and_added_to_group(self): 53 user = User.objects.get(username='my_username') 54 group = Group.objects.get(name='my_group') 55 self.assertEquals(group, user.groups.get()) 56 57 58 class LoadDataWithNaturalKeysTestCase(TestCase): 59 fixtures = ['natural.json'] 60 def test_user_is_created_and_added_to_group(self): 61 user = User.objects.get(username='my_username') 62 group = Group.objects.get(name='my_group') 63 self.assertEquals(group, user.groups.get()) 64 -
django/contrib/auth/fixtures/regular.json
1 [ 2 { 3 "pk": 1, 4 "model": "auth.group", 5 "fields": { 6 "name": "my_group", 7 "permissions": [] 8 } 9 }, 10 { 11 "pk": 1, 12 "model": "auth.user", 13 "fields": { 14 "username": "my_username", 15 "first_name": "", 16 "last_name": "", 17 "is_active": true, 18 "is_superuser": true, 19 "is_staff": true, 20 "last_login": "2012-01-13 00:14:00", 21 "groups": [ 22 1 23 ], 24 "user_permissions": [], 25 "password": "pbkdf2_sha256$10000$LUyhxJjuLwXF$f6Zbpnx1L5dPze8m0itBaHMDyZ/n6JyhuavQy2RrBIM=", 26 "email": "email@example.com", 27 "date_joined": "2012-01-13 00:14:00" 28 } 29 } 30 ] -
django/contrib/auth/fixtures/natural.json
1 [ 2 { 3 "pk": 1, 4 "model": "auth.group", 5 "fields": { 6 "name": "my_group", 7 "permissions": [] 8 } 9 }, 10 { 11 "pk": 1, 12 "model": "auth.user", 13 "fields": { 14 "username": "my_username", 15 "first_name": "", 16 "last_name": "", 17 "is_active": true, 18 "is_superuser": true, 19 "is_staff": true, 20 "last_login": "2012-01-13 00:14:00", 21 "groups": [ 22 [ 23 "my_group" 24 ] 25 ], 26 "user_permissions": [], 27 "password": "pbkdf2_sha256$10000$LUyhxJjuLwXF$f6Zbpnx1L5dPze8m0itBaHMDyZ/n6JyhuavQy2RrBIM=", 28 "email": "email@example.com", 29 "date_joined": "2012-01-13 00:14:00" 30 } 31 } 32 ] -
django/contrib/auth/models.py
86 86 natural_key.dependencies = ['contenttypes.contenttype'] 87 87 88 88 89 class GroupManager(models.Manager): 90 def get_by_natural_key(self, name): 91 return self.get(name=name) 92 89 93 class Group(models.Model): 90 94 """ 91 95 Groups are a generic way of categorizing users to apply permissions, or … … 104 108 messages. 105 109 """ 106 110 name = models.CharField(_('name'), max_length=80, unique=True) 111 objects = GroupManager() 107 112 permissions = models.ManyToManyField(Permission, 108 113 verbose_name=_('permissions'), blank=True) 109 114 … … 114 119 def __unicode__(self): 115 120 return self.name 116 121 122 def natural_key(self): 123 return (self.name,) 117 124 125 118 126 class UserManager(models.Manager): 119 127 def create_user(self, username, email=None, password=None): 120 128 """ … … 160 168 """ 161 169 return get_random_string(length, allowed_chars) 162 170 171 def get_by_natural_key(self, username): 172 return self.get(username=username) 163 173 174 164 175 # A few helper functions for common logic between User and AnonymousUser. 165 176 def _user_get_all_permissions(user, obj): 166 177 permissions = set() … … 240 251 def __unicode__(self): 241 252 return self.username 242 253 254 def natural_key(self): 255 return (self.username,) 256 243 257 def get_absolute_url(self): 244 258 return "/users/%s/" % urllib.quote(smart_str(self.username)) 245 259