Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

magic-removal: Fixed #1280 -- Removed contenttypes.Package model and …

…changed ContentType field to have a 'model' field

git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2411 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit dac1a68645798fa48c165f350ee87f2e76a26e38 1 parent f0e185c
@adrianholovaty adrianholovaty authored
Showing with 24 additions and 37 deletions.
  1. +11 −11 django/contrib/auth/models.py
  2. +13 −26 django/contrib/contenttypes/models.py
View
22 django/contrib/auth/models.py
@@ -1,6 +1,6 @@
from django.core import validators
from django.db import backend, connection, models
-from django.contrib.contenttypes.models import Package
+from django.contrib.contenttypes.models import ContentType
from django.utils.translation import gettext_lazy as _
import datetime
@@ -11,16 +11,16 @@ class SiteProfileNotAvailable(Exception):
class Permission(models.Model):
name = models.CharField(_('name'), maxlength=50)
- package = models.ForeignKey(Package, db_column='package')
+ content_type = models.ForeignKey(ContentType)
codename = models.CharField(_('codename'), maxlength=100)
class Meta:
verbose_name = _('Permission')
verbose_name_plural = _('Permissions')
- unique_together = (('package', 'codename'),)
- ordering = ('package', 'codename')
+ unique_together = (('content_type', 'codename'),)
+ ordering = ('content_type', 'codename')
def __repr__(self):
- return "%s | %s" % (self.package_id, self.name)
+ return "%r | %s" % (self.content_type, self.name)
class Group(models.Model):
name = models.CharField(_('name'), maxlength=80, unique=True)
@@ -133,7 +133,7 @@ def get_group_permissions(self):
cursor = connection.cursor()
# The SQL below works out to the following, after DB quoting:
# cursor.execute("""
- # SELECT p.package, p.codename
+ # SELECT p.content_type_id, p.codename
# FROM auth_permission p, auth_group_permissions gp, auth_user_groups ug
# WHERE p.id = gp.permission_id
# AND gp.group_id = ug.group_id
@@ -144,7 +144,7 @@ def get_group_permissions(self):
WHERE p.%s = gp.%s
AND gp.%s = ug.%s
AND ug.%s = %%s""" % (
- backend.quote_name('package'), backend.quote_name('codename'),
+ backend.quote_name('content_type_id'), backend.quote_name('codename'),
backend.quote_name('auth_permission'), backend.quote_name('auth_group_permissions'),
backend.quote_name('auth_user_groups'), backend.quote_name('id'),
backend.quote_name('permission_id'), backend.quote_name('group_id'),
@@ -156,7 +156,7 @@ def get_group_permissions(self):
def get_all_permissions(self):
if not hasattr(self, '_perm_cache'):
import sets
- self._perm_cache = sets.Set(["%s.%s" % (p.package_id, p.codename) for p in self.user_permissions.all()])
+ self._perm_cache = sets.Set(["%s.%s" % (p.content_type, p.codename) for p in self.user_permissions.all()])
self._perm_cache.update(self.get_group_permissions())
return self._perm_cache
@@ -175,11 +175,11 @@ def has_perms(self, perm_list):
return False
return True
- def has_module_perms(self, package_name):
- "Returns True if the user has any permissions in the given package."
+ def has_module_perms(self, app_label):
+ "Returns True if the user has any permissions in the given app label."
if self.is_superuser:
return True
- return bool(len([p for p in self.get_all_permissions() if p[:p.index('.')] == package_name]))
+ return bool(len([p for p in self.get_all_permissions() if p[:p.index('.')] == app_label]))
def get_and_delete_messages(self):
messages = []
View
39 django/contrib/contenttypes/models.py
@@ -1,18 +1,6 @@
from django.db import models
from django.utils.translation import gettext_lazy as _
-class Package(models.Model):
- label = models.CharField(_('label'), maxlength=20, primary_key=True)
- name = models.CharField(_('name'), maxlength=30, unique=True)
- class Meta:
- verbose_name = _('package')
- verbose_name_plural = _('packages')
- db_table = 'django_package'
- ordering = ('name',)
-
- def __repr__(self):
- return self.name
-
class ContentTypeManager(models.Manager):
def get_for_model(self, model):
"""
@@ -21,35 +9,34 @@ def get_for_model(self, model):
"""
opts = model._meta
try:
- return self.model._default_manager.get(python_module_name__exact=opts.module_name,
- package__label__exact=opts.app_label)
+ return self.model._default_manager.get(app_label=opts.app_label,
+ model=opts.object_name.lower())
except self.model.DoesNotExist:
# The str() is needed around opts.verbose_name because it's a
# django.utils.functional.__proxy__ object.
ct = self.model(name=str(opts.verbose_name),
- package=Package.objects.get(label=opts.app_label),
- python_module_name=opts.module_name)
+ app_label=opts.app_label, model=opts.object_name.lower())
ct.save()
return ct
class ContentType(models.Model):
- name = models.CharField(_('name'), maxlength=100)
- package = models.ForeignKey(Package, db_column='package')
- python_module_name = models.CharField(_('python module name'), maxlength=50)
+ name = models.CharField(maxlength=100)
+ app_label = models.CharField(maxlength=100)
+ model = models.CharField(_('python model class name'), maxlength=100, unique=True)
objects = ContentTypeManager()
class Meta:
verbose_name = _('content type')
verbose_name_plural = _('content types')
db_table = 'django_content_type'
- ordering = ('package', 'name')
- unique_together = (('package', 'python_module_name'),)
+ ordering = ('name',)
def __repr__(self):
- return "%s | %s" % (self.package_id, self.name)
+ return self.name
- def get_model_module(self):
- "Returns the Python model module for accessing this type of content."
- return __import__('django.models.%s.%s' % (self.package_id, self.python_module_name), '', '', [''])
+ def model_class(self):
+ "Returns the Python model class for this type of content."
+ from django.db import models
+ return models.get_model(self.app_label, self.model)
def get_object_for_this_type(self, **kwargs):
"""
@@ -58,4 +45,4 @@ def get_object_for_this_type(self, **kwargs):
method. The ObjectNotExist exception, if thrown, will not be caught,
so code that calls this method should catch it.
"""
- return self.get_model_module().get_object(**kwargs)
+ return self.model_class()._default_manager.get(**kwargs)

0 comments on commit dac1a68

Please sign in to comment.
Something went wrong with that request. Please try again.