Ticket #1280: contenttypes.diff

File contenttypes.diff, 10.4 KB (added by Esaj, 16 years ago)
  • django/contrib/contenttypes/models.py

     
    11from django.db import models
    22from django.utils.translation import gettext_lazy as _
    33
    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 
    164class ContentType(models.Model):
    175    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
    208    class Meta:
    219        verbose_name = _('content type')
    2210        verbose_name_plural = _('content types')
    2311        db_table = 'django_content_type'
    24         ordering = ('package', 'name')
    25         unique_together = (('package', 'python_module_name'),)
     12        ordering = ('name',)
    2613
    2714    def __repr__(self):
    28         return "%s | %s" % (self.package_id, self.name)
     15        return self.name
    2916
    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:])
    3323
    3424    def get_object_for_this_type(self, **kwargs):
    3525        """
     
    3828        method. The ObjectNotExist exception, if thrown, will not be caught,
    3929        so code that calls this method should catch it.
    4030        """
    41         return self.get_model_module().get_object(**kwargs)
     31        return self.get_model_class().objects.get(**kwargs)
  • django/contrib/auth/models.py

     
    11from django.core import validators
    22from django.db import backend, connection, models
    3 from django.contrib.contenttypes.models import Package
     3from django.contrib.contenttypes.models import ContentType
    44from django.utils.translation import gettext_lazy as _
    55import datetime
    66
     
    1111
    1212class Permission(models.Model):
    1313    name = models.CharField(_('name'), maxlength=50)
    14     package = models.ForeignKey(Package, db_column='package')
     14    content_type = models.ForeignKey(ContentType, db_column='content_type')
    1515    codename = models.CharField(_('codename'), maxlength=100)
    1616    class Meta:
    1717        verbose_name = _('Permission')
    1818        verbose_name_plural = _('Permissions')
    19         unique_together = (('package', 'codename'),)
    20         ordering = ('package', 'codename')
     19        unique_together = (('content_type', 'codename'),)
     20        ordering = ('content_type', 'codename')
    2121
    2222    def __repr__(self):
    23         return "%s | %s" % (self.package_id, self.name)
     23        return "%s | %s" % (self.content_type, self.name)
    2424
    2525class Group(models.Model):
    2626    name = models.CharField(_('name'), maxlength=80, unique=True)
     
    133133            cursor = connection.cursor()
    134134            # The SQL below works out to the following, after DB quoting:
    135135            # cursor.execute("""
    136             #     SELECT p.package, p.codename
     136            #     SELECT p.content_type, p.codename
    137137            #     FROM auth_permission p, auth_group_permissions gp, auth_user_groups ug
    138138            #     WHERE p.id = gp.permission_id
    139139            #         AND gp.group_id = ug.group_id
     
    144144                WHERE p.%s = gp.%s
    145145                    AND gp.%s = ug.%s
    146146                    AND ug.%s = %%s""" % (
    147                 backend.quote_name('package'), backend.quote_name('codename'),
     147                backend.quote_name('content_type'), backend.quote_name('codename'),
    148148                backend.quote_name('auth_permission'), backend.quote_name('auth_group_permissions'),
    149149                backend.quote_name('auth_user_groups'), backend.quote_name('id'),
    150150                backend.quote_name('permission_id'), backend.quote_name('group_id'),
     
    156156    def get_all_permissions(self):
    157157        if not hasattr(self, '_perm_cache'):
    158158            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()])
    160160            self._perm_cache.update(self.get_group_permissions())
    161161        return self._perm_cache
    162162
     
    175175                return False
    176176        return True
    177177
    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."
    180181        if self.is_superuser:
    181182            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]))
    183184
    184185    def get_and_delete_messages(self):
    185186        messages = []
  • django/core/management.py

     
    2626
    2727INVALID_PROJECT_NAMES = ('django', 'test')
    2828
    29 def _get_packages_insert(app_label):
    30     from django.db import backend
    31     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 
    3529def _get_permission_codename(action, opts):
    3630    return '%s_%s' % (action, opts.object_name.lower())
    3731
     
    4337            perms.append((_get_permission_codename(action, opts), 'Can %s %s' % (action, opts.verbose_name)))
    4438    return perms + list(opts.permissions)
    4539
    46 def _get_permission_insert(name, codename, opts):
     40def _get_permission_insert(name, codename, klass):
    4741    from django.db import backend
    4842    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)
    5145
    52 def _get_contenttype_insert(opts):
     46def _get_contenttype_insert(klass):
    5347    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__)
    5751
    5852def _is_valid_dir_name(s):
    5953    return bool(re.search(r'^\w+$', s))
     
    290284
    291285    app_models = get_models(app)
    292286    app_label = app_models[0]._meta.app_label
    293     output.append(_get_packages_insert(app_label))
    294287    app_dir = os.path.normpath(os.path.join(os.path.dirname(app.__file__), 'sql'))
    295288
    296289    for klass in app_models:
     
    307300                fp.close()
    308301
    309302        # Content types.
    310         output.append(_get_contenttype_insert(opts))
     303        output.append(_get_contenttype_insert(klass))
    311304        # Permissions.
    312305        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))
    314307    return output
    315308get_sql_initial_data.help_doc = "Prints the initial INSERT SQL statements for the given app name(s)."
    316309get_sql_initial_data.args = APP_ARGS
  • django/db/models/options.py

     
    8888        "Returns the content-type ID for this object type."
    8989        if not hasattr(self, '_content_type_id'):
    9090            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
    9492        return self._content_type_id
    9593
    9694    def get_field(self, name, many_to_many=True):
  • django/db/models/manipulators.py

     
    185185                        # Related links are a special case, because we have to
    186186                        # manually set the "content_type_id" and "object_id" fields.
    187187                        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()
    190189                            params['object_id'] = new_object.id
    191190
    192191                    # Create the related item.
Back to Top