@@ -118,6 +118,7 @@ class ModelAdminView(object):
118
118
"Class that encapsulates all admin views for a given model."
119
119
def __init__ (self , model ):
120
120
self .model = model
121
+ self .opts = model ._meta
121
122
122
123
def __call__ (self , request , url ):
123
124
if url is None :
@@ -131,17 +132,38 @@ def __call__(self, request, url):
131
132
else :
132
133
return self .change_view (request , unquote (url ))
133
134
135
+ def has_add_permission (self , request ):
136
+ "Returns True if the given request has permission to add an object."
137
+ opts = self .opts
138
+ return request .user .has_perm (opts .app_label + '.' + opts .get_add_permission ())
139
+
140
+ def has_change_permission (self , request , object_id ):
141
+ """
142
+ Returns True if the given request has permission to change the object
143
+ with the given object_id.
144
+ """
145
+ opts = self .opts
146
+ return request .user .has_perm (opts .app_label + '.' + opts .get_change_permission ())
147
+
148
+ def has_delete_permission (self , request , object_id ):
149
+ """
150
+ Returns True if the given request has permission to change the object
151
+ with the given object_id.
152
+ """
153
+ opts = self .opts
154
+ return request .user .has_perm (opts .app_label + '.' + opts .get_delete_permission ())
155
+
134
156
def add_view (self , request , show_delete = False , form_url = '' , post_url = None , post_url_continue = '../%s/' , object_id_override = None ):
135
157
"The 'add' admin view for this model."
136
158
model = self .model
137
159
opts = model ._meta
138
160
app_label = opts .app_label
139
161
140
- if not request . user . has_perm ( app_label + '.' + opts . get_add_permission () ):
162
+ if not self . has_add_permission ( request ):
141
163
raise PermissionDenied
142
164
143
165
if post_url is None :
144
- if request . user . has_perm ( app_label + '.' + opts . get_change_permission () ):
166
+ if self . has_change_permission ( request , None ):
145
167
# redirect to list view
146
168
post_url = '../'
147
169
else :
@@ -211,7 +233,7 @@ def change_view(self, request, object_id):
211
233
opts = model ._meta
212
234
app_label = opts .app_label
213
235
214
- if not request . user . has_perm ( app_label + '.' + opts . get_change_permission () ):
236
+ if not self . has_change_permission ( request , object_id ):
215
237
raise PermissionDenied
216
238
217
239
if request .POST and request .POST .has_key ("_saveasnew" ):
@@ -305,7 +327,7 @@ def change_list_view(self, request):
305
327
"The 'change list' admin view for this model."
306
328
opts = self .model ._meta
307
329
app_label = opts .app_label
308
- if not request . user . has_perm ( app_label + '.' + opts . get_change_permission () ):
330
+ if not self . has_change_permission ( request , None ):
309
331
raise PermissionDenied
310
332
try :
311
333
cl = ChangeList (request , self .model )
@@ -332,7 +354,7 @@ def delete_view(self, request, object_id):
332
354
"The 'delete' admin view for this model."
333
355
opts = self .model ._meta
334
356
app_label = opts .app_label
335
- if not request . user . has_perm ( app_label + '.' + opts . get_delete_permission () ):
357
+ if not self . has_delete_permission ( request , object_id ):
336
358
raise PermissionDenied
337
359
obj = get_object_or_404 (self .model , pk = object_id )
338
360
0 commit comments