Ticket #15915: 15915.2.diff

File 15915.2.diff, 4.8 KB (added by ptone, 4 years ago)

updated patch for 16730

  • django/contrib/auth/management/__init__.py

    diff --git a/django/contrib/auth/management/__init__.py b/django/contrib/auth/management/__init__.py
    index f82060e..6fabdc2 100644
    a b import unicodedata 
    77from django.contrib.auth import models as auth_app
    88from django.db.models import get_models, signals
    99from django.contrib.auth.models import User
     10from django.core.management.base import CommandError
    1011
    1112
    1213def _get_permission_codename(action, opts):
    1314    return u'%s_%s' % (action, opts.object_name.lower())
    1415
    1516
    16 def _get_all_permissions(opts):
    17     "Returns (codename, name) for all permissions in the given opts."
     17def _check_permissions_clashing(custom, builtin, ctype):
     18    "Raises CommandError if there are duplicate permissions."
     19    pool = set()
     20    builtin_codenames = set(p[0] for p in builtin)
     21    for codename, _name in custom:
     22        if codename in pool:
     23            raise CommandError(
     24                "The permission codename is duplicated for model %s.%s: %s" %
     25                (ctype.app_label, ctype.model_class().__name__, codename))
     26        elif codename in builtin_codenames:
     27            raise CommandError(
     28                "The permission codename %s clashes with a builtin for model "
     29                "%s.%s." %
     30                (codename, ctype.app_label, ctype.model_class().__name__))
     31        pool.add(codename)
     32
     33def _get_builtin_permissions(opts):
     34    "Returns (codename, name) for all autogenerated permissions."
    1835    perms = []
    1936    for action in ('add', 'change', 'delete'):
    20         perms.append((_get_permission_codename(action, opts), u'Can %s %s' % (action, opts.verbose_name_raw)))
    21     return perms + list(opts.permissions)
     37        perms.append(
     38            (_get_permission_codename(action, opts),
     39             u'Can %s %s' % (action, opts.verbose_name_raw)))
     40    return perms
     41
     42def _get_all_permissions(opts, ctype):
     43    "Returns (codename, name) for all permissions in the given opts."
     44    builtin = _get_builtin_permissions(opts)
     45    custom = list(opts.permissions)
     46    _check_permissions_clashing(custom, builtin, ctype)
     47    return builtin + custom
    2248
    2349
    2450def create_permissions(app, created_models, verbosity, **kwargs):
    def create_permissions(app, created_models, verbosity, **kwargs): 
    3460    for klass in app_models:
    3561        ctype = ContentType.objects.get_for_model(klass)
    3662        ctypes.add(ctype)
    37         for perm in _get_all_permissions(klass._meta):
     63        for perm in _get_all_permissions(klass._meta, ctype):
    3864            searched_perms.append((ctype, perm))
    3965
    4066    # Find all the Permissions that have a context_type for a model we're
  • django/contrib/auth/tests/management.py

    diff --git a/django/contrib/auth/tests/management.py b/django/contrib/auth/tests/management.py
    index 0af6873..ab1a29c 100644
    a b  
    11from django.test import TestCase
     2from django.contrib.auth.management import create_permissions
    23from django.contrib.auth import models, management
    3 
     4from django.core.management import CommandError
    45
    56class GetDefaultUsernameTestCase(TestCase):
    67
    class GetDefaultUsernameTestCase(TestCase): 
    2526        # 'Julia' with accented 'u':
    2627        management.get_system_username = lambda: u'J\xfalia'
    2728        self.assertEqual(management.get_default_username(), 'julia')
     29
     30class TestPermissionDuplication(TestCase):
     31
     32    def setUp(self):
     33        self._original_user_permissions = models.User._meta.permissions
     34
     35    def tearDown(self):
     36        models.User._meta.permissions = self._original_user_permissions
     37
     38
     39    def test_permission_duplicates_checking(self):
     40        """Test that we show proper error message if we are trying to
     41        create duplicate permissions.
     42        """
     43        models.User._meta.permissions = [
     44           ('change_user', 'Can edit user (duplicate)')]
     45        self.assertRaisesRegexp(CommandError,
     46            'The permission codename change_user clashes with a builtin '
     47            'for model auth.User.',
     48            create_permissions, models, [], verbosity=0)
     49
     50        models.User._meta.permissions = [
     51            ('my_custom_permission', 'Some permission'),
     52            ('other_one', 'Some other permission'),
     53            ('my_custom_permission', 'Some permission with duplicate code'),
     54        ]
     55        self.assertRaisesRegexp(CommandError,
     56            'The permission codename is duplicated for model auth.User: '
     57            'my_custom_permission',
     58            create_permissions, models, [], verbosity=0)
     59
     60        # should not raise anything
     61        models.User._meta.permissions = [
     62            ('my_custom_permission', 'Some permission'),
     63            ('other_one', 'Some other permission'),
     64        ]
     65        create_permissions(models, [], verbosity=0)
     66
Back to Top