Ticket #3400: 3400-against-10706.diff

File 3400-against-10706.diff, 14.0 KB (added by Honza_Kral, 6 years ago)

new patch with init and some more data in tests

  • django/contrib/admin/filterspecs.py

    commit 6a328f5b06e13a25bf8a4214fe4bebfa2efe4dd7
    Author: Honza Kral <Honza.Kral@gmail.com>
    Date:   Fri May 8 13:19:26 2009 +0200
    
        New version of #3400 against [10706]
        
        Added some data to the test to actually test the filtering and just he
        fact that it works.
    
    diff --git a/django/contrib/admin/filterspecs.py b/django/contrib/admin/filterspecs.py
    index 6f643ee..7f473a2 100644
    a b import datetime 
    1515
    1616class FilterSpec(object):
    1717    filter_specs = []
    18     def __init__(self, f, request, params, model, model_admin):
     18    def __init__(self, f, request, params, model, model_admin, field_path=None):
    1919        self.field = f
    2020        self.params = params
     21        self.field_path = field_path or f.name
    2122
    2223    def register(cls, test, factory):
    2324        cls.filter_specs.append((test, factory))
    2425    register = classmethod(register)
    2526
    26     def create(cls, f, request, params, model, model_admin):
     27    def create(cls, f, request, params, model, model_admin, field_path=None):
    2728        for test, factory in cls.filter_specs:
    2829            if test(f):
    29                 return factory(f, request, params, model, model_admin)
     30                return factory(f, request, params, model, model_admin, field_path=field_path)
    3031    create = classmethod(create)
    3132
    3233    def has_output(self):
    class FilterSpec(object): 
    5253        return mark_safe("".join(t))
    5354
    5455class RelatedFilterSpec(FilterSpec):
    55     def __init__(self, f, request, params, model, model_admin):
    56         super(RelatedFilterSpec, self).__init__(f, request, params, model, model_admin)
     56    def __init__(self, f, request, params, model, model_admin, field_path=None):
     57        super(RelatedFilterSpec, self).__init__(f, request, params, model, model_admin, field_path=field_path)
    5758        if isinstance(f, models.ManyToManyField):
    5859            self.lookup_title = f.rel.to._meta.verbose_name
    5960        else:
    6061            self.lookup_title = f.verbose_name
    61         rel_name = f.rel.get_related_field().name
    62         self.lookup_kwarg = '%s__%s__exact' % (f.name, rel_name)
     62        rel_name = f.rel.to._meta.pk.name
     63        self.lookup_kwarg = '%s__%s__exact' % (self.field_path, rel_name)
    6364        self.lookup_val = request.GET.get(self.lookup_kwarg, None)
    6465        self.lookup_choices = f.get_choices(include_blank=False)
    6566
    class RelatedFilterSpec(FilterSpec): 
    8182FilterSpec.register(lambda f: bool(f.rel), RelatedFilterSpec)
    8283
    8384class ChoicesFilterSpec(FilterSpec):
    84     def __init__(self, f, request, params, model, model_admin):
    85         super(ChoicesFilterSpec, self).__init__(f, request, params, model, model_admin)
    86         self.lookup_kwarg = '%s__exact' % f.name
     85    def __init__(self, f, request, params, model, model_admin, field_path=None):
     86        super(ChoicesFilterSpec, self).__init__(f, request, params, model, model_admin, field_path=field_path)
     87        self.lookup_kwarg = '%s__exact' % self.field_path
    8788        self.lookup_val = request.GET.get(self.lookup_kwarg, None)
    8889
    8990    def choices(self, cl):
    class ChoicesFilterSpec(FilterSpec): 
    9899FilterSpec.register(lambda f: bool(f.choices), ChoicesFilterSpec)
    99100
    100101class DateFieldFilterSpec(FilterSpec):
    101     def __init__(self, f, request, params, model, model_admin):
    102         super(DateFieldFilterSpec, self).__init__(f, request, params, model, model_admin)
     102    def __init__(self, f, request, params, model, model_admin, field_path=None):
     103        super(DateFieldFilterSpec, self).__init__(f, request, params, model, model_admin, field_path=field_path)
    103104
    104         self.field_generic = '%s__' % self.field.name
     105        self.field_generic = '%s__' % self.field_path
    105106
    106107        self.date_params = dict([(k, v) for k, v in params.items() if k.startswith(self.field_generic)])
    107108
    class DateFieldFilterSpec(FilterSpec): 
    111112
    112113        self.links = (
    113114            (_('Any date'), {}),
    114             (_('Today'), {'%s__year' % self.field.name: str(today.year),
    115                        '%s__month' % self.field.name: str(today.month),
    116                        '%s__day' % self.field.name: str(today.day)}),
    117             (_('Past 7 days'), {'%s__gte' % self.field.name: one_week_ago.strftime('%Y-%m-%d'),
    118                              '%s__lte' % f.name: today_str}),
    119             (_('This month'), {'%s__year' % self.field.name: str(today.year),
    120                              '%s__month' % f.name: str(today.month)}),
    121             (_('This year'), {'%s__year' % self.field.name: str(today.year)})
     115            (_('Today'), {'%s__year' % self.field_path: str(today.year),
     116                       '%s__month' % self.field_path: str(today.month),
     117                       '%s__day' % self.field_path: str(today.day)}),
     118            (_('Past 7 days'), {'%s__gte' % self.field_path: one_week_ago.strftime('%Y-%m-%d'),
     119                             '%s__lte' % self.field_path: today_str}),
     120            (_('This month'), {'%s__year' % self.field_path: str(today.year),
     121                             '%s__month' % self.field_path: str(today.month)}),
     122            (_('This year'), {'%s__year' % self.field_path: str(today.year)})
    122123        )
    123124
    124125    def title(self):
    class DateFieldFilterSpec(FilterSpec): 
    133134FilterSpec.register(lambda f: isinstance(f, models.DateField), DateFieldFilterSpec)
    134135
    135136class BooleanFieldFilterSpec(FilterSpec):
    136     def __init__(self, f, request, params, model, model_admin):
    137         super(BooleanFieldFilterSpec, self).__init__(f, request, params, model, model_admin)
    138         self.lookup_kwarg = '%s__exact' % f.name
    139         self.lookup_kwarg2 = '%s__isnull' % f.name
     137    def __init__(self, f, request, params, model, model_admin, field_path=None):
     138        super(BooleanFieldFilterSpec, self).__init__(f, request, params, model, model_admin, field_path=field_path)
     139        self.lookup_kwarg = '%s__exact' % self.field_path
     140        self.lookup_kwarg2 = '%s__isnull' % self.field_path
    140141        self.lookup_val = request.GET.get(self.lookup_kwarg, None)
    141142        self.lookup_val2 = request.GET.get(self.lookup_kwarg2, None)
    142143
    FilterSpec.register(lambda f: isinstance(f, models.BooleanField) or isinstance(f 
    159160# if a field is eligible to use the BooleanFieldFilterSpec, that'd be much
    160161# more appropriate, and the AllValuesFilterSpec won't get used for it.
    161162class AllValuesFilterSpec(FilterSpec):
    162     def __init__(self, f, request, params, model, model_admin):
    163         super(AllValuesFilterSpec, self).__init__(f, request, params, model, model_admin)
    164         self.lookup_val = request.GET.get(f.name, None)
    165         self.lookup_choices = model_admin.queryset(request).distinct().order_by(f.name).values(f.name)
     163    def __init__(self, f, request, params, model, model_admin, field_path=None):
     164        super(AllValuesFilterSpec, self).__init__(f, request, params, model, model_admin, field_path=field_path)
     165        self.lookup_val = request.GET.get(self.field_path, None)
     166        #self.lookup_choices = model_admin.queryset(request).distinct().order_by(f.name).values(f.name)
     167        self.lookup_choices = model._default_manager.all().distinct().order_by(f.name).values(f.name)
    166168
    167169    def title(self):
    168170        return self.field.verbose_name
    169171
    170172    def choices(self, cl):
    171173        yield {'selected': self.lookup_val is None,
    172                'query_string': cl.get_query_string({}, [self.field.name]),
     174               'query_string': cl.get_query_string({}, [self.field_path]),
    173175               'display': _('All')}
    174176        for val in self.lookup_choices:
    175177            val = smart_unicode(val[self.field.name])
    176178            yield {'selected': self.lookup_val == val,
    177                    'query_string': cl.get_query_string({self.field.name: val}),
     179                   'query_string': cl.get_query_string({self.field_path: val}),
    178180                   'display': val}
    179181FilterSpec.register(lambda f: True, AllValuesFilterSpec)
  • django/contrib/admin/validation.py

    diff --git a/django/contrib/admin/validation.py b/django/contrib/admin/validation.py
    index 4bef007..70d2dfc 100644
    a b def check_isdict(cls, label, obj): 
    279279
    280280def get_field(cls, model, opts, label, field):
    281281    try:
    282         return opts.get_field(field)
     282        if '__' in field:
     283            f = None
     284            m = model
     285            path = field.split('__')
     286            for field_name in path[:-1]:
     287                f = model._meta.get_field(field_name)
     288                model = f.rel.to
     289            return opts.get_field(path[0])
     290        else:
     291            return opts.get_field(field)
    283292    except models.FieldDoesNotExist:
    284293        raise ImproperlyConfigured("'%s.%s' refers to field '%s' that is missing from model '%s'."
    285294                % (cls.__name__, label, field, model.__name__))
  • django/contrib/admin/views/main.py

    diff --git a/django/contrib/admin/views/main.py b/django/contrib/admin/views/main.py
    index 98e5b9f..e444a17 100644
    a b class ChangeList(object): 
    7474    def get_filters(self, request):
    7575        filter_specs = []
    7676        if self.list_filter:
    77             filter_fields = [self.lookup_opts.get_field(field_name) for field_name in self.list_filter]
    78             for f in filter_fields:
    79                 spec = FilterSpec.create(f, request, self.params, self.model, self.model_admin)
     77            lookup_opts = self.lookup_opts
     78            for filter_name in self.list_filter:
     79                if '__' in filter_name:
     80                    f = None
     81                    model = self.model
     82                    path = filter_name.split('__')
     83                    for field_name in path[:-1]:
     84                        f = model._meta.get_field(field_name)
     85                        model = f.rel.to
     86                        f = model._meta.get_field(path[-1])
     87                        spec = FilterSpec.create(f, request, self.params, model, self.model_admin, field_path=filter_name)
     88                else:
     89                    f = lookup_opts.get_field(filter_name)
     90                    spec = FilterSpec.create(f, request, self.params, self.model, self.model_admin)
    8091                if spec and spec.has_output():
    8192                    filter_specs.append(spec)
    8293        return filter_specs, bool(filter_specs)
  • docs/ref/contrib/admin/index.txt

    diff --git a/docs/ref/contrib/admin/index.txt b/docs/ref/contrib/admin/index.txt
    index 700303f..263f4c2 100644
    a b how both ``list_display`` and ``list_filter`` work:: 
    446446        list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff')
    447447        list_filter = ('is_staff', 'is_superuser')
    448448
     449In ``list_filter`` can be defined lookup separator as well::
     450
     451    class UserAdminWithLookup(UserAdmin):
     452        list_filter = ('groups__name')
     453
    449454The above code results in an admin change list page that looks like this:
    450455
    451456    .. image:: _images/users_changelist.png
  • new file tests/regressiontests/admin_filters/fixtures/admin-filter-data.json

    diff --git a/tests/regressiontests/admin_filters/__init__.py b/tests/regressiontests/admin_filters/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/admin_filters/fixtures/admin-filter-data.json b/tests/regressiontests/admin_filters/fixtures/admin-filter-data.json
    new file mode 100644
    index 0000000..dd739a3
    - +  
     1[
     2    {
     3        "pk": 1,
     4        "model": "admin_filters.filterable",
     5        "fields": {
     6            "sites": [1]
     7        }
     8    },
     9    {
     10        "pk": 2,
     11        "model": "admin_filters.filterable",
     12        "fields": {
     13            "sites": []
     14        }
     15    },
     16    {
     17        "pk": 3,
     18        "model": "admin_filters.filterable",
     19        "fields": {
     20            "sites": [1]
     21        }
     22    }
     23]
  • new file tests/regressiontests/admin_filters/fixtures/admin-filter-user.json

    diff --git a/tests/regressiontests/admin_filters/fixtures/admin-filter-user.json b/tests/regressiontests/admin_filters/fixtures/admin-filter-user.json
    new file mode 100644
    index 0000000..33ae10e
    - +  
     1[
     2    {
     3        "pk": 1,
     4        "model": "auth.user",
     5        "fields": {
     6            "username": "admin",
     7            "first_name": "",
     8            "last_name": "",
     9            "is_active": true,
     10            "is_superuser": true,
     11            "is_staff": true,
     12            "last_login": "2009-03-30 22:05:28",
     13            "groups": [],
     14            "user_permissions": [],
     15            "password": "sha1$6d71f$d2b636e70cbd76dd4138766efffc46f30bcc5895",
     16            "email": "a@a.cz",
     17            "date_joined": "2009-03-30 22:05:28"
     18        }
     19    }
     20]
  • new file tests/regressiontests/admin_filters/models.py

    diff --git a/tests/regressiontests/admin_filters/models.py b/tests/regressiontests/admin_filters/models.py
    new file mode 100644
    index 0000000..6c28ae2
    - +  
     1from django.db import models
     2from django.contrib.sites.models import Site
     3
     4class Filterable(models.Model):
     5    sites = models.ManyToManyField(Site, blank=True)
     6
     7
     8from django.contrib import admin
     9
     10class FilterableAdmin(admin.ModelAdmin):
     11    list_filter = ('sites__domain',)
     12
     13admin.site.register(Filterable, FilterableAdmin)
     14
  • new file tests/regressiontests/admin_filters/tests.py

    diff --git a/tests/regressiontests/admin_filters/tests.py b/tests/regressiontests/admin_filters/tests.py
    new file mode 100644
    index 0000000..4b71c1a
    - +  
     1from django.test import TestCase
     2
     3
     4class AdminFilters(TestCase):
     5    fixtures = ['admin-filter-user.json', 'admin-filter-data.json']
     6    admin_url = '/test_admin/admin'
     7
     8    def setUp(self):
     9        self.client.login(username='admin', password='admin')
     10
     11    def tearDown(self):
     12        self.client.logout()
     13
     14    def test_filters_are_enabled(self):
     15        """
     16        log superuser in and go to filtered page
     17        """
     18        response = self.client.get('%s/admin_filters/filterable/' % self.admin_url, {'sites__domain': 'example.com'})
     19
     20        self.failUnlessEqual(response.status_code, 200)
     21        self.failUnlessEqual('2 filterables' in response.content, True)
     22
Back to Top