Ticket #19755: 19755.diff

File 19755.diff, 15.9 KB (added by Przemysław Suliga, 13 years ago)
  • django/contrib/admin/filters.py

    diff --git a/django/contrib/admin/filters.py b/django/contrib/admin/filters.py
    index ff66d3e..c4f7ca7 100644
    a b class ListFilter(object):  
    5454        """
    5555        raise NotImplementedError
    5656
     57
     58
     59
    5760
    5861class SimpleListFilter(ListFilter):
    5962    # The parameter that should be used in the query string for that filter.
    class FieldListFilter(ListFilter):  
    118121        self.field = field
    119122        self.field_path = field_path
    120123        self.title = getattr(field, 'verbose_name', field_path)
     124
    121125        super(FieldListFilter, self).__init__(
    122126            request, params, model, model_admin)
    123127        for p in self.expected_parameters():
    class FieldListFilter(ListFilter):  
    134138        except ValidationError as e:
    135139            raise IncorrectLookupParameters(e)
    136140
     141
     142
     143
    137144    @classmethod
    138145    def register(cls, test, list_filter_class, take_priority=False):
    139146        if take_priority:
    class FieldListFilter(ListFilter):  
    157164
    158165class RelatedFieldListFilter(FieldListFilter):
    159166    def __init__(self, field, request, params, model, model_admin, field_path):
    160         other_model = get_model_from_relation(field)
     167        other_model = get_model_from_relation(field)
    161168        if hasattr(field, 'rel'):
    162169            rel_name = field.rel.get_related_field().name
    163170        else:
    164             rel_name = other_model._meta.pk.name
     171            rel_name = other_model._meta.pk.name
    165172        self.lookup_kwarg = '%s__%s__exact' % (field_path, rel_name)
    166173        self.lookup_kwarg_isnull = '%s__isnull' % field_path
    167174        self.lookup_val = request.GET.get(self.lookup_kwarg, None)
    168175        self.lookup_val_isnull = request.GET.get(
    169176                                      self.lookup_kwarg_isnull, None)
    170         self.lookup_choices = field.get_choices(include_blank=False)
     177        self.
    171178        super(RelatedFieldListFilter, self).__init__(
    172179            field, request, params, model, model_admin, field_path)
    173180        if hasattr(field, 'verbose_name'):
    174181            self.lookup_title = field.verbose_name
    175182        else:
    176             self.lookup_title = other_model._meta.verbose_name
     183            self.lookup_title = other_model._meta.verbose_name
    177184        self.title = self.lookup_title
    178185
    179186    def has_output(self):
    class RelatedFieldListFilter(FieldListFilter):  
    183190            extra = 1
    184191        else:
    185192            extra = 0
    186         return len(self.lookup_choices) + extra > 1
     193        return len(self.lookup_choices) + extra >
    187194
    188195    def expected_parameters(self):
    189196        return [self.lookup_kwarg, self.lookup_kwarg_isnull]
    190197
     198
     199
     200
     201
     202
     203
     204
     205
     206
     207
     208
     209
     210
    191211    def choices(self, cl):
    192212        from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE
    193213        yield {
    class BooleanFieldListFilter(FieldListFilter):  
    233253        return [self.lookup_kwarg, self.lookup_kwarg2]
    234254
    235255    def choices(self, cl):
    236         for lookup, title in (
    237                 (None, _('All')),
    238                 ('1', _('Yes')),
    239                 ('0', _('No'))):
     256        lookup, title = None, _('All')
     257        yield {
     258            'selected': self.lookup_val == lookup and not self.lookup_val2,
     259            'query_string': cl.get_query_string({
     260                    self.lookup_kwarg: lookup,
     261                }, [self.lookup_kwarg2]),
     262            'display': title,
     263        }
     264        choices = self.lookup_choices()
     265        for choice in choices:
     266            if choice is None:
     267                continue
     268            elif choice:
     269                lookup, title = '1', _('Yes')
     270            else:
     271                lookup, title = '0', _('No')
    240272            yield {
    241273                'selected': self.lookup_val == lookup and not self.lookup_val2,
    242274                'query_string': cl.get_query_string({
    class BooleanFieldListFilter(FieldListFilter):  
    244276                    }, [self.lookup_kwarg2]),
    245277                'display': title,
    246278            }
    247         if isinstance(self.field, models.NullBooleanField):
     279        if :
    248280            yield {
    249281                'selected': self.lookup_val2 == 'True',
    250282                'query_string': cl.get_query_string({
    class BooleanFieldListFilter(FieldListFilter):  
    253285                'display': _('Unknown'),
    254286            }
    255287
     288
     289
     290
     291
     292
     293
     294
     295
     296
     297
     298
     299
     300
    256301FieldListFilter.register(lambda f: isinstance(f,
    257302    (models.BooleanField, models.NullBooleanField)), BooleanFieldListFilter)
    258303
    class ChoicesFieldListFilter(FieldListFilter):  
    261306    def __init__(self, field, request, params, model, model_admin, field_path):
    262307        self.lookup_kwarg = '%s__exact' % field_path
    263308        self.lookup_val = request.GET.get(self.lookup_kwarg)
     309
    264310        super(ChoicesFieldListFilter, self).__init__(
    265311            field, request, params, model, model_admin, field_path)
    266312
     313
     314
     315
    267316    def expected_parameters(self):
    268317        return [self.lookup_kwarg]
    269318
    class ChoicesFieldListFilter(FieldListFilter):  
    273322            'query_string': cl.get_query_string({}, [self.lookup_kwarg]),
    274323            'display': _('All')
    275324        }
    276         for lookup, title in self.field.flatchoices:
     325        for lookup, title in self.choices:
    277326            yield {
    278327                'selected': smart_text(lookup) == self.lookup_val,
    279328                'query_string': cl.get_query_string({
    class ChoicesFieldListFilter(FieldListFilter):  
    281330                'display': title,
    282331            }
    283332
     333
     334
     335
     336
     337
     338
     339
     340
     341
     342
     343
     344
     345
     346
     347
     348
     349
     350
     351
    284352FieldListFilter.register(lambda f: bool(f.choices), ChoicesFieldListFilter)
    285353
    286354
    class DateFieldListFilter(FieldListFilter):  
    302370            today = now.date()
    303371        tomorrow = today + datetime.timedelta(days=1)
    304372
     373
     374
     375
    305376        self.lookup_kwarg_since = '%s__gte' % field_path
    306377        self.lookup_kwarg_until = '%s__lt' % field_path
    307         self.links = (
    308             (_('Any date'), {}),
    309             (_('Today'), {
    310                 self.lookup_kwarg_since: str(today),
    311                 self.lookup_kwarg_until: str(tomorrow),
    312             }),
    313             (_('Past 7 days'), {
    314                 self.lookup_kwarg_since: str(today - datetime.timedelta(days=7)),
    315                 self.lookup_kwarg_until: str(tomorrow),
    316             }),
    317             (_('This month'), {
    318                 self.lookup_kwarg_since: str(today.replace(day=1)),
    319                 self.lookup_kwarg_until: str(tomorrow),
    320             }),
    321             (_('This year'), {
    322                 self.lookup_kwarg_since: str(today.replace(month=1, day=1)),
    323                 self.lookup_kwarg_until: str(tomorrow),
    324             }),
    325         )
     378        self._links = None
    326379        super(DateFieldListFilter, self).__init__(
    327380            field, request, params, model, model_admin, field_path)
    328381
     382
     383
     384
    329385    def expected_parameters(self):
    330386        return [self.lookup_kwarg_since, self.lookup_kwarg_until]
    331387
    class DateFieldListFilter(FieldListFilter):  
    338394                'display': title,
    339395            }
    340396
     397
     398
     399
     400
     401
     402
     403
     404
     405
     406
     407
     408
     409
     410
     411
     412
     413
     414
     415
     416
     417
     418
     419
     420
     421
     422
     423
     424
     425
     426
    341427FieldListFilter.register(
    342428    lambda f: isinstance(f, models.DateField), DateFieldListFilter)
    343429
    class AllValuesFieldListFilter(FieldListFilter):  
    352438        self.lookup_val = request.GET.get(self.lookup_kwarg, None)
    353439        self.lookup_val_isnull = request.GET.get(self.lookup_kwarg_isnull,
    354440                                                 None)
    355         parent_model, reverse_path = reverse_field_path(model, field_path)
     441        parent_model, reverse_path = reverse_field_path(model, field_path)
    356442        queryset = parent_model._default_manager.all()
    357443        # optional feature: limit choices base on existing relationships
    358444        # queryset = queryset.complex_filter(
    359445        #    {'%s__isnull' % reverse_path: False})
    360446        limit_choices_to = get_limit_choices_to_from_path(model, field_path)
    361447        queryset = queryset.filter(limit_choices_to)
     448
    362449
    363         self.lookup_choices = (queryset
    364                                .distinct()
    365                                .order_by(field.name)
    366                                .values_list(field.name, flat=True))
     450        self._lookup_choices = None
    367451        super(AllValuesFieldListFilter, self).__init__(
    368452            field, request, params, model, model_admin, field_path)
    369453
     454
     455
     456
    370457    def expected_parameters(self):
    371458        return [self.lookup_kwarg, self.lookup_kwarg_isnull]
    372459
    class AllValuesFieldListFilter(FieldListFilter):  
    401488                'display': EMPTY_CHANGELIST_VALUE,
    402489            }
    403490
     491
     492
     493
     494
     495
     496
     497
     498
     499
     500
     501
     502
     503
     504
     505
    404506FieldListFilter.register(lambda f: True, AllValuesFieldListFilter)
  • django/contrib/admin/options.py

    diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
    index b35f100..ee8759f 100644
    a b class ModelAdmin(BaseModelAdmin):  
    345345    list_display = ('__str__',)
    346346    list_display_links = ()
    347347    list_filter = ()
     348
    348349    list_select_related = False
    349350    list_per_page = 100
    350351    list_max_show_all = 200
  • django/contrib/admin/validation.py

    diff --git a/django/contrib/admin/validation.py b/django/contrib/admin/validation.py
    index a02bb7a..1efbcd7 100644
    a b def validate(cls, model):  
    163163    if hasattr(cls, "readonly_fields"):
    164164        check_readonly_fields(cls, model, opts)
    165165
     166
    166167    # list_select_related = False
    167168    # save_as = False
    168169    # save_on_top = False
    169     for attr in ('list_select_related', 'save_as', 'save_on_top'):
     170    for attr in ('list_select_related', 'save_as', 'save_on_top'):
    170171        if not isinstance(getattr(cls, attr), bool):
    171172            raise ImproperlyConfigured("'%s.%s' should be a boolean."
    172173                    % (cls.__name__, attr))
  • django/contrib/admin/views/main.py

    diff --git a/django/contrib/admin/views/main.py b/django/contrib/admin/views/main.py
    index 050d477..9b78561 100644
    a b class ChangeList(six.with_metaclass(RenameChangeListMethods)):  
    158158                    use_distinct = (use_distinct or
    159159                                    lookup_needs_distinct(self.lookup_opts,
    160160                                                          field_path))
    161                 if spec and spec.has_output():
     161                incremental = self.model_admin.list_filter_incremental
     162                if spec and (incremental or spec.has_output()):
    162163                    filter_specs.append(spec)
    163164
    164165        # At this point, all the parameters used by the various ListFilters
    class ChangeList(six.with_metaclass(RenameChangeListMethods)):  
    330331
    331332    def get_queryset(self, request):
    332333        # First, we collect all the declared list filters.
    333         (self.filter_specs, self.has_filters, remaining_lookup_params,
     334        (filter_specs, self.has_filters, remaining_lookup_params,
    334335         use_distinct) = self.get_filters(request)
    335336
    336337        # Then, we let every list filter modify the queryset to its liking.
    337338        qs = self.root_queryset
    338         for filter_spec in self.filter_specs:
     339        for filter_spec in filter_specs:
    339340            new_qs = filter_spec.queryset(request, qs)
    340341            if new_qs is not None:
    341342                qs = new_qs
    342343
     344
     345
     346
     347
     348
     349
     350
     351
    343352        try:
    344353            # Finally, we apply the remaining lookup parameters from the query
    345354            # string (i.e. those that haven't already been processed by the
Back to Top