Ticket #15915: 15915.2.diff
File 15915.2.diff, 4.8 KB (added by , 13 years ago) |
---|
-
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 7 7 from django.contrib.auth import models as auth_app 8 8 from django.db.models import get_models, signals 9 9 from django.contrib.auth.models import User 10 from django.core.management.base import CommandError 10 11 11 12 12 13 def _get_permission_codename(action, opts): 13 14 return u'%s_%s' % (action, opts.object_name.lower()) 14 15 15 16 16 def _get_all_permissions(opts): 17 "Returns (codename, name) for all permissions in the given opts." 17 def _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 33 def _get_builtin_permissions(opts): 34 "Returns (codename, name) for all autogenerated permissions." 18 35 perms = [] 19 36 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 42 def _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 22 48 23 49 24 50 def create_permissions(app, created_models, verbosity, **kwargs): … … def create_permissions(app, created_models, verbosity, **kwargs): 34 60 for klass in app_models: 35 61 ctype = ContentType.objects.get_for_model(klass) 36 62 ctypes.add(ctype) 37 for perm in _get_all_permissions(klass._meta ):63 for perm in _get_all_permissions(klass._meta, ctype): 38 64 searched_perms.append((ctype, perm)) 39 65 40 66 # 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 1 1 from django.test import TestCase 2 from django.contrib.auth.management import create_permissions 2 3 from django.contrib.auth import models, management 3 4 from django.core.management import CommandError 4 5 5 6 class GetDefaultUsernameTestCase(TestCase): 6 7 … … class GetDefaultUsernameTestCase(TestCase): 25 26 # 'Julia' with accented 'u': 26 27 management.get_system_username = lambda: u'J\xfalia' 27 28 self.assertEqual(management.get_default_username(), 'julia') 29 30 class 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