Ticket #5833: 5833-against-7875.3.patch

File 5833-against-7875.3.patch, 3.7 KB (added by Honza Král, 16 years ago)
  • django/contrib/admin/filterspecs.py

    commit 84326207d3b631cfa03d50799fe4c20a3ec9d7c1
    Author: Honza Král <Honza.Kral@gmail.com>
    Date:   Mon Jan 28 22:15:20 2008 +0100
    
        newforms-admin enable registering custom FilterSpecs
        
        http://code.djangoproject.com/ticket/5833
    
    diff --git a/django/contrib/admin/filterspecs.py b/django/contrib/admin/filterspecs.py
    index d6a4a0b..148bb50 100644
    a b class FilterSpec(object):  
    2020        self.params = params
    2121
    2222    def register(cls, test, factory):
    23         cls.filter_specs.append((test, factory))
     23        cls.filter_specs.insert(0, (test, factory))
    2424    register = classmethod(register)
    2525
    2626    def create(cls, f, request, params, model, model_admin):
    class RelatedFilterSpec(FilterSpec):  
    7878                   'query_string': cl.get_query_string({self.lookup_kwarg: pk_val}),
    7979                   'display': val}
    8080
    81 FilterSpec.register(lambda f: bool(f.rel), RelatedFilterSpec)
    82 
    8381class ChoicesFilterSpec(FilterSpec):
    8482    def __init__(self, f, request, params, model, model_admin):
    8583        super(ChoicesFilterSpec, self).__init__(f, request, params, model, model_admin)
    class ChoicesFilterSpec(FilterSpec):  
    9593                    'query_string': cl.get_query_string({self.lookup_kwarg: k}),
    9694                    'display': v}
    9795
    98 FilterSpec.register(lambda f: bool(f.choices), ChoicesFilterSpec)
    99 
    10096class DateFieldFilterSpec(FilterSpec):
    10197    def __init__(self, f, request, params, model, model_admin):
    10298        super(DateFieldFilterSpec, self).__init__(f, request, params, model, model_admin)
    class DateFieldFilterSpec(FilterSpec):  
    130126                   'query_string': cl.get_query_string(param_dict, [self.field_generic]),
    131127                   'display': title}
    132128
    133 FilterSpec.register(lambda f: isinstance(f, models.DateField), DateFieldFilterSpec)
    134 
    135129class BooleanFieldFilterSpec(FilterSpec):
    136130    def __init__(self, f, request, params, model, model_admin):
    137131        super(BooleanFieldFilterSpec, self).__init__(f, request, params, model, model_admin)
    class BooleanFieldFilterSpec(FilterSpec):  
    153147                   'query_string': cl.get_query_string({self.lookup_kwarg2: 'True'}, [self.lookup_kwarg]),
    154148                   'display': _('Unknown')}
    155149
    156 FilterSpec.register(lambda f: isinstance(f, models.BooleanField) or isinstance(f, models.NullBooleanField), BooleanFieldFilterSpec)
    157 
    158 # This should be registered last, because it's a last resort. For example,
    159 # if a field is eligible to use the BooleanFieldFilterSpec, that'd be much
    160 # more appropriate, and the AllValuesFilterSpec won't get used for it.
    161150class AllValuesFilterSpec(FilterSpec):
    162151    def __init__(self, f, request, params, model, model_admin):
    163152        super(AllValuesFilterSpec, self).__init__(f, request, params, model, model_admin)
    class AllValuesFilterSpec(FilterSpec):  
    176165            yield {'selected': self.lookup_val == val,
    177166                   'query_string': cl.get_query_string({self.field.name: val}),
    178167                   'display': val}
     168
     169# This should be registered first, because it's a last resort. For example,
     170# if a field is eligible to use the BooleanFieldFilterSpec, that'd be much
     171# more appropriate, and the AllValuesFilterSpec won't get used for it.
    179172FilterSpec.register(lambda f: True, AllValuesFilterSpec)
     173FilterSpec.register(lambda f: isinstance(f, (models.BooleanField, models.NullBooleanField), BooleanFieldFilterSpec))
     174FilterSpec.register(lambda f: isinstance(f, models.DateField), DateFieldFilterSpec)
     175FilterSpec.register(lambda f: bool(f.choices), ChoicesFilterSpec)
     176FilterSpec.register(lambda f: bool(f.rel), RelatedFilterSpec)
Back to Top