Ticket #13914: 13914.3.diff

File 13914.3.diff, 7.0 KB (added by Juarez Bochi, 13 years ago)
  • docs/topics/serialization.txt

     
    215215the primary key of a given content type isn't easy to predict; it will
    216216depend on how and when :djadmin:`syncdb` was executed. This is true for all
    217217models 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`.
    219219
    220220.. warning::
    221221
  • 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, NaturalKeysTestCase,
     14    LoadDataWithoutNaturalKeysTestCase, LoadDataWithNaturalKeysTestCase)
    1415from django.contrib.auth.tests.hashers import TestUtilsHashPass
    1516from django.contrib.auth.tests.signals import SignalTestCase
    1617from django.contrib.auth.tests.tokens import TokenGeneratorTest
  • 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
     49
     50class 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
     58class 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

     
    8686    natural_key.dependencies = ['contenttypes.contenttype']
    8787
    8888
     89class GroupManager(models.Manager):
     90    def get_by_natural_key(self, name):
     91        return self.get(name=name)
     92
    8993class Group(models.Model):
    9094    """
    9195    Groups are a generic way of categorizing users to apply permissions, or
     
    104108    messages.
    105109    """
    106110    name = models.CharField(_('name'), max_length=80, unique=True)
     111    objects = GroupManager()
    107112    permissions = models.ManyToManyField(Permission,
    108113        verbose_name=_('permissions'), blank=True)
    109114
     
    114119    def __unicode__(self):
    115120        return self.name
    116121
     122    def natural_key(self):
     123        return (self.name,)
    117124
     125
    118126class UserManager(models.Manager):
    119127    def create_user(self, username, email=None, password=None):
    120128        """
     
    160168        """
    161169        return get_random_string(length, allowed_chars)
    162170
     171    def get_by_natural_key(self, username):
     172        return self.get(username=username)
    163173
     174
    164175# A few helper functions for common logic between User and AnonymousUser.
    165176def _user_get_all_permissions(user, obj):
    166177    permissions = set()
     
    240251    def __unicode__(self):
    241252        return self.username
    242253
     254    def natural_key(self):
     255        return (self.username,)
     256
    243257    def get_absolute_url(self):
    244258        return "/users/%s/" % urllib.quote(smart_str(self.username))
    245259
Back to Top