Ticket #8060: admin_inline_permissions_v1.diff
File admin_inline_permissions_v1.diff, 6.7 KB (added by , 13 years ago) |
---|
-
django/contrib/admin/options.py
diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py index 5271e02..f2cf662 100644
a b class ModelAdmin(BaseModelAdmin): 306 306 self.model = model 307 307 self.opts = model._meta 308 308 self.admin_site = admin_site 309 self.inline_instances = []310 for inline_class in self.inlines:311 inline_instance = inline_class(self.model, self.admin_site)312 self.inline_instances.append(inline_instance)313 309 if 'action_checkbox' not in self.list_display and self.actions is not None: 314 310 self.list_display = ['action_checkbox'] + list(self.list_display) 315 311 if not self.list_display_links: … … class ModelAdmin(BaseModelAdmin): 319 315 break 320 316 super(ModelAdmin, self).__init__() 321 317 318 def get_inline_instances(self, user=None, action=None): 319 inline_instances = [] 320 for inline_class in self.inlines: 321 inline = inline_class(self.model, self.admin_site) 322 if user: 323 if inline.opts.auto_created: 324 # The model was auto-created as intermediary for a 325 # ManyToMany-relationship, find out the destination model 326 for field in inline.opts.fields: 327 if isinstance(field, models.ForeignKey) and field.rel.to != inline.opts.auto_created: 328 perm_opts = field.rel.to._meta 329 else: 330 perm_opts = inline.opts 331 perm_string = perm_opts.app_label + '.' 332 if action == 'change': 333 # Currently, we can't make sure the user can only edit 334 # existing inlines or only add new ones but not edit 335 # existing ones. So we only allow editing if the user 336 # has both permissions. 337 if not user.has_perm(perm_string + perm_opts.get_change_permission()): 338 continue 339 if not user.has_perm(perm_string + perm_opts.get_add_permission()): 340 continue 341 if not user.has_perm(perm_string + perm_opts.get_delete_permission()): 342 inline.can_delete = False 343 inline_instances.append(inline) 344 345 return inline_instances 346 347 @property 348 def inline_instances(self): 349 return self.get_inline_instances() 350 322 351 def get_urls(self): 323 352 from django.conf.urls import patterns, url 324 353 … … class ModelAdmin(BaseModelAdmin): 499 528 fields=self.list_editable, **defaults) 500 529 501 530 def get_formsets(self, request, obj=None): 502 for inline in self.inline_instances: 531 action = ('change' if obj else 'add') 532 for inline in self.get_inline_instances(request.user, action): 503 533 yield inline.get_formset(request, obj) 504 534 505 535 def get_paginator(self, request, queryset, per_page, orphans=0, allow_empty_first_page=True): … … class ModelAdmin(BaseModelAdmin): 898 928 899 929 ModelForm = self.get_form(request) 900 930 formsets = [] 931 inline_instances = self.get_inline_instances(request.user, 'add') 901 932 if request.method == 'POST': 902 933 form = ModelForm(request.POST, request.FILES) 903 934 if form.is_valid(): … … class ModelAdmin(BaseModelAdmin): 907 938 form_validated = False 908 939 new_object = self.model() 909 940 prefixes = {} 910 for FormSet, inline in zip(self.get_formsets(request), self.inline_instances):941 for FormSet, inline in zip(self.get_formsets(request), inline_instances): 911 942 prefix = FormSet.get_default_prefix() 912 943 prefixes[prefix] = prefixes.get(prefix, 0) + 1 913 944 if prefixes[prefix] != 1: … … class ModelAdmin(BaseModelAdmin): 935 966 initial[k] = initial[k].split(",") 936 967 form = ModelForm(initial=initial) 937 968 prefixes = {} 938 for FormSet, inline in zip(self.get_formsets(request), 939 self.inline_instances): 969 for FormSet, inline in zip(self.get_formsets(request), inline_instances): 940 970 prefix = FormSet.get_default_prefix() 941 971 prefixes[prefix] = prefixes.get(prefix, 0) + 1 942 972 if prefixes[prefix] != 1: … … class ModelAdmin(BaseModelAdmin): 952 982 media = self.media + adminForm.media 953 983 954 984 inline_admin_formsets = [] 955 for inline, formset in zip( self.inline_instances, formsets):985 for inline, formset in zip(inline_instances, formsets): 956 986 fieldsets = list(inline.get_fieldsets(request)) 957 987 readonly = list(inline.get_readonly_fields(request)) 958 988 prepopulated = dict(inline.get_prepopulated_fields(request)) … … class ModelAdmin(BaseModelAdmin): 994 1024 995 1025 ModelForm = self.get_form(request, obj) 996 1026 formsets = [] 1027 inline_instances = self.get_inline_instances(request.user, 'change') 997 1028 if request.method == 'POST': 998 1029 form = ModelForm(request.POST, request.FILES, instance=obj) 999 1030 if form.is_valid(): … … class ModelAdmin(BaseModelAdmin): 1003 1034 form_validated = False 1004 1035 new_object = obj 1005 1036 prefixes = {} 1006 for FormSet, inline in zip(self.get_formsets(request, new_object), 1007 self.inline_instances): 1037 for FormSet, inline in zip(self.get_formsets(request, new_object), inline_instances): 1008 1038 prefix = FormSet.get_default_prefix() 1009 1039 prefixes[prefix] = prefixes.get(prefix, 0) + 1 1010 1040 if prefixes[prefix] != 1: … … class ModelAdmin(BaseModelAdmin): 1025 1055 else: 1026 1056 form = ModelForm(instance=obj) 1027 1057 prefixes = {} 1028 for FormSet, inline in zip(self.get_formsets(request, obj), self.inline_instances):1058 for FormSet, inline in zip(self.get_formsets(request, obj), inline_instances): 1029 1059 prefix = FormSet.get_default_prefix() 1030 1060 prefixes[prefix] = prefixes.get(prefix, 0) + 1 1031 1061 if prefixes[prefix] != 1: … … class ModelAdmin(BaseModelAdmin): 1041 1071 media = self.media + adminForm.media 1042 1072 1043 1073 inline_admin_formsets = [] 1044 for inline, formset in zip( self.inline_instances, formsets):1074 for inline, formset in zip(inline_instances, formsets): 1045 1075 fieldsets = list(inline.get_fieldsets(request, obj)) 1046 1076 readonly = list(inline.get_readonly_fields(request, obj)) 1047 1077 prepopulated = dict(inline.get_prepopulated_fields(request, obj))