Ticket #13914: 13914.2.diff

File 13914.2.diff, 4.4 KB (added by Flaviu Simihaian, 13 years ago)
  • django/contrib/auth/models.py

    diff --git a/django/contrib/auth/models.py b/django/contrib/auth/models.py
    index ec3af63..8d95de6 100644
    a b class Permission(models.Model):  
    9494        return (self.codename,) + self.content_type.natural_key()
    9595    natural_key.dependencies = ['contenttypes.contenttype']
    9696
     97class GroupManager(models.Manager):
     98    def get_by_natural_key(self, name):
     99        return self.get(name=name)
     100
    97101class Group(models.Model):
    98102    """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.
    99103
    class Group(models.Model):  
    103107    """
    104108    name = models.CharField(_('name'), max_length=80, unique=True)
    105109    permissions = models.ManyToManyField(Permission, verbose_name=_('permissions'), blank=True)
     110    objects = GroupManager()
    106111
    107112    class Meta:
    108113        verbose_name = _('group')
    class Group(models.Model):  
    111116    def __unicode__(self):
    112117        return self.name
    113118
     119    def natural_key(self):
     120        return (self.name,)
     121
    114122class UserManager(models.Manager):
    115123    def create_user(self, username, email, password=None):
    116124        """
    class UserManager(models.Manager):  
    150158        from random import choice
    151159        return ''.join([choice(allowed_chars) for i in range(length)])
    152160
     161    def get_by_natural_key(self, username):
     162        return self.get(username=username)
     163
    153164
    154165# A few helper functions for common logic between User and AnonymousUser.
    155166def _user_get_all_permissions(user, obj):
    class User(models.Model):  
    393404        return self._message_set
    394405    message_set = property(_get_message_set)
    395406
     407    def natural_key(self):
     408        return (self.username,)
     409
    396410class Message(models.Model):
    397411    """
    398412    The message system is a lightweight way to queue messages for given
  • django/contrib/auth/tests/__init__.py

    diff --git a/django/contrib/auth/tests/__init__.py b/django/contrib/auth/tests/__init__.py
    index 3a8f55b..9dda8e2 100644
    a b from django.contrib.auth.tests.forms import (UserCreationFormTest,  
    88    UserChangeFormTest, PasswordResetFormTest)
    99from django.contrib.auth.tests.remote_user import (RemoteUserTest,
    1010    RemoteUserNoCreateTest, RemoteUserCustomTest)
    11 from django.contrib.auth.tests.models import ProfileTestCase
     11from django.contrib.auth.tests.models import ProfileTestCase, NaturalKeysTestCase
    1212from django.contrib.auth.tests.signals import SignalTestCase
    1313from django.contrib.auth.tests.tokens import TokenGeneratorTest
    1414from django.contrib.auth.tests.views import (PasswordResetTest,
  • django/contrib/auth/tests/models.py

    diff --git a/django/contrib/auth/tests/models.py b/django/contrib/auth/tests/models.py
    index 754c6db..925ed7d 100644
    a b  
    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']
    class ProfileTestCase(TestCase):  
    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)
  • docs/topics/serialization.txt

    diff --git a/docs/topics/serialization.txt b/docs/topics/serialization.txt
    index c8acc85..f506f20 100644
    a b dependency, we add one extra line::  
    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