Ticket #13223: 13223.newview-prototype.diff
File 13223.newview-prototype.diff, 8.9 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 f5f6256..1a0ec54 100644
a b from django import forms 3 3 from django.conf import settings 4 4 from django.forms.formsets import all_valid 5 5 from django.forms.models import (modelform_factory, modelformset_factory, 6 inlineformset_factory, BaseInlineFormSet )6 inlineformset_factory, BaseInlineFormSet, model_to_dict) 7 7 from django.contrib.contenttypes.models import ContentType 8 8 from django.contrib.admin import widgets, helpers 9 9 from django.contrib.admin.util import unquote, flatten_fieldsets, get_deleted_objects, model_format_dict … … class ModelAdmin(BaseModelAdmin): 383 383 url(r'^(.+)/delete/$', 384 384 wrap(self.delete_view), 385 385 name='%s_%s_delete' % info), 386 url(r'^(.+)/clone/$', 387 wrap(self.clone_view), 388 name='%s_%s_clone' % info), 386 389 url(r'^(.+)/$', 387 390 wrap(self.change_view), 388 391 name='%s_%s_change' % info), … … class ModelAdmin(BaseModelAdmin): 950 953 prefix = "%s-%s" % (prefix, prefixes[prefix]) 951 954 formset = FormSet(data=request.POST, files=request.FILES, 952 955 instance=new_object, 953 save_as_new="_saveasnew" in request.POST,954 956 prefix=prefix, queryset=inline.queryset(request)) 955 957 formsets.append(formset) 956 958 if all_valid(formsets) and form_validated: … … class ModelAdmin(BaseModelAdmin): 1009 1011 context.update(extra_context or {}) 1010 1012 return self.render_change_form(request, context, form_url=form_url, add=True) 1011 1013 1014 1015 @csrf_protect_m 1016 @transaction.commit_on_success 1017 def clone_view(self, request, object_id, form_url='', extra_context=None): 1018 model = self.model 1019 opts = model._meta 1020 1021 if not self.has_add_permission(request): 1022 raise PermissionDenied 1023 1024 original_obj = self.get_object(request, unquote(object_id)) 1025 1026 ModelForm = self.get_form(request) 1027 formsets = [] 1028 inline_instances = self.get_inline_instances(request) 1029 if request.method == 'POST': 1030 form = ModelForm(request.POST, request.FILES) 1031 if form.is_valid(): 1032 new_object = self.save_form(request, form, change=False) 1033 form_validated = True 1034 else: 1035 form_validated = False 1036 new_object = self.model() 1037 prefixes = {} 1038 for FormSet, inline in zip(self.get_formsets(request), inline_instances): 1039 prefix = FormSet.get_default_prefix() 1040 prefixes[prefix] = prefixes.get(prefix, 0) + 1 1041 if prefixes[prefix] != 1 or not prefix: 1042 prefix = "%s-%s" % (prefix, prefixes[prefix]) 1043 formset = FormSet(data=request.POST, files=request.FILES, 1044 instance=new_object, 1045 prefix=prefix, queryset=inline.queryset(request)) 1046 formsets.append(formset) 1047 if all_valid(formsets) and form_validated: 1048 self.save_model(request, new_object, form, False) 1049 self.save_related(request, form, formsets, False) 1050 self.log_addition(request, new_object) 1051 return self.response_add(request, new_object) 1052 else: 1053 initial = model_to_dict(original_obj) 1054 form = ModelForm(initial=initial) 1055 prefixes = {} 1056 for FormSet, inline in zip(self.get_formsets(request, original_obj), inline_instances): 1057 prefix = FormSet.get_default_prefix() 1058 prefixes[prefix] = prefixes.get(prefix, 0) + 1 1059 if prefixes[prefix] != 1 or not prefix: 1060 prefix = "%s-%s" % (prefix, prefixes[prefix]) 1061 formset = FormSet(instance=original_obj, 1062 prefix=prefix, 1063 save_as_new=True, 1064 queryset=inline.queryset(request)) 1065 formsets.append(formset) 1066 1067 admin_form = helpers.AdminForm(form, list(self.get_fieldsets(request)), 1068 self.get_prepopulated_fields(request), 1069 self.get_readonly_fields(request), 1070 model_admin=self) 1071 media = self.media + admin_form.media 1072 1073 inline_admin_formsets = [] 1074 for inline, formset in zip(inline_instances, formsets): 1075 fieldsets = list(inline.get_fieldsets(request, original_obj)) 1076 readonly = list(inline.get_readonly_fields(request, original_obj)) 1077 prepopulated = dict(inline.get_prepopulated_fields(request, original_obj)) 1078 inline_admin_formset = helpers.InlineAdminFormSet(inline, formset, 1079 fieldsets, prepopulated, readonly, model_admin=self) 1080 inline_admin_formsets.append(inline_admin_formset) 1081 media = media + inline_admin_formset.media 1082 1083 context = { 1084 'title': _('Add %s') % force_unicode(opts.verbose_name), 1085 'adminform': admin_form, 1086 'is_popup': "_popup" in request.REQUEST, 1087 'show_delete': False, 1088 'media': media, 1089 'inline_admin_formsets': inline_admin_formsets, 1090 'errors': helpers.AdminErrorList(form, formsets), 1091 'app_label': opts.app_label, 1092 } 1093 context.update(extra_context or {}) 1094 return self.render_change_form(request, context, form_url=form_url, add=True) 1095 1012 1096 @csrf_protect_m 1013 1097 @transaction.commit_on_success 1014 1098 def change_view(self, request, object_id, form_url='', extra_context=None): -
django/contrib/admin/templates/admin/submit_line.html
diff --git a/django/contrib/admin/templates/admin/submit_line.html b/django/contrib/admin/templates/admin/submit_line.html index d6f854a..78bc255 100644
a b 2 2 <div class="submit-row"> 3 3 {% if show_save %}<input type="submit" value="{% trans 'Save' %}" class="default" name="_save" {{ onclick_attrib }}/>{% endif %} 4 4 {% if show_delete_link %}<p class="deletelink-box"><a href="delete/" class="deletelink">{% trans "Delete" %}</a></p>{% endif %} 5 {% if show_save_as_new %}< input type="submit" value="{% trans 'Save as new' %}" name="_saveasnew" {{ onclick_attrib }}/>{%endif%}5 {% if show_save_as_new %}<p><a href="clone/">"{% trans 'Save as new' %}"</a></p>{%endif%} 6 6 {% if show_save_and_add_another %}<input type="submit" value="{% trans 'Save and add another' %}" name="_addanother" {{ onclick_attrib }} />{% endif %} 7 7 {% if show_save_and_continue %}<input type="submit" value="{% trans 'Save and continue editing' %}" name="_continue" {{ onclick_attrib }}/>{% endif %} 8 8 </div> -
django/forms/models.py
diff --git a/django/forms/models.py b/django/forms/models.py index cd8f027..6961eed 100644
a b class BaseInlineFormSet(BaseModelFormSet): 682 682 """A formset for child objects related to a parent.""" 683 683 def __init__(self, data=None, files=None, instance=None, 684 684 save_as_new=False, prefix=None, queryset=None, **kwargs): 685 685 686 from django.db.models.fields.related import RelatedObject 686 687 if instance is None: 687 688 self.instance = self.fk.rel.to() … … class BaseInlineFormSet(BaseModelFormSet): 693 694 if queryset is None: 694 695 queryset = self.model._default_manager 695 696 qs = queryset.filter(**{self.fk.name: self.instance}) 697 698 initial = [] 699 if save_as_new: 700 for obj in qs: 701 initial.append(model_to_dict(obj, exclude=[self.fk.name, 702 self.instance._meta.pk.name])) 703 # make sure original instance won't be reused 704 self.instance = self.fk.rel.to() 705 696 706 super(BaseInlineFormSet, self).__init__(data, files, prefix=prefix, 697 queryset=qs, **kwargs)707 queryset=qs, initial=initial, **kwargs) 698 708 699 709 def initial_form_count(self): 700 710 if self.save_as_new: 701 711 return 0 702 712 return super(BaseInlineFormSet, self).initial_form_count() 703 713 704 705 714 def _construct_form(self, i, **kwargs): 706 715 form = super(BaseInlineFormSet, self)._construct_form(i, **kwargs) 707 if self.save_as_new:708 # Remove the primary key from the form's data, we are only709 # creating new instances710 form.data[form.add_prefix(self._pk_field.name)] = None711 712 # Remove the foreign key from the form's data713 form.data[form.add_prefix(self.fk.name)] = None714 715 716 # Set the fk value here so that the form can do it's validation. 716 717 setattr(form.instance, self.fk.get_attname(), self.instance.pk) 717 718 return form