From 1ab27e9a65015373a49688f3ff6723cf85d5de56 Mon Sep 17 00:00:00 2001 From: joaoxsouls Date: Mon, 14 Oct 2013 13:48:01 +0100 Subject: [PATCH] Fixed #18866 -- added validation error for verbose_name longer than 39 characters Added a validation error check when creating the permissions for model, to avoid cryptic database error when the verbose_name is longer than 39 characters thanks elena for reporting it --- AUTHORS | 1 + django/contrib/auth/management/__init__.py | 6 ++++++ django/contrib/auth/tests/test_management.py | 12 ++++++++++++ 3 files changed, 19 insertions(+) diff --git a/AUTHORS b/AUTHORS index 0b270fec81..74f0aae2f0 100644 --- a/AUTHORS +++ b/AUTHORS @@ -463,6 +463,7 @@ answer newbie questions, and generally made Django that much better: Neal Norwitz Todd O'Bryan Alex Ogier + Joao Oliveira Selwin Ong Gerardo Orozco Christian Oudard diff --git a/django/contrib/auth/management/__init__.py b/django/contrib/auth/management/__init__.py index cb77694d1c..dafcda243a 100644 --- a/django/contrib/auth/management/__init__.py +++ b/django/contrib/auth/management/__init__.py @@ -99,6 +99,12 @@ def create_permissions(app, created_models, verbosity, db=DEFAULT_DB_ALIAS, **kw for ctype, (codename, name) in searched_perms if (ctype.pk, codename) not in all_perms ] + # Validate the permissions before bulk_creation to avoid cryptic + # database error when the verbose_name is longer than 50 characters + for perm in perms: + if len(perm.name) > 50: + raise exceptions.ValidationError( + "The verbose_name of %s is longer than 39 characters" % perm.content_type) auth_app.Permission.objects.using(db).bulk_create(perms) if verbosity >= 2: for perm in perms: diff --git a/django/contrib/auth/tests/test_management.py b/django/contrib/auth/tests/test_management.py index e56df0676b..edf433c835 100644 --- a/django/contrib/auth/tests/test_management.py +++ b/django/contrib/auth/tests/test_management.py @@ -8,6 +8,7 @@ from django.contrib.auth.models import User from django.contrib.auth.tests.custom_user import CustomUser from django.contrib.auth.tests.utils import skipIfCustomUser from django.contrib.contenttypes.models import ContentType +from django.core import exceptions from django.core.management import call_command from django.core.management.base import CommandError from django.core.management.validation import get_validation_errors @@ -201,10 +202,12 @@ class PermissionTestCase(TestCase): def setUp(self): self._original_permissions = models.Permission._meta.permissions[:] self._original_default_permissions = models.Permission._meta.default_permissions + self._original_verbose_name = models.Permission._meta.verbose_name def tearDown(self): models.Permission._meta.permissions = self._original_permissions models.Permission._meta.default_permissions = self._original_default_permissions + models.Permission._meta.verbose_name = self._original_verbose_name ContentType.objects.clear_cache() def test_duplicated_permissions(self): @@ -258,3 +261,12 @@ class PermissionTestCase(TestCase): self.assertEqual(models.Permission.objects.filter( content_type=permission_content_type, ).count(), 1) + + def test_verbose_name_length(self): + permission_content_type = ContentType.objects.get_by_natural_key('auth', 'permission') + models.Permission.objects.filter(content_type=permission_content_type).delete() + models.Permission._meta.verbose_name = "some ridiculously long verbose name that is out of control" + + six.assertRaisesRegex(self, exceptions.ValidationError, + "The verbose_name of permission is longer than 39 characters", + create_permissions, models, [], verbosity=0)