Ticket #19721: comma_in__in_data.patch

File comma_in__in_data.patch, 2.8 KB (added by chlunde, 12 years ago)

Patch with test

  • django/contrib/admin/util.py

    diff --git a/django/contrib/admin/util.py b/django/contrib/admin/util.py
    index a85045c..0c63b99 100644
    a b from __future__ import unicode_literals  
    22
    33import datetime
    44import decimal
     5import re
    56
    67from django.db import models
    78from django.db.models.constants import LOOKUP_SEP
    def prepare_lookup_value(key, value):  
    3637    """
    3738    # if key ends with __in, split parameter into separate values
    3839    if key.endswith('__in'):
    39         value = value.split(',')
     40        # Use '\,' to escape ',', and '\\' to escape '\'
     41        value = [re.sub(r"\\([\\,])", r"\1", v) for v in re.split(r'(?:(?<!\\)|(?<=\\\\)),', value)]
    4042    # if key ends with __isnull, special case '' and false
    4143    if key.endswith('__isnull'):
    4244        if value.lower() in ('', 'false'):
  • tests/regressiontests/admin_filters/tests.py

    diff --git a/tests/regressiontests/admin_filters/tests.py b/tests/regressiontests/admin_filters/tests.py
    index 11f792e..6f7d88b 100644
    a b class ListFiltersTests(TestCase):  
    163163        self.dev = Department.objects.create(code='DEV', description='Development')
    164164        self.design = Department.objects.create(code='DSN', description='Design')
    165165
    166         # Employees
    167         self.john = Employee.objects.create(name='John Blue', department=self.dev)
    168         self.jack = Employee.objects.create(name='Jack Red', department=self.design)
     166        # Employees, with special characters in names. Refs #19721
     167        self.john = Employee.objects.create(name='Red, John', department=self.dev)
     168        self.jack = Employee.objects.create(name='Blue, Jack \\', department=self.design)
    169169
    170170    def get_changelist(self, request, model, modeladmin):
    171171        return ChangeList(request, model, modeladmin.list_display, modeladmin.list_display_links,
    class ListFiltersTests(TestCase):  
    741741        self.assertEqual(choices[2]['display'], 'Design')
    742742        self.assertEqual(choices[2]['selected'], False)
    743743        self.assertEqual(choices[2]['query_string'], '?department__code__exact=DSN')
     744
     745    def test_comma_in___in_data(self):
     746        """
     747        Ensure that we can use values with ',' and '\\' with __in, by escaping
     748        Refs #19721
     749        """
     750        modeladmin = EmployeeAdmin(Employee, site)
     751
     752        employees = [self.jack, self.john]
     753
     754        def quote(name):
     755            return name.replace("\\", "\\\\").replace(",", "\\,")
     756
     757        request = self.request_factory.get('/', {'name__in': ",".join(quote(e.name) for e in employees)})
     758
     759        changelist = self.get_changelist(request, Employee, modeladmin)
     760
     761        # Make sure the correct queryset is returned
     762        queryset = changelist.get_query_set(request)
     763        self.assertEqual(list(queryset), employees)
Back to Top