Code

Ticket #9057: 9057.diff

File 9057.diff, 3.7 KB (added by koenb, 3 years ago)

patch with no_default_permission meta option

Line 
1Index: tests/regressiontests/auth_permissions/__init__.py
2===================================================================
3Index: tests/regressiontests/auth_permissions/tests.py
4===================================================================
5--- tests/regressiontests/auth_permissions/tests.py     (revision 0)
6+++ tests/regressiontests/auth_permissions/tests.py     (revision 0)
7@@ -0,0 +1,15 @@
8+from django.utils import unittest
9+
10+from django.contrib.auth.models import Permission
11+from django.contrib.contenttypes.models import ContentType
12+
13+class AuthPermissionsTest(unittest.TestCase):
14+
15+    def test_permissions(self):
16+        self.assertEqual(Permission.objects.filter(content_type=
17+                ContentType.objects.get_by_natural_key('auth_permissions', 'poll')
18+            ).count(), 3)
19+        self.assertEqual(Permission.objects.filter(content_type=
20+                ContentType.objects.get_by_natural_key('auth_permissions', 'choice')
21+            ).count(), 1)
22+
23Index: tests/regressiontests/auth_permissions/models.py
24===================================================================
25--- tests/regressiontests/auth_permissions/models.py    (revision 0)
26+++ tests/regressiontests/auth_permissions/models.py    (revision 0)
27@@ -0,0 +1,17 @@
28+from django.db import models
29+
30+class Poll(models.Model):
31+    question = models.CharField(max_length=200)
32+    pub_date = models.DateTimeField('date published')
33+
34+class Choice(models.Model):
35+    poll = models.ForeignKey(Poll)
36+    choice = models.CharField(max_length=200)
37+    votes = models.IntegerField()
38+
39+    class Meta:
40+        no_default_permissions = True
41+        permissions = (
42+            ("display_choices", "May display choices"),
43+        )
44+
45Index: django/db/models/options.py
46===================================================================
47--- django/db/models/options.py (revision 16906)
48+++ django/db/models/options.py (working copy)
49@@ -15,9 +15,9 @@
50 get_verbose_name = lambda class_name: re.sub('(((?<=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))', ' \\1', class_name).lower().strip()
51 
52 DEFAULT_NAMES = ('verbose_name', 'verbose_name_plural', 'db_table', 'ordering',
53-                 'unique_together', 'permissions', 'get_latest_by',
54-                 'order_with_respect_to', 'app_label', 'db_tablespace',
55-                 'abstract', 'managed', 'proxy', 'auto_created')
56+                 'unique_together', 'no_default_permissions', 'permissions',
57+                 'get_latest_by', 'order_with_respect_to', 'app_label',
58+                 'db_tablespace', 'abstract', 'managed', 'proxy', 'auto_created')
59 
60 class Options(object):
61     def __init__(self, meta, app_label=None):
62@@ -28,6 +28,7 @@
63         self.db_table = ''
64         self.ordering = []
65         self.unique_together =  []
66+        self.no_default_permissions = False
67         self.permissions =  []
68         self.object_name, self.app_label = None, app_label
69         self.get_latest_by = None
70Index: django/contrib/auth/management/__init__.py
71===================================================================
72--- django/contrib/auth/management/__init__.py  (revision 16906)
73+++ django/contrib/auth/management/__init__.py  (working copy)
74@@ -16,8 +16,9 @@
75 def _get_all_permissions(opts):
76     "Returns (codename, name) for all permissions in the given opts."
77     perms = []
78-    for action in ('add', 'change', 'delete'):
79-        perms.append((_get_permission_codename(action, opts), u'Can %s %s' % (action, opts.verbose_name_raw)))
80+    if not opts.no_default_permissions:
81+        for action in ('add', 'change', 'delete'):
82+            perms.append((_get_permission_codename(action, opts), u'Can %s %s' % (action, opts.verbose_name_raw)))
83     return perms + list(opts.permissions)
84 
85