Ticket #6903: 6903.3.diff

File 6903.3.diff, 10.6 KB (added by Julien Phalip, 12 years ago)
  • django/contrib/admin/options.py

    diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
    index f7bfca4..11aff87 100644
    a b from django.contrib.admin.util import (unquote, flatten_fieldsets, get_deleted_o  
    1212    model_format_dict, NestedObjects)
    1313from django.contrib.admin.templatetags.admin_static import static
    1414from django.contrib import messages
     15from django.utils.http import is_safe_url
    1516from django.views.decorators.csrf import csrf_protect
    1617from django.core.exceptions import PermissionDenied, ValidationError
    1718from django.core.paginator import Paginator
    from django.utils.translation import ugettext as _  
    3536from django.utils.translation import ungettext
    3637from django.utils.encoding import force_text
    3738
     39try:
     40    from urllib import parse as urllib_parse
     41except ImportError:     # Python 2
     42    import urllib as urllib_parse
     43    import urlparse
     44    urllib_parse.urlparse = urlparse.urlparse
     45
     46
    3847HORIZONTAL, VERTICAL = 1, 2
    3948# returns the <ul> class for a given radio_admin field
    4049get_ul_class = lambda x: 'radiolist%s' % ((x == HORIZONTAL) and ' inline' or '')
    class ModelAdmin(BaseModelAdmin):  
    909918        when editing an existing object.
    910919        """
    911920        opts = self.model._meta
     921        changelist_url = reverse('admin:%s_%s_changelist' %
     922                                 (opts.app_label, opts.model_name),
     923                                 current_app=self.admin_site.name)
     924
     925        changelist_filters = request.POST.get('_changelist_filters')
     926        if changelist_filters:
     927            return HttpResponseRedirect(
     928                '%s?%s' % (changelist_url, changelist_filters))
     929
    912930        if self.has_change_permission(request, None):
    913             post_url = reverse('admin:%s_%s_changelist' %
    914                                (opts.app_label, opts.model_name),
    915                                current_app=self.admin_site.name)
     931            post_url = changelist_url
    916932        else:
    917933            post_url = reverse('admin:index',
    918934                               current_app=self.admin_site.name)
    class ModelAdmin(BaseModelAdmin):  
    10951111        ModelForm = self.get_form(request, obj)
    10961112        formsets = []
    10971113        inline_instances = self.get_inline_instances(request, obj)
     1114        changelist_filters = None
    10981115        if request.method == 'POST':
    10991116            form = ModelForm(request.POST, request.FILES, instance=obj)
    11001117            if form.is_valid():
    class ModelAdmin(BaseModelAdmin):  
    11341151                                  queryset=inline.get_queryset(request))
    11351152                formsets.append(formset)
    11361153
     1154            referer = request.META.get('HTTP_REFERER')
     1155            if referer:
     1156                referer = urllib_parse.urlparse(referer)
     1157                changelist_url = reverse('admin:%s_%s_changelist' % (opts.app_label, opts.model_name))
     1158                if is_safe_url(url=referer.geturl(), host=request.get_host()) and referer.path.startswith(changelist_url):
     1159                    changelist_filters = referer.query
     1160
    11371161        adminForm = helpers.AdminForm(form, self.get_fieldsets(request, obj),
    11381162            self.get_prepopulated_fields(request, obj),
    11391163            self.get_readonly_fields(request, obj),
    class ModelAdmin(BaseModelAdmin):  
    11601184            'inline_admin_formsets': inline_admin_formsets,
    11611185            'errors': helpers.AdminErrorList(form, formsets),
    11621186            'app_label': opts.app_label,
     1187            'changelist_filters': changelist_filters,
    11631188        }
    11641189        context.update(extra_context or {})
    11651190        return self.render_change_form(request, context, change=True, obj=obj, form_url=form_url)
    class ModelAdmin(BaseModelAdmin):  
    13481373
    13491374            self.message_user(request, _('The %(name)s "%(obj)s" was deleted successfully.') % {'name': force_text(opts.verbose_name), 'obj': force_text(obj_display)})
    13501375
     1376            changelist_url = reverse('admin:%s_%s_changelist' %
     1377                                     (opts.app_label, opts.model_name),
     1378                                     current_app=self.admin_site.name)
     1379
     1380            changelist_filters = request.POST.get('_changelist_filters')
     1381            if changelist_filters:
     1382                return HttpResponseRedirect('%s?%s' % (changelist_url, changelist_filters))
     1383
    13511384            if not self.has_change_permission(request, None):
    13521385                return HttpResponseRedirect(reverse('admin:index',
    13531386                                                    current_app=self.admin_site.name))
    1354             return HttpResponseRedirect(reverse('admin:%s_%s_changelist' %
    1355                                         (opts.app_label, opts.model_name),
    1356                                         current_app=self.admin_site.name))
     1387            return HttpResponseRedirect(changelist_url)
     1388        else:
     1389            changelist_filters = request.GET.get('_changelist_filters')
    13571390
    13581391        object_name = force_text(opts.verbose_name)
    13591392
    class ModelAdmin(BaseModelAdmin):  
    13711404            "protected": protected,
    13721405            "opts": opts,
    13731406            "app_label": app_label,
     1407            "changelist_filters": changelist_filters,
    13741408        }
    13751409        context.update(extra_context or {})
    13761410
  • django/contrib/admin/templates/admin/change_form.html

    diff --git a/django/contrib/admin/templates/admin/change_form.html b/django/contrib/admin/templates/admin/change_form.html
    index daf3775..0b2312e 100644
    a b  
    3838<form {% if has_file_field %}enctype="multipart/form-data" {% endif %}action="{{ form_url }}" method="post" id="{{ opts.model_name }}_form">{% csrf_token %}{% block form_top %}{% endblock %}
    3939<div>
    4040{% if is_popup %}<input type="hidden" name="_popup" value="1" />{% endif %}
     41{% if changelist_filters %}<input type="hidden" name="_changelist_filters" value="{{ changelist_filters }}" />{% endif %}
    4142{% if save_on_top %}{% block submit_buttons_top %}{% submit_row %}{% endblock %}{% endif %}
    4243{% if errors %}
    4344    <p class="errornote">
  • django/contrib/admin/templates/admin/delete_confirmation.html

    diff --git a/django/contrib/admin/templates/admin/delete_confirmation.html b/django/contrib/admin/templates/admin/delete_confirmation.html
    index c1a7115..065fa14 100644
    a b  
    3636    <form action="" method="post">{% csrf_token %}
    3737    <div>
    3838    <input type="hidden" name="post" value="yes" />
     39    {% if changelist_filters %}<input type="hidden" name="_changelist_filters" value="{{ changelist_filters }}" />{% endif %}
    3940    <input type="submit" value="{% trans "Yes, I'm sure" %}" />
    4041    </div>
    4142    </form>
  • tests/admin_views/tests.py

    diff --git a/tests/admin_views/tests.py b/tests/admin_views/tests.py
    index bb77932..9199a5e 100644
    a b class AdminUserMessageTest(TestCase):  
    40244024        self.assertContains(response,
    40254025                            '<li class="extra_tag info">Test tags</li>',
    40264026                            html=True)
     4027
     4028
     4029@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
     4030class AdminChangeListRedirectionTests(TestCase):
     4031    urls = "admin_views.urls"
     4032    fixtures = ['admin-views-users']
     4033
     4034    def setUp(self):
     4035        Person.objects.create(name="Chris", gender=1, alive=True, age=25)
     4036        Person.objects.create(name="Jack", gender=2, alive=False, age=47)
     4037        Person.objects.create(name="Bob", gender=1, alive=True, age=36)
     4038
     4039        self.client.login(username='super', password='secret')
     4040
     4041    def get_data(self):
     4042        return {
     4043            'name': 'Joe', 'gender': '1', 'alive': '1', 'age': '58'
     4044        }
     4045
     4046    def tearDown(self):
     4047        self.client.logout()
     4048
     4049    def get_filters_querystring(self):
     4050        return urlencode({
     4051                'alive': 1,
     4052            })
     4053
     4054    def get_filtered_changelist_url(self):
     4055        return "%s%s?%s" % ('http://testserver', reverse('admin:admin_views_person_changelist'), self.get_filters_querystring())
     4056
     4057    def test_change_redirect(self):
     4058        """
     4059        Ensure that we're correctly redirected to the filtered changelist
     4060        after an existing object is saved.
     4061        Refs #6903.
     4062        """
     4063        person = Person.objects.get(name='Chris')
     4064        change_url = reverse('admin:admin_views_person_change', args=(person.pk,))
     4065
     4066        # Simulate clicking an object from a filtered changelist
     4067        response = self.client.get(change_url, HTTP_REFERER=self.get_filtered_changelist_url())
     4068        self.assertContains(response, '_changelist_filters')
     4069        self.assertContains(response, self.get_filters_querystring())
     4070
     4071        # Save the object
     4072        data = self.get_data()
     4073        data['_save'] = 1
     4074        data['_changelist_filters'] = self.get_filters_querystring()
     4075        response = self.client.post(change_url, data)
     4076
     4077        # Check that we return to the filtered changelist
     4078        self.assertRedirects(response, self.get_filtered_changelist_url())
     4079
     4080    def test_add_redirect(self):
     4081        """
     4082        Ensure that we're correctly redirected to the non-filtered changelist
     4083        after a new object is added.
     4084        Refs #6903.
     4085        """
     4086        add_url = reverse('admin:admin_views_person_add')
     4087
     4088        response = self.client.get(add_url, HTTP_REFERER=self.get_filtered_changelist_url())
     4089        self.assertNotContains(response, '_changelist_filters')
     4090        self.assertNotContains(response, self.get_filters_querystring())
     4091
     4092        # Add a new object
     4093        data = self.get_data()
     4094        data['_save'] = 1
     4095        data['_changelist_filters'] = self.get_filters_querystring()
     4096        response = self.client.post(add_url, data)
     4097
     4098        # Check that we return to the non-filtered changelist
     4099        self.assertRedirects(response, reverse('admin:admin_views_person_changelist'))
     4100
     4101    def test_delete_redirect(self):
     4102        """
     4103        Ensure that we're correctly redirected to the filtered changelist
     4104        after an existing object is deleted.
     4105        Refs #6903.
     4106        """
     4107        person = Person.objects.get(name='Chris')
     4108        delete_url = reverse('admin:admin_views_person_delete', args=(person.pk,))
     4109
     4110        response = self.client.get(delete_url + '?_changelist_filters=%s' % self.get_filters_querystring())
     4111        self.assertContains(response, '_changelist_filters')
     4112        self.assertContains(response, self.get_filters_querystring())
     4113
     4114        # Delete the object
     4115        data = {'post': 'yes', '_changelist_filters': self.get_filters_querystring()}
     4116        response = self.client.post(delete_url, data)
     4117
     4118        # Check that we return to the filtered changelist
     4119        self.assertRedirects(response, self.get_filtered_changelist_url())
     4120
Back to Top