Ticket #8005: 8005.2.diff

File 8005.2.diff, 3.0 KB (added by simon, 11 years ago)

Improved patch; refactored formset boilerplate in to a separate method

  • django/contrib/admin/options.py

     
    324324    def get_formsets(self, request, obj=None):
    325325        for inline in self.inline_instances:
    326326            yield inline.get_formset(request, obj)
    327 
     327   
     328    def save_formset_add(self, request, formset, new_object):
     329        # HACK: it seems like the parent obejct should be passed into
     330        # a method of something, not just set as an attribute
     331        formset.instance = new_object
     332        formset.save()
     333   
     334    def save_model_add(self, request, form, formsets):
     335        return form.save(commit=True)
     336   
    328337    def save_add(self, request, form, formsets, post_url_continue):
    329338        """
    330339        Saves the object in the "add" stage and returns an HttpResponseRedirect.
     
    333342        """
    334343        from django.contrib.admin.models import LogEntry, ADDITION
    335344        opts = self.model._meta
    336         new_object = form.save(commit=True)
    337 
     345       
     346        new_object = self.save_model_add(request, form, formsets)
     347       
    338348        if formsets:
    339349            for formset in formsets:
    340                 # HACK: it seems like the parent obejct should be passed into
    341                 # a method of something, not just set as an attribute
    342                 formset.instance = new_object
    343                 formset.save()
    344 
     350                self.save_formset_add(request, formset, new_object)
     351       
    345352        pk_value = new_object._get_pk_val()
    346353        LogEntry.objects.log_action(request.user.id, ContentType.objects.get_for_model(self.model).id, pk_value, force_unicode(new_object), ADDITION)
    347354        msg = _('The %(name)s "%(obj)s" was added successfully.') % {'name': opts.verbose_name, 'obj': new_object}
     
    371378                post_url = '../../../'
    372379            return HttpResponseRedirect(post_url)
    373380    save_add = transaction.commit_on_success(save_add)
    374 
     381   
     382    def save_formset_change(self, request, formset, new_object):
     383        formset.save()
     384   
     385    def save_model_change(self, request, form, formsets):
     386        return form.save(commit=True)
     387   
    375388    def save_change(self, request, form, formsets=None):
    376389        """
    377390        Saves the object in the "change" stage and returns an HttpResponseRedirect.
     
    382395        """
    383396        from django.contrib.admin.models import LogEntry, CHANGE
    384397        opts = self.model._meta
    385         new_object = form.save(commit=True)
    386         pk_value = new_object._get_pk_val()
    387 
     398       
     399        new_object = self.save_model_change(request, form, formsets)
     400       
    388401        if formsets:
    389402            for formset in formsets:
    390                 formset.save()
    391 
     403                self.save_formset_change(self, request, formset, new_object)
     404       
     405        pk_value = new_object._get_pk_val()
     406       
    392407        # Construct the change message.
    393408        change_message = []
    394409        if form.changed_data:
Back to Top