Ticket #550: patch_limit_admin_view_based_on_user_permissions.txt

File patch_limit_admin_view_based_on_user_permissions.txt, 5.0 KB (added by Jason Huggins, 19 years ago)
Line 
1--- trunk/django/django/conf/admin_templates/index.html (revision 656)
2+++ trunk/django/django/conf/admin_templates/index.html (working copy)
3@@ -9,20 +9,40 @@
4 {% load adminapplist %}
5
6 {% get_admin_app_list as app_list %}
7-{% for app in app_list %}
8- <div class="module">
9- <h2>{{ app.name }}</h2>
10- <table>
11- {% for model in app.models %}
12- <tr>
13- <th><a href="{{ model.admin_url }}">{{ model.name }}</a></th>
14- <td class="x50"><a href="{{ model.admin_url }}add/" class="addlink">Add</a></td>
15- <td class="x75"><a href="{{ model.admin_url }}" class="changelink">Change</a></td>
16- </tr>
17+{% if user_has_perms_in_any_app %}
18+ {% for app in app_list %}
19+ {% if app.has_module_perms %}
20+ <div class="module">
21+ <h2>{{ app.name }}</h2>
22+ <table>
23+ {% for model in app.models %}
24+ <tr>
25+ {% if model.perms.change %}
26+ <th><a href="{{ model.admin_url }}">{{ model.name }}</a></th>
27+ {% else %}
28+ <th>{{ model.name }}</th>
29+ {% endif %}
30+
31+ <td class="x125" style="align:right;">
32+ {% if model.perms.add %}
33+ <a href="{{ model.admin_url }}add/" class="addlink">Add</a>&nbsp;&nbsp;
34+ {% endif %}
35+
36+ {% if model.perms.change %}
37+ <a href="{{ model.admin_url }}" class="changelink">Change</a>
38+ {% endif %}
39+ </td>
40+
41+ </tr>
42+ {% endfor %}
43+ </table>
44+ </div>
45+ {% endif %}
46 {% endfor %}
47- </table>
48- </div>
49-{% endfor %}
50+
51+{% else %}
52+ <p>You do not have any permissions in any application.</p>
53+{% endif %}
54
55 </div>
56 {% endblock %}
57--- trunk/django/django/conf/admin_media/css/global.css (revision 656)
58+++ trunk/django/django/conf/admin_media/css/global.css (working copy)
59@@ -99,6 +99,7 @@
60 .x50 { width:50px; }
61 .x75 { width:75px; }
62 .x100 { width:100px; }
63+.x125 { width:125px; }
64 .x150 { width:150px; }
65 .x200 { width:200px; }
66 .x250 { width:250px; }
67--- trunk/django/django/templatetags/adminapplist.py (revision 656)
68+++ trunk/django/django/templatetags/adminapplist.py (working copy)
69@@ -8,23 +8,52 @@
70 from django.core import meta
71 from django.utils.text import capfirst
72 app_list = []
73+ user = context['user']
74+ user_has_perms_in_any_app = False
75+
76 for app in meta.get_installed_model_modules():
77 app_label = app.__name__[app.__name__.rindex('.')+1:]
78- model_list = [{'name': capfirst(m._meta.verbose_name_plural),
79- 'admin_url': '%s/%s/' % (app_label, m._meta.module_name)} \
80- for m in app._MODELS if m._meta.admin]
81+ has_module_perms = user.has_module_perms(app_label)
82+ if has_module_perms == True:
83+ user_has_perms_in_any_app = True
84+
85+ model_list = []
86+ for m in app._MODELS:
87+ if m._meta.admin:
88+ module_name = m._meta.module_name
89+ perms = {}
90+ add_perm = "%s.%s" % (app_label, m._meta.get_add_permission())
91+ perms['add'] = user.has_perm(add_perm)
92+
93+ change_perm = "%s.%s" % (app_label, m._meta.get_change_permission())
94+ perms['change'] = user.has_perm(change_perm)
95+
96+ delete_perm = "%s.%s" % (app_label, m._meta.get_delete_permission())
97+ perms['delete'] = user.has_perm(delete_perm)
98+
99+ # Check to see if user has any perm for this one module
100+ # If so, add the module to the model_list
101+ if True in [perm for perm in perms.values()]:
102+ model_dict = {}
103+ model_dict['name'] = capfirst(m._meta.verbose_name_plural)
104+ model_dict['admin_url'] = '%s/%s/' % (app_label, m._meta.module_name)
105+ model_dict['perms'] = perms
106+ model_list.append(model_dict)
107+
108 if model_list:
109 app_list.append({
110 'name': app_label.title(),
111+ 'has_module_perms': has_module_perms,
112 'models': model_list,
113 })
114 context[self.varname] = app_list
115+ context['user_has_perms_in_any_app'] = user_has_perms_in_any_app
116 return ''
117
118 def get_admin_app_list(parser, token):
119 """
120 {% get_admin_app_list as app_list %}
121- """
122+ """
123 tokens = token.contents.split()
124 if len(tokens) < 3:
125 raise template.TemplateSyntaxError, "'%s' tag requires two arguments" % tokens[0]
126@@ -33,3 +62,4 @@
127 return AdminApplistNode(tokens[2])
128
129 template.register_tag('get_admin_app_list', get_admin_app_list)
130+
Back to Top