Ticket #25057: 25057_permission_for_view_with_locales.diff

File 25057_permission_for_view_with_locales.diff, 5.1 KB (added by Grigoriy Kramarenko, 9 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 6380d2f..d408d35 100644
    a b import getpass  
    77import unicodedata
    88
    99from django.apps import apps
     10from django.conf import settings
    1011from django.contrib.auth import get_permission_codename
    1112from django.core import exceptions
    1213from django.core.management.base import CommandError
    1314from django.db import DEFAULT_DB_ALIAS, router
    1415from django.utils import six
    1516from django.utils.encoding import DEFAULT_LOCALE_ENCODING
     17from django.utils.translation import activate, get_language, ugettext as _
    1618
    1719
    1820def _get_all_permissions(opts, ctype):
    def _get_all_permissions(opts, ctype):  
    2830def _get_builtin_permissions(opts):
    2931    """
    3032    Returns (codename, name) for all autogenerated permissions.
    31     By default, this is ('add', 'change', 'delete')
     33    By default, this is ('view', 'add', 'change', 'delete')
    3234    """
    3335    perms = []
     36    trans = {
     37        'view':   _('Can view %s'),
     38        'add':    _('Can add %s'),
     39        'change': _('Can change %s'),
     40        'delete': _('Can delete %s'),
     41    }
    3442    for action in opts.default_permissions:
    3543        perms.append((get_permission_codename(action, opts),
    36             'Can %s %s' % (action, opts.verbose_name_raw)))
     44            trans.get(action, 'Can '+action+' %s') %  opts.verbose_name))
    3745    return perms
    3846
    3947
    def create_permissions(app_config, verbosity=2, interactive=True, using=DEFAULT_  
    7179
    7280    from django.contrib.contenttypes.models import ContentType
    7381
     82    # setup locale
     83    lang = get_language()
     84    try:
     85        activate(settings.LANGUAGE_CODE)
     86    except:
     87        pass
     88
     89    # For localized prefixes ('Can view ', 'Can add ', 'Can change '
     90    # and 'Can delete ') digit `30` - probably maximum the length of
     91    # two words in all languages
    7492    permission_name_max_length = Permission._meta.get_field('name').max_length
    75     verbose_name_max_length = permission_name_max_length - 11  # len('Can change ') prefix
     93    verbose_name_max_length = permission_name_max_length - 30
    7694
    7795    # This will hold the permissions we're looking for as
    7896    # (content_type, (codename, name))
    def create_permissions(app_config, verbosity=2, interactive=True, using=DEFAULT_  
    128146        for perm in perms:
    129147            print("Adding permission '%s'" % perm)
    130148
     149    # revert locale
     150    activate(lang)
     151
     152
    131153
    132154def get_system_username():
    133155    """
  • django/contrib/auth/models.py

    diff --git a/django/contrib/auth/models.py b/django/contrib/auth/models.py
    index ad46c5c..e028926 100644
    a b class Permission(models.Model):  
    4949          list, view the "change" form and change an object.
    5050        - The "delete" permission limits the ability to delete an object.
    5151
     52    The "view" permission allows the user's to view the object without
     53    the possibility of change. This permission not uses in Django admin.
     54
    5255    Permissions are set globally per type of object, not per specific object
    5356    instance. It is possible to say "Mary may change news stories," but it's
    5457    not currently possible to say "Mary may change news stories, but only the
  • django/db/models/options.py

    diff --git a/django/db/models/options.py b/django/db/models/options.py
    index 368ac62..3f1f3c5 100644
    a b class Options(object):  
    106106        self.unique_together = []
    107107        self.index_together = []
    108108        self.select_on_save = False
    109         self.default_permissions = ('add', 'change', 'delete')
     109        self.default_permissions = ('view', 'add', 'change', 'delete')
    110110        self.permissions = []
    111111        self.object_name = None
    112112        self.app_label = app_label
  • tests/auth_tests/test_management.py

    diff --git a/tests/auth_tests/test_management.py b/tests/auth_tests/test_management.py
    index f45703f..40f50c9 100644
    a b class PermissionTestCase(TestCase):  
    570570        ]
    571571        create_permissions(auth_app_config, verbosity=0)
    572572
    573         # add/change/delete permission by default + custom permission
     573        # view/add/change/delete permission by default + custom permission
    574574        self.assertEqual(models.Permission.objects.filter(
    575575            content_type=permission_content_type,
    576         ).count(), 4)
     576        ).count(), 5)
    577577
    578578        models.Permission.objects.filter(content_type=permission_content_type).delete()
    579579        models.Permission._meta.default_permissions = []
    class PermissionTestCase(TestCase):  
    592592        models.Permission._meta.verbose_name = "some ridiculously long verbose name that is out of control" * 5
    593593
    594594        six.assertRaisesRegex(self, exceptions.ValidationError,
    595             "The verbose_name of auth.permission is longer than 244 characters",
     595            "The verbose_name of auth.permission is longer than 225 characters",
    596596            create_permissions, auth_app_config, verbosity=0)
    597597
    598598    def test_custom_permission_name_length(self):
Back to Top