Code

Ticket #6903: 6903-r9781.diff

File 6903-r9781.diff, 4.5 KB (added by ramiro, 5 years ago)

Patch updated to r9781

Line 
1diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
2--- a/django/contrib/admin/options.py
3+++ b/django/contrib/admin/options.py
4@@ -17,6 +17,7 @@
5 from django.utils.text import capfirst, get_text_list
6 from django.utils.translation import ugettext as _
7 from django.utils.encoding import force_unicode
8+from urllib import quote as urllib_quote
9 try:
10     set
11 except NameError:
12@@ -26,6 +27,10 @@
13 # returns the <ul> class for a given radio_admin field
14 get_ul_class = lambda x: 'radiolist%s' % ((x == HORIZONTAL) and ' inline' or '')
15 
16+# Query string variable where the previous URL will be passed to both change
17+# and add views so they can redirect the user back to it when they are finished
18+RETURN_URL = 'return_to'
19+
20 class IncorrectLookupParameters(Exception):
21     pass
22 
23@@ -469,11 +474,14 @@
24             return HttpResponseRedirect(request.path)
25         else:
26             self.message_user(request, msg)
27-           
28-            # Figure out where to redirect. If the user has change permission,
29+
30+            # Figure out where to redirect. If we have been given an change list URL
31+            # (possibly filtered), go back to that URL. Otherwise If the user has change permission,
32             # redirect to the change-list page for this object. Otherwise,
33             # redirect to the admin index.
34-            if self.has_change_permission(request, None):
35+            if RETURN_URL in request.GET:
36+                post_url = request.GET[RETURN_URL]
37+            elif self.has_change_permission(request, None):
38                 post_url = '../'
39             else:
40                 post_url = '../../../'
41@@ -502,8 +510,9 @@
42             return HttpResponseRedirect("../add/")
43         else:
44             self.message_user(request, msg)
45-            return HttpResponseRedirect("../")
46-   
47+            post_url = request.GET.get(RETURN_URL, '../')
48+            return HttpResponseRedirect(post_url)
49+
50     def add_view(self, request, form_url='', extra_context=None):
51         "The 'add' admin view for this model."
52         model = self.model
53@@ -515,6 +524,8 @@
54         ModelForm = self.get_form(request)
55         formsets = []
56         if request.method == 'POST':
57+            return_to = request.GET.get(RETURN_URL, None)
58+
59             form = ModelForm(request.POST, request.FILES)
60             if form.is_valid():
61                 form_validated = True
62@@ -550,7 +561,12 @@
63             for FormSet in self.get_formsets(request):
64                 formset = FormSet(instance=self.model())
65                 formsets.append(formset)
66-       
67+
68+            return_to = request.META.get('HTTP_REFERER', None)
69+
70+        if return_to:
71+            form_url = form_url + '?%s=%s' % (RETURN_URL, urllib_quote(return_to))
72+
73         adminForm = helpers.AdminForm(form, list(self.get_fieldsets(request)), self.prepopulated_fields)
74         media = self.media + adminForm.media
75         
76@@ -573,7 +589,7 @@
77             'app_label': opts.app_label,
78         }
79         context.update(extra_context or {})
80-        return self.render_change_form(request, context, add=True)
81+        return self.render_change_form(request, context, form_url=form_url, add=True)
82     add_view = transaction.commit_on_success(add_view)
83     
84     def change_view(self, request, object_id, extra_context=None):
85@@ -601,6 +617,8 @@
86         ModelForm = self.get_form(request, obj)
87         formsets = []
88         if request.method == 'POST':
89+            return_to = request.GET.get(RETURN_URL, None)
90+
91             form = ModelForm(request.POST, request.FILES, instance=obj)
92             if form.is_valid():
93                 form_validated = True
94@@ -628,7 +646,14 @@
95             for FormSet in self.get_formsets(request, obj):
96                 formset = FormSet(instance=obj)
97                 formsets.append(formset)
98-       
99+
100+            return_to = request.META.get('HTTP_REFERER', None)
101+
102+        if return_to:
103+            form_url = '?%s=%s' % (RETURN_URL, urllib_quote(return_to))
104+        else:
105+            form_url = ''
106+
107         adminForm = helpers.AdminForm(form, self.get_fieldsets(request, obj), self.prepopulated_fields)
108         media = self.media + adminForm.media
109         
110@@ -652,7 +677,7 @@
111             'app_label': opts.app_label,
112         }
113         context.update(extra_context or {})
114-        return self.render_change_form(request, context, change=True, obj=obj)
115+        return self.render_change_form(request, context, change=True, form_url=form_url, obj=obj)
116     change_view = transaction.commit_on_success(change_view)
117     
118     def changelist_view(self, request, extra_context=None):