Ticket #8001: 8001_18072_18310.6.diff

File 8001_18072_18310.6.diff, 9.9 KB (added by ramiro, 3 years ago)

Updated patch

  • django/contrib/admin/options.py

    diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
    index 081d001..79f82d4 100644
    a b class ModelAdmin(BaseModelAdmin): 
    762762            "admin/change_form.html"
    763763        ], context, current_app=self.admin_site.name)
    764764
    765     def response_add(self, request, obj, post_url_continue='../%s/'):
     765    def response_add(self, request, obj, continue_url=None, add_url=None, hasperm_url=None, noperm_url=None):
    766766        """
    767767        Determines the HttpResponse for the add_view stage.
    768768        """
    769769        opts = obj._meta
    770770        pk_value = obj._get_pk_val()
     771        app_label = opts.app_label
     772        model_name = opts.module_name
     773        site_name = self.admin_site.name
     774
     775        msg_dict = {'name': force_text(opts.verbose_name), 'obj': force_text(obj)}
    771776
    772         msg = _('The %(name)s "%(obj)s" was added successfully.') % {'name': force_text(opts.verbose_name), 'obj': force_text(obj)}
    773777        # Here, we distinguish between different save types by checking for
    774778        # the presence of keys in request.POST.
    775779        if "_continue" in request.POST:
    776             self.message_user(request, msg + ' ' + _("You may edit it again below."))
     780            msg = _('The %(name)s "%(obj)s" was added successfully. You may edit it again below.') % msg_dict
     781            self.message_user(request, msg)
     782            if continue_url is None:
     783                continue_url = reverse('admin:%s_%s_change' % (
     784                        app_label, model_name
     785                    ),
     786                    args=(pk_value,),
     787                    current_app=site_name)
    777788            if "_popup" in request.POST:
    778                 post_url_continue += "?_popup=1"
    779             return HttpResponseRedirect(post_url_continue % pk_value)
     789                continue_url += "?_popup=1"
     790            return HttpResponseRedirect(continue_url)
    780791
    781792        if "_popup" in request.POST:
    782793            return HttpResponse(
    class ModelAdmin(BaseModelAdmin): 
    785796                # escape() calls force_text.
    786797                (escape(pk_value), escapejs(obj)))
    787798        elif "_addanother" in request.POST:
    788             self.message_user(request, msg + ' ' + (_("You may add another %s below.") % force_text(opts.verbose_name)))
    789             return HttpResponseRedirect(request.path)
     799            msg = _('The %(name)s "%(obj)s" was added successfully. You may add another %(name)s below.') % msg_dict
     800            self.message_user(request, msg)
     801            if add_url is None:
     802                add_url = reverse('admin:%s_%s_add' %
     803                    (app_label, model_name),
     804                    current_app=site_name)
     805            return HttpResponseRedirect(add_url)
    790806        else:
     807            msg = _('The %(name)s "%(obj)s" was added successfully.') % msg_dict
    791808            self.message_user(request, msg)
    792809
    793810            # Figure out where to redirect. If the user has change permission,
    794811            # redirect to the change-list page for this object. Otherwise,
    795812            # redirect to the admin index.
    796813            if self.has_change_permission(request, None):
    797                 post_url = reverse('admin:%s_%s_changelist' %
    798                                    (opts.app_label, opts.module_name),
    799                                    current_app=self.admin_site.name)
     814                if hasperm_url is None:
     815                    url = reverse('admin:%s_%s_changelist' %
     816                        (app_label, model_name),
     817                        current_app=site_name)
     818                else:
     819                    url = hasperm_url
    800820            else:
    801                 post_url = reverse('admin:index',
    802                                    current_app=self.admin_site.name)
    803             return HttpResponseRedirect(post_url)
     821                if noperm_url is None:
     822                    url = reverse('admin:index', current_app=site_name)
     823                else:
     824                    url = noperm_url
     825            return HttpResponseRedirect(url)
    804826
    805     def response_change(self, request, obj):
     827    def response_change(self, request, obj, continue_url=None, add_url=None, hasperm_url=None, noperm_url=None):
    806828        """
    807829        Determines the HttpResponse for the change_view stage.
    808830        """
    809831        opts = obj._meta
    810832
     833        app_label = opts.app_label
     834        model_name = opts.module_name
     835        site_name = self.admin_site.name
     836        verbose_name = opts.verbose_name
    811837        # Handle proxy models automatically created by .only() or .defer().
    812838        # Refs #14529
    813         verbose_name = opts.verbose_name
    814         module_name = opts.module_name
    815839        if obj._deferred:
    816840            opts_ = opts.proxy_for_model._meta
    817841            verbose_name = opts_.verbose_name
    818             module_name = opts_.module_name
     842            model_name = opts_.module_name
    819843
     844        msg_dict = {'name': force_text(verbose_name), 'obj': force_text(obj)}
    820845        pk_value = obj._get_pk_val()
    821846
    822         msg = _('The %(name)s "%(obj)s" was changed successfully.') % {'name': force_text(verbose_name), 'obj': force_text(obj)}
     847        if "_continue" in request.POST or "_saveasnew" in request.POST:
     848            if continue_url is None:
     849                continue_url = reverse('admin:%s_%s_change' %
     850                    (app_label, model_name),
     851                    args=(pk_value,),
     852                    current_app=site_name)
    823853        if "_continue" in request.POST:
    824             self.message_user(request, msg + ' ' + _("You may edit it again below."))
    825             if "_popup" in request.REQUEST:
    826                 return HttpResponseRedirect(request.path + "?_popup=1")
    827             else:
    828                 return HttpResponseRedirect(request.path)
     854            msg = _('The %(name)s "%(obj)s" was changed successfully. You may edit it again below.') % msg_dict
     855            self.message_user(request, msg)
     856            if "_popup" in request.POST:
     857                continue_url += "?_popup=1"
     858            return HttpResponseRedirect(continue_url)
    829859        elif "_saveasnew" in request.POST:
    830             msg = _('The %(name)s "%(obj)s" was added successfully. You may edit it again below.') % {'name': force_text(verbose_name), 'obj': obj}
     860            msg = _('The %(name)s "%(obj)s" was added successfully. You may edit it again below.') % msg_dict
    831861            self.message_user(request, msg)
    832             return HttpResponseRedirect(reverse('admin:%s_%s_change' %
    833                                         (opts.app_label, module_name),
    834                                         args=(pk_value,),
    835                                         current_app=self.admin_site.name))
     862            return HttpResponseRedirect(continue_url)
    836863        elif "_addanother" in request.POST:
    837             self.message_user(request, msg + ' ' + (_("You may add another %s below.") % force_text(verbose_name)))
    838             return HttpResponseRedirect(reverse('admin:%s_%s_add' %
    839                                         (opts.app_label, module_name),
    840                                         current_app=self.admin_site.name))
     864            msg = _('The %(name)s "%(obj)s" was changed successfully. You may add another %(name)s below.') % msg_dict
     865            self.message_user(request, msg)
     866            if add_url is None:
     867                add_url = reverse('admin:%s_%s_add' %
     868                                       (app_label, model_name),
     869                                       current_app=site_name)
     870            return HttpResponseRedirect(add_url)
    841871        else:
     872            msg = _('The %(name)s "%(obj)s" was changed successfully.') % msg_dict
    842873            self.message_user(request, msg)
    843874            # Figure out where to redirect. If the user has change permission,
    844875            # redirect to the change-list page for this object. Otherwise,
    845876            # redirect to the admin index.
    846877            if self.has_change_permission(request, None):
    847                 post_url = reverse('admin:%s_%s_changelist' %
    848                                    (opts.app_label, module_name),
    849                                    current_app=self.admin_site.name)
     878                if hasperm_url is None:
     879                    url = reverse('admin:%s_%s_changelist' %
     880                                   (app_label, model_name),
     881                                   current_app=site_name)
     882                else:
     883                    url = hasperm_url
    850884            else:
    851                 post_url = reverse('admin:index',
    852                                    current_app=self.admin_site.name)
    853             return HttpResponseRedirect(post_url)
     885                if noperm_url is None:
     886                    url = reverse('admin:index', current_app=site_name)
     887                else:
     888                    url = noperm_url
     889            return HttpResponseRedirect(url)
    854890
    855891    def response_action(self, request, queryset):
    856892        """
  • django/contrib/auth/admin.py

    diff --git a/django/contrib/auth/admin.py b/django/contrib/auth/admin.py
    index ccf940d..52f8083 100644
    a b class UserAdmin(admin.ModelAdmin): 
    153153            'admin/auth/user/change_password.html'
    154154        ], context, current_app=self.admin_site.name)
    155155
    156     def response_add(self, request, obj, post_url_continue='../%s/'):
     156    def response_add(self, request, obj, continue_url=None, add_url=None, hasperm_url=None, noperm_url=None):
    157157        """
    158158        Determines the HttpResponse for the add_view stage. It mostly defers to
    159159        its superclass implementation but is customized because the User model
    class UserAdmin(admin.ModelAdmin): 
    166166        # * We are adding a user in a popup
    167167        if '_addanother' not in request.POST and '_popup' not in request.POST:
    168168            request.POST['_continue'] = 1
    169         return super(UserAdmin, self).response_add(request, obj,
    170                                                    post_url_continue)
     169        return super(UserAdmin, self).response_add(request, obj, continue_url, add_url, hasperm_url, noperm_url)
    171170
    172171admin.site.register(Group, GroupAdmin)
    173172admin.site.register(User, UserAdmin)
Back to Top