Ticket #1280: contenttypes.diff
File contenttypes.diff, 10.4 KB (added by , 19 years ago) |
---|
-
django/contrib/contenttypes/models.py
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.name15 16 4 class ContentType(models.Model): 17 5 name = models.CharField(_('name'), maxlength=100) 18 package = models.ForeignKey(Package, db_column='package')19 python_module_name = models.CharField(_('python module name'), maxlength=50) 6 model = models.CharField(_('python model class name'), maxlength=100, primary_key=True) 7 20 8 class Meta: 21 9 verbose_name = _('content type') 22 10 verbose_name_plural = _('content types') 23 11 db_table = 'django_content_type' 24 ordering = ('package', 'name') 25 unique_together = (('package', 'python_module_name'),) 12 ordering = ('name',) 26 13 27 14 def __repr__(self): 28 return "%s | %s" % (self.package_id, self.name)15 return self.name 29 16 30 def get_model_module(self): 31 "Returns the Python model module for accessing this type of content." 32 return __import__('django.models.%s.%s' % (self.package_id, self.python_module_name), '', '', ['']) 17 def get_model_class(self): 18 "Returns the Python model class for accessing this type of content." 19 lastdot = self.model.rfind('.') 20 package = lastdot > 0 and self.model[:lastdot] or '' 21 module = __import__(package, '', '', ['']) 22 return getattr(module, self.model[lastdot+1:]) 33 23 34 24 def get_object_for_this_type(self, **kwargs): 35 25 """ … … 38 28 method. The ObjectNotExist exception, if thrown, will not be caught, 39 29 so code that calls this method should catch it. 40 30 """ 41 return self.get_model_ module().get_object(**kwargs)31 return self.get_model_class().objects.get(**kwargs) -
django/contrib/auth/models.py
1 1 from django.core import validators 2 2 from django.db import backend, connection, models 3 from django.contrib.contenttypes.models import Package3 from django.contrib.contenttypes.models import ContentType 4 4 from django.utils.translation import gettext_lazy as _ 5 5 import datetime 6 6 … … 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, db_column='content_type') 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 "%s | %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 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.codename136 # SELECT p.content_type, 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 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'), 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 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.get_permission_list()])159 self._perm_cache = sets.Set(["%s.%s" % (p.content_type, p.codename) for p in self.get_permission_list()]) 160 160 self._perm_cache.update(self.get_group_permissions()) 161 161 return self._perm_cache 162 162 … … 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 # TODO: Rename to has_model_perms? 179 def has_module_perms(self, model_name): 180 "Returns True if the user has any permissions for the given model." 180 181 if self.is_superuser: 181 182 return True 182 return bool(len([p for p in self.get_all_permissions() if p[:p.index('.')] == package_name]))183 return bool(len([p for p in self.get_all_permissions() if p[:p.index('.')] == model_name])) 183 184 184 185 def get_and_delete_messages(self): 185 186 messages = [] -
django/core/management.py
26 26 27 27 INVALID_PROJECT_NAMES = ('django', 'test') 28 28 29 def _get_packages_insert(app_label):30 from django.db import backend31 return "INSERT INTO %s (%s, %s) VALUES ('%s', '%s');" % \32 (backend.quote_name('django_package'), backend.quote_name('label'), backend.quote_name('name'),33 app_label, app_label)34 35 29 def _get_permission_codename(action, opts): 36 30 return '%s_%s' % (action, opts.object_name.lower()) 37 31 … … 43 37 perms.append((_get_permission_codename(action, opts), 'Can %s %s' % (action, opts.verbose_name))) 44 38 return perms + list(opts.permissions) 45 39 46 def _get_permission_insert(name, codename, opts):40 def _get_permission_insert(name, codename, klass): 47 41 from django.db import backend 48 42 return "INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s');" % \ 49 (backend.quote_name('auth_permission'), backend.quote_name('name'), backend.quote_name(' package'),50 backend.quote_name('codename'), name.replace("'", "''"), opts.app_label, codename)43 (backend.quote_name('auth_permission'), backend.quote_name('name'), backend.quote_name('content_type'), 44 backend.quote_name('codename'), name.replace("'", "''"), klass.__module__+'.'+klass.__name__, codename) 51 45 52 def _get_contenttype_insert( opts):46 def _get_contenttype_insert(klass): 53 47 from django.db import backend 54 return "INSERT INTO %s (%s, %s , %s) VALUES ('%s','%s', '%s');" % \55 (backend.quote_name('django_content_type'), backend.quote_name('name'), backend.quote_name(' package'),56 backend.quote_name('python_module_name'), opts.verbose_name, opts.app_label, opts.module_name)48 return "INSERT INTO %s (%s, %s) VALUES ('%s', '%s');" % \ 49 (backend.quote_name('django_content_type'), backend.quote_name('name'), backend.quote_name('model'), 50 klass._meta.object_name, klass.__module__+'.'+klass.__name__) 57 51 58 52 def _is_valid_dir_name(s): 59 53 return bool(re.search(r'^\w+$', s)) … … 290 284 291 285 app_models = get_models(app) 292 286 app_label = app_models[0]._meta.app_label 293 output.append(_get_packages_insert(app_label))294 287 app_dir = os.path.normpath(os.path.join(os.path.dirname(app.__file__), 'sql')) 295 288 296 289 for klass in app_models: … … 307 300 fp.close() 308 301 309 302 # Content types. 310 output.append(_get_contenttype_insert( opts))303 output.append(_get_contenttype_insert(klass)) 311 304 # Permissions. 312 305 for codename, name in _get_all_permissions(opts): 313 output.append(_get_permission_insert(name, codename, opts))306 output.append(_get_permission_insert(name, codename, klass)) 314 307 return output 315 308 get_sql_initial_data.help_doc = "Prints the initial INSERT SQL statements for the given app name(s)." 316 309 get_sql_initial_data.args = APP_ARGS -
django/db/models/options.py
88 88 "Returns the content-type ID for this object type." 89 89 if not hasattr(self, '_content_type_id'): 90 90 from django.contrib.contenttypes.models import ContentType 91 self._content_type_id = ContentType.objects.get( 92 python_module_name__exact=self.module_name, 93 package__label__exact=self.app_label).id 91 self._content_type_id = ContentType.objects.get(model__exact=self.module_name+'.'+self.object_name).id 94 92 return self._content_type_id 95 93 96 94 def get_field(self, name, many_to_many=True): -
django/db/models/manipulators.py
185 185 # Related links are a special case, because we have to 186 186 # manually set the "content_type_id" and "object_id" fields. 187 187 if self.opts.has_related_links and related.opts.module_name == 'relatedlinks': 188 contenttypes_mod = get_module('core', 'contenttypes') 189 params['content_type_id'] = contenttypes_mod.get_object(package__label__exact=self.opts.app_label, python_module_name__exact=self.opts.module_name).id 188 params['content_type_id'] = self.opts.get_content_type_id() 190 189 params['object_id'] = new_object.id 191 190 192 191 # Create the related item.