Code

Ticket #8001: 8001_18072_18310.6.diff

File 8001_18072_18310.6.diff, 9.9 KB (added by ramiro, 21 months ago)

Updated patch

Line 
1diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
2index 081d001..79f82d4 100644
3--- a/django/contrib/admin/options.py
4+++ b/django/contrib/admin/options.py
5@@ -762,21 +762,32 @@ class ModelAdmin(BaseModelAdmin):
6             "admin/change_form.html"
7         ], context, current_app=self.admin_site.name)
8 
9-    def response_add(self, request, obj, post_url_continue='../%s/'):
10+    def response_add(self, request, obj, continue_url=None, add_url=None, hasperm_url=None, noperm_url=None):
11         """
12         Determines the HttpResponse for the add_view stage.
13         """
14         opts = obj._meta
15         pk_value = obj._get_pk_val()
16+        app_label = opts.app_label
17+        model_name = opts.module_name
18+        site_name = self.admin_site.name
19+
20+        msg_dict = {'name': force_text(opts.verbose_name), 'obj': force_text(obj)}
21 
22-        msg = _('The %(name)s "%(obj)s" was added successfully.') % {'name': force_text(opts.verbose_name), 'obj': force_text(obj)}
23         # Here, we distinguish between different save types by checking for
24         # the presence of keys in request.POST.
25         if "_continue" in request.POST:
26-            self.message_user(request, msg + ' ' + _("You may edit it again below."))
27+            msg = _('The %(name)s "%(obj)s" was added successfully. You may edit it again below.') % msg_dict
28+            self.message_user(request, msg)
29+            if continue_url is None:
30+                continue_url = reverse('admin:%s_%s_change' % (
31+                        app_label, model_name
32+                    ),
33+                    args=(pk_value,),
34+                    current_app=site_name)
35             if "_popup" in request.POST:
36-                post_url_continue += "?_popup=1"
37-            return HttpResponseRedirect(post_url_continue % pk_value)
38+                continue_url += "?_popup=1"
39+            return HttpResponseRedirect(continue_url)
40 
41         if "_popup" in request.POST:
42             return HttpResponse(
43@@ -785,72 +796,97 @@ class ModelAdmin(BaseModelAdmin):
44                 # escape() calls force_text.
45                 (escape(pk_value), escapejs(obj)))
46         elif "_addanother" in request.POST:
47-            self.message_user(request, msg + ' ' + (_("You may add another %s below.") % force_text(opts.verbose_name)))
48-            return HttpResponseRedirect(request.path)
49+            msg = _('The %(name)s "%(obj)s" was added successfully. You may add another %(name)s below.') % msg_dict
50+            self.message_user(request, msg)
51+            if add_url is None:
52+                add_url = reverse('admin:%s_%s_add' %
53+                    (app_label, model_name),
54+                    current_app=site_name)
55+            return HttpResponseRedirect(add_url)
56         else:
57+            msg = _('The %(name)s "%(obj)s" was added successfully.') % msg_dict
58             self.message_user(request, msg)
59 
60             # Figure out where to redirect. If the user has change permission,
61             # redirect to the change-list page for this object. Otherwise,
62             # redirect to the admin index.
63             if self.has_change_permission(request, None):
64-                post_url = reverse('admin:%s_%s_changelist' %
65-                                   (opts.app_label, opts.module_name),
66-                                   current_app=self.admin_site.name)
67+                if hasperm_url is None:
68+                    url = reverse('admin:%s_%s_changelist' %
69+                        (app_label, model_name),
70+                        current_app=site_name)
71+                else:
72+                    url = hasperm_url
73             else:
74-                post_url = reverse('admin:index',
75-                                   current_app=self.admin_site.name)
76-            return HttpResponseRedirect(post_url)
77+                if noperm_url is None:
78+                    url = reverse('admin:index', current_app=site_name)
79+                else:
80+                    url = noperm_url
81+            return HttpResponseRedirect(url)
82 
83-    def response_change(self, request, obj):
84+    def response_change(self, request, obj, continue_url=None, add_url=None, hasperm_url=None, noperm_url=None):
85         """
86         Determines the HttpResponse for the change_view stage.
87         """
88         opts = obj._meta
89 
90+        app_label = opts.app_label
91+        model_name = opts.module_name
92+        site_name = self.admin_site.name
93+        verbose_name = opts.verbose_name
94         # Handle proxy models automatically created by .only() or .defer().
95         # Refs #14529
96-        verbose_name = opts.verbose_name
97-        module_name = opts.module_name
98         if obj._deferred:
99             opts_ = opts.proxy_for_model._meta
100             verbose_name = opts_.verbose_name
101-            module_name = opts_.module_name
102+            model_name = opts_.module_name
103 
104+        msg_dict = {'name': force_text(verbose_name), 'obj': force_text(obj)}
105         pk_value = obj._get_pk_val()
106 
107-        msg = _('The %(name)s "%(obj)s" was changed successfully.') % {'name': force_text(verbose_name), 'obj': force_text(obj)}
108+        if "_continue" in request.POST or "_saveasnew" in request.POST:
109+            if continue_url is None:
110+                continue_url = reverse('admin:%s_%s_change' %
111+                    (app_label, model_name),
112+                    args=(pk_value,),
113+                    current_app=site_name)
114         if "_continue" in request.POST:
115-            self.message_user(request, msg + ' ' + _("You may edit it again below."))
116-            if "_popup" in request.REQUEST:
117-                return HttpResponseRedirect(request.path + "?_popup=1")
118-            else:
119-                return HttpResponseRedirect(request.path)
120+            msg = _('The %(name)s "%(obj)s" was changed successfully. You may edit it again below.') % msg_dict
121+            self.message_user(request, msg)
122+            if "_popup" in request.POST:
123+                continue_url += "?_popup=1"
124+            return HttpResponseRedirect(continue_url)
125         elif "_saveasnew" in request.POST:
126-            msg = _('The %(name)s "%(obj)s" was added successfully. You may edit it again below.') % {'name': force_text(verbose_name), 'obj': obj}
127+            msg = _('The %(name)s "%(obj)s" was added successfully. You may edit it again below.') % msg_dict
128             self.message_user(request, msg)
129-            return HttpResponseRedirect(reverse('admin:%s_%s_change' %
130-                                        (opts.app_label, module_name),
131-                                        args=(pk_value,),
132-                                        current_app=self.admin_site.name))
133+            return HttpResponseRedirect(continue_url)
134         elif "_addanother" in request.POST:
135-            self.message_user(request, msg + ' ' + (_("You may add another %s below.") % force_text(verbose_name)))
136-            return HttpResponseRedirect(reverse('admin:%s_%s_add' %
137-                                        (opts.app_label, module_name),
138-                                        current_app=self.admin_site.name))
139+            msg = _('The %(name)s "%(obj)s" was changed successfully. You may add another %(name)s below.') % msg_dict
140+            self.message_user(request, msg)
141+            if add_url is None:
142+                add_url = reverse('admin:%s_%s_add' %
143+                                       (app_label, model_name),
144+                                       current_app=site_name)
145+            return HttpResponseRedirect(add_url)
146         else:
147+            msg = _('The %(name)s "%(obj)s" was changed successfully.') % msg_dict
148             self.message_user(request, msg)
149             # Figure out where to redirect. If the user has change permission,
150             # redirect to the change-list page for this object. Otherwise,
151             # redirect to the admin index.
152             if self.has_change_permission(request, None):
153-                post_url = reverse('admin:%s_%s_changelist' %
154-                                   (opts.app_label, module_name),
155-                                   current_app=self.admin_site.name)
156+                if hasperm_url is None:
157+                    url = reverse('admin:%s_%s_changelist' %
158+                                   (app_label, model_name),
159+                                   current_app=site_name)
160+                else:
161+                    url = hasperm_url
162             else:
163-                post_url = reverse('admin:index',
164-                                   current_app=self.admin_site.name)
165-            return HttpResponseRedirect(post_url)
166+                if noperm_url is None:
167+                    url = reverse('admin:index', current_app=site_name)
168+                else:
169+                    url = noperm_url
170+            return HttpResponseRedirect(url)
171 
172     def response_action(self, request, queryset):
173         """
174diff --git a/django/contrib/auth/admin.py b/django/contrib/auth/admin.py
175index ccf940d..52f8083 100644
176--- a/django/contrib/auth/admin.py
177+++ b/django/contrib/auth/admin.py
178@@ -153,7 +153,7 @@ class UserAdmin(admin.ModelAdmin):
179             'admin/auth/user/change_password.html'
180         ], context, current_app=self.admin_site.name)
181 
182-    def response_add(self, request, obj, post_url_continue='../%s/'):
183+    def response_add(self, request, obj, continue_url=None, add_url=None, hasperm_url=None, noperm_url=None):
184         """
185         Determines the HttpResponse for the add_view stage. It mostly defers to
186         its superclass implementation but is customized because the User model
187@@ -166,8 +166,7 @@ class UserAdmin(admin.ModelAdmin):
188         # * We are adding a user in a popup
189         if '_addanother' not in request.POST and '_popup' not in request.POST:
190             request.POST['_continue'] = 1
191-        return super(UserAdmin, self).response_add(request, obj,
192-                                                   post_url_continue)
193+        return super(UserAdmin, self).response_add(request, obj, continue_url, add_url, hasperm_url, noperm_url)
194 
195 admin.site.register(Group, GroupAdmin)
196 admin.site.register(User, UserAdmin)