diff options
| author | Russell Keith-Magee <russell@keith-magee.com> | 2009-12-14 12:39:20 +0000 |
|---|---|---|
| committer | Russell Keith-Magee <russell@keith-magee.com> | 2009-12-14 12:39:20 +0000 |
| commit | 35cc439228cd32dfa7a3ec919db01a8a5cd17d33 (patch) | |
| tree | ed9aff433487895c0e649994450fd0accb6362d2 /django/contrib | |
| parent | 44b9076bbed3e629230d9b77a8765e4c906036d1 (diff) | |
Fixed #7052 -- Added support for natural keys in serialization.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11863 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/contrib')
| -rw-r--r-- | django/contrib/auth/models.py | 12 | ||||
| -rw-r--r-- | django/contrib/contenttypes/models.py | 10 |
2 files changed, 22 insertions, 0 deletions
diff --git a/django/contrib/auth/models.py b/django/contrib/auth/models.py index 053761cb56..b20a2caf17 100644 --- a/django/contrib/auth/models.py +++ b/django/contrib/auth/models.py @@ -47,6 +47,13 @@ def check_password(raw_password, enc_password): class SiteProfileNotAvailable(Exception): pass +class PermissionManager(models.Manager): + def get_by_natural_key(self, codename, app_label, model): + return self.get( + codename=codename, + content_type=ContentType.objects.get_by_natural_key(app_label, model) + ) + class Permission(models.Model): """The permissions system provides a way to assign permissions to specific users and groups of users. @@ -63,6 +70,7 @@ class Permission(models.Model): name = models.CharField(_('name'), max_length=50) content_type = models.ForeignKey(ContentType) codename = models.CharField(_('codename'), max_length=100) + objects = PermissionManager() class Meta: verbose_name = _('permission') @@ -76,6 +84,10 @@ class Permission(models.Model): unicode(self.content_type), unicode(self.name)) + def natural_key(self): + return (self.codename,) + self.content_type.natural_key() + natural_key.dependencies = ['contenttypes.contenttype'] + class Group(models.Model): """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. diff --git a/django/contrib/contenttypes/models.py b/django/contrib/contenttypes/models.py index def7ce6986..69d0806385 100644 --- a/django/contrib/contenttypes/models.py +++ b/django/contrib/contenttypes/models.py @@ -8,6 +8,13 @@ class ContentTypeManager(models.Manager): # This cache is shared by all the get_for_* methods. _cache = {} + def get_by_natural_key(self, app_label, model): + try: + ct = self.__class__._cache[(app_label, model)] + except KeyError: + ct = self.get(app_label=app_label, model=model) + return ct + def get_for_model(self, model): """ Returns the ContentType object for a given model, creating the @@ -93,3 +100,6 @@ class ContentType(models.Model): so code that calls this method should catch it. """ return self.model_class()._default_manager.get(**kwargs) + + def natural_key(self): + return (self.app_label, self.model) |
