Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Adrian Holovaty authored February 27, 2006
22  django/contrib/auth/models.py
... ...
@@ -1,6 +1,6 @@
1 1
 from django.core import validators
2 2
 from django.db import backend, connection, models
3  
-from django.contrib.contenttypes.models import Package
  3
+from django.contrib.contenttypes.models import ContentType
4 4
 from django.utils.translation import gettext_lazy as _
5 5
 import datetime
6 6
 
@@ -11,16 +11,16 @@ class SiteProfileNotAvailable(Exception):
11 11
 
12 12
 class Permission(models.Model):
13 13
     name = models.CharField(_('name'), maxlength=50)
14  
-    package = models.ForeignKey(Package, db_column='package')
  14
+    content_type = models.ForeignKey(ContentType)
15 15
     codename = models.CharField(_('codename'), maxlength=100)
16 16
     class Meta:
17 17
         verbose_name = _('Permission')
18 18
         verbose_name_plural = _('Permissions')
19  
-        unique_together = (('package', 'codename'),)
20  
-        ordering = ('package', 'codename')
  19
+        unique_together = (('content_type', 'codename'),)
  20
+        ordering = ('content_type', 'codename')
21 21
 
22 22
     def __repr__(self):
23  
-        return "%s | %s" % (self.package_id, self.name)
  23
+        return "%r | %s" % (self.content_type, self.name)
24 24
 
25 25
 class Group(models.Model):
26 26
     name = models.CharField(_('name'), maxlength=80, unique=True)
@@ -133,7 +133,7 @@ def get_group_permissions(self):
133 133
             cursor = connection.cursor()
134 134
             # The SQL below works out to the following, after DB quoting:
135 135
             # cursor.execute("""
136  
-            #     SELECT p.package, p.codename
  136
+            #     SELECT p.content_type_id, p.codename
137 137
             #     FROM auth_permission p, auth_group_permissions gp, auth_user_groups ug
138 138
             #     WHERE p.id = gp.permission_id
139 139
             #         AND gp.group_id = ug.group_id
@@ -144,7 +144,7 @@ def get_group_permissions(self):
144 144
                 WHERE p.%s = gp.%s
145 145
                     AND gp.%s = ug.%s
146 146
                     AND ug.%s = %%s""" % (
147  
-                backend.quote_name('package'), backend.quote_name('codename'),
  147
+                backend.quote_name('content_type_id'), backend.quote_name('codename'),
148 148
                 backend.quote_name('auth_permission'), backend.quote_name('auth_group_permissions'),
149 149
                 backend.quote_name('auth_user_groups'), backend.quote_name('id'),
150 150
                 backend.quote_name('permission_id'), backend.quote_name('group_id'),
@@ -156,7 +156,7 @@ def get_group_permissions(self):
156 156
     def get_all_permissions(self):
157 157
         if not hasattr(self, '_perm_cache'):
158 158
             import sets
159  
-            self._perm_cache = sets.Set(["%s.%s" % (p.package_id, p.codename) for p in self.user_permissions.all()])
  159
+            self._perm_cache = sets.Set(["%s.%s" % (p.content_type, p.codename) for p in self.user_permissions.all()])
160 160
             self._perm_cache.update(self.get_group_permissions())
161 161
         return self._perm_cache
162 162
 
@@ -175,11 +175,11 @@ def has_perms(self, perm_list):
175 175
                 return False
176 176
         return True
177 177
 
178  
-    def has_module_perms(self, package_name):
179  
-        "Returns True if the user has any permissions in the given package."
  178
+    def has_module_perms(self, app_label):
  179
+        "Returns True if the user has any permissions in the given app label."
180 180
         if self.is_superuser:
181 181
             return True
182  
-        return bool(len([p for p in self.get_all_permissions() if p[:p.index('.')] == package_name]))
  182
+        return bool(len([p for p in self.get_all_permissions() if p[:p.index('.')] == app_label]))
183 183
 
184 184
     def get_and_delete_messages(self):
185 185
         messages = []
39  django/contrib/contenttypes/models.py
... ...
@@ -1,18 +1,6 @@
1 1
 from django.db import models
2 2
 from django.utils.translation import gettext_lazy as _
3 3
 
4  
-class Package(models.Model):
5  
-    label = models.CharField(_('label'), maxlength=20, primary_key=True)
6  
-    name = models.CharField(_('name'), maxlength=30, unique=True)
7  
-    class Meta:
8  
-        verbose_name = _('package')
9  
-        verbose_name_plural = _('packages')
10  
-        db_table = 'django_package'
11  
-        ordering = ('name',)
12  
-
13  
-    def __repr__(self):
14  
-        return self.name
15  
-
16 4
 class ContentTypeManager(models.Manager):
17 5
     def get_for_model(self, model):
18 6
         """
@@ -21,35 +9,34 @@ def get_for_model(self, model):
21 9
         """
22 10
         opts = model._meta
23 11
         try:
24  
-            return self.model._default_manager.get(python_module_name__exact=opts.module_name,
25  
-                package__label__exact=opts.app_label)
  12
+            return self.model._default_manager.get(app_label=opts.app_label,
  13
+                model=opts.object_name.lower())
26 14
         except self.model.DoesNotExist:
27 15
             # The str() is needed around opts.verbose_name because it's a
28 16
             # django.utils.functional.__proxy__ object.
29 17
             ct = self.model(name=str(opts.verbose_name),
30  
-                package=Package.objects.get(label=opts.app_label),
31  
-                python_module_name=opts.module_name)
  18
+                app_label=opts.app_label, model=opts.object_name.lower())
32 19
             ct.save()
33 20
             return ct
34 21
 
35 22
 class ContentType(models.Model):
36  
-    name = models.CharField(_('name'), maxlength=100)
37  
-    package = models.ForeignKey(Package, db_column='package')
38  
-    python_module_name = models.CharField(_('python module name'), maxlength=50)
  23
+    name = models.CharField(maxlength=100)
  24
+    app_label = models.CharField(maxlength=100)
  25
+    model = models.CharField(_('python model class name'), maxlength=100, unique=True)
39 26
     objects = ContentTypeManager()
40 27
     class Meta:
41 28
         verbose_name = _('content type')
42 29
         verbose_name_plural = _('content types')
43 30
         db_table = 'django_content_type'
44  
-        ordering = ('package', 'name')
45  
-        unique_together = (('package', 'python_module_name'),)
  31
+        ordering = ('name',)
46 32
 
47 33
     def __repr__(self):
48  
-        return "%s | %s" % (self.package_id, self.name)
  34
+        return self.name
49 35
 
50  
-    def get_model_module(self):
51  
-        "Returns the Python model module for accessing this type of content."
52  
-        return __import__('django.models.%s.%s' % (self.package_id, self.python_module_name), '', '', [''])
  36
+    def model_class(self):
  37
+        "Returns the Python model class for this type of content."
  38
+        from django.db import models
  39
+        return models.get_model(self.app_label, self.model)
53 40
 
54 41
     def get_object_for_this_type(self, **kwargs):
55 42
         """
@@ -58,4 +45,4 @@ def get_object_for_this_type(self, **kwargs):
58 45
         method. The ObjectNotExist exception, if thrown, will not be caught,
59 46
         so code that calls this method should catch it.
60 47
         """
61  
-        return self.get_model_module().get_object(**kwargs)
  48
+        return self.model_class()._default_manager.get(**kwargs)

0 notes on commit dac1a68

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