Ticket #6903: 6903-changelist-return-to.diff

File 6903-changelist-return-to.diff, 8.2 KB (added by Jacob, 16 years ago)

A few updates to ramiro's patch, and updated to r9888

  • django/contrib/admin/options.py

    diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
    index ed41c7f..bfd0f19 100644
    a b from django.http import Http404, HttpResponse, HttpResponseRedirect  
    1212from django.shortcuts import get_object_or_404, render_to_response
    1313from django.utils.functional import update_wrapper
    1414from django.utils.html import escape
     15from django.utils.http import urlquote
    1516from django.utils.safestring import mark_safe
    1617from django.utils.functional import curry
    1718from django.utils.text import capfirst, get_text_list
    HORIZONTAL, VERTICAL = 1, 2  
    2627# returns the <ul> class for a given radio_admin field
    2728get_ul_class = lambda x: 'radiolist%s' % ((x == HORIZONTAL) and ' inline' or '')
    2829
     30# GET parameter for the URL to return to after change/add views. This lets the
     31# admin save the state of the changelist page through the change/add page.
     32RETURN_GET_PARAM = 'return_to'
     33
    2934class IncorrectLookupParameters(Exception):
    3035    pass
    3136
    class ModelAdmin(BaseModelAdmin):  
    452457        pk_value = obj._get_pk_val()
    453458       
    454459        msg = _('The %(name)s "%(obj)s" was added successfully.') % {'name': force_unicode(opts.verbose_name), 'obj': force_unicode(obj)}
    455         # Here, we distinguish between different save types by checking for
     460       
     461        # Below, we distinguish between different save types by checking for
    456462        # the presence of keys in request.POST.
     463       
     464        # The user clicked "save and continue editing". Redirect to admin URL
     465        # for this object, possibly in popup mode if needed.
    457466        if request.POST.has_key("_continue"):
    458467            self.message_user(request, msg + ' ' + _("You may edit it again below."))
    459468            if request.POST.has_key("_popup"):
    460469                post_url_continue += "?_popup=1"
    461470            return HttpResponseRedirect(post_url_continue % pk_value)
    462471       
     472        # This was a popup and the user clicked the simple "save" button.
     473        # Return a little script which populates the calling page with the
     474        # saved key.
    463475        if request.POST.has_key("_popup"):
    464476            return HttpResponse('<script type="text/javascript">opener.dismissAddAnotherPopup(window, "%s", "%s");</script>' % \
    465477                # escape() calls force_unicode.
    466478                (escape(pk_value), escape(obj)))
     479       
     480        # The user clicked "save and add another", so redirect back to the bare
     481        # "add" page, which will be request.path.
    467482        elif request.POST.has_key("_addanother"):
    468483            self.message_user(request, msg + ' ' + (_("You may add another %s below.") % force_unicode(opts.verbose_name)))
    469484            return HttpResponseRedirect(request.path)
     485       
     486        # Just a regular "save" click. IF we've been pased an explicit return
     487        # URL in GET, go there. Otherwise, redirect to the plain changelist.
     488        # If the user doesn't have changelist permission, just redirect back
     489        # to the main admin index.
    470490        else:
    471491            self.message_user(request, msg)
    472            
    473             # Figure out where to redirect. If the user has change permission,
    474             # redirect to the change-list page for this object. Otherwise,
    475             # redirect to the admin index.
    476             if self.has_change_permission(request, None):
     492            if RETURN_GET_PARAM in request.GET:
     493                post_url = request.GET[RETURN_GET_PARAM]
     494            elif self.has_change_permission(request, None):
    477495                post_url = '../'
    478496            else:
    479497                post_url = '../../../'
     498
    480499            return HttpResponseRedirect(post_url)
    481500   
    482501    def response_change(self, request, obj):
    class ModelAdmin(BaseModelAdmin):  
    487506        pk_value = obj._get_pk_val()
    488507       
    489508        msg = _('The %(name)s "%(obj)s" was changed successfully.') % {'name': force_unicode(opts.verbose_name), 'obj': force_unicode(obj)}
     509       
     510        # Similar redirecting logic to response_add, above.
     511       
     512        # "Save and continue editing"
    490513        if request.POST.has_key("_continue"):
    491514            self.message_user(request, msg + ' ' + _("You may edit it again below."))
    492515            if request.REQUEST.has_key('_popup'):
    493516                return HttpResponseRedirect(request.path + "?_popup=1")
    494517            else:
    495518                return HttpResponseRedirect(request.path)
     519       
     520        # "Save as a new object"
    496521        elif request.POST.has_key("_saveasnew"):
    497522            msg = _('The %(name)s "%(obj)s" was added successfully. You may edit it again below.') % {'name': force_unicode(opts.verbose_name), 'obj': obj}
    498523            self.message_user(request, msg)
    499524            return HttpResponseRedirect("../%s/" % pk_value)
     525       
     526        # "Save and add another"
    500527        elif request.POST.has_key("_addanother"):
    501528            self.message_user(request, msg + ' ' + (_("You may add another %s below.") % force_unicode(opts.verbose_name)))
    502529            return HttpResponseRedirect("../add/")
     530           
     531        # "Save"
    503532        else:
    504533            self.message_user(request, msg)
    505             return HttpResponseRedirect("../")
    506    
     534            post_url = request.GET.get(RETURN_GET_PARAM, '../')
     535            return HttpResponseRedirect(post_url)
     536
    507537    def add_view(self, request, form_url='', extra_context=None):
    508         "The 'add' admin view for this model."
     538        """
     539        The 'add' admin view for this model.
     540        """
    509541        model = self.model
    510542        opts = model._meta
    511543       
    class ModelAdmin(BaseModelAdmin):  
    515547        ModelForm = self.get_form(request)
    516548        formsets = []
    517549        if request.method == 'POST':
     550            return_to = request.GET.get(RETURN_GET_PARAM, None)
     551
    518552            form = ModelForm(request.POST, request.FILES)
    519553            if form.is_valid():
    520554                form_validated = True
    class ModelAdmin(BaseModelAdmin):  
    550584            for FormSet in self.get_formsets(request):
    551585                formset = FormSet(instance=self.model())
    552586                formsets.append(formset)
    553        
     587
     588            return_to = request.META.get('HTTP_REFERER', None)
     589
     590        if return_to:
     591            form_url = form_url + '?%s=%s' % (RETURN_GET_PARAM, urlquote(return_to))
     592
    554593        adminForm = helpers.AdminForm(form, list(self.get_fieldsets(request)), self.prepopulated_fields)
    555594        media = self.media + adminForm.media
    556595       
    class ModelAdmin(BaseModelAdmin):  
    573612            'app_label': opts.app_label,
    574613        }
    575614        context.update(extra_context or {})
    576         return self.render_change_form(request, context, add=True)
     615        return self.render_change_form(request, context, form_url=form_url, add=True)
    577616    add_view = transaction.commit_on_success(add_view)
    578617   
    579618    def change_view(self, request, object_id, extra_context=None):
    class ModelAdmin(BaseModelAdmin):  
    601640        ModelForm = self.get_form(request, obj)
    602641        formsets = []
    603642        if request.method == 'POST':
     643            return_to = request.GET.get(RETURN_GET_PARAM, None)
     644
    604645            form = ModelForm(request.POST, request.FILES, instance=obj)
    605646            if form.is_valid():
    606647                form_validated = True
    class ModelAdmin(BaseModelAdmin):  
    628669            for FormSet in self.get_formsets(request, obj):
    629670                formset = FormSet(instance=obj)
    630671                formsets.append(formset)
    631        
     672
     673            return_to = request.META.get('HTTP_REFERER', None)
     674
     675        if return_to:
     676            form_url = '?%s=%s' % (RETURN_GET_PARAM, urlquote(return_to))
     677        else:
     678            form_url = ''
     679
    632680        adminForm = helpers.AdminForm(form, self.get_fieldsets(request, obj), self.prepopulated_fields)
    633681        media = self.media + adminForm.media
    634682       
    class ModelAdmin(BaseModelAdmin):  
    652700            'app_label': opts.app_label,
    653701        }
    654702        context.update(extra_context or {})
    655         return self.render_change_form(request, context, change=True, obj=obj)
     703        return self.render_change_form(request, context, change=True, form_url=form_url, obj=obj)
    656704    change_view = transaction.commit_on_success(change_view)
    657705   
    658706    def changelist_view(self, request, extra_context=None):
Back to Top