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):
|
20 | 20 | self.params = params |
21 | 21 | |
22 | 22 | def register(cls, test, factory): |
23 | | cls.filter_specs.append((test, factory)) |
| 23 | cls.filter_specs.insert(0, (test, factory)) |
24 | 24 | register = classmethod(register) |
25 | 25 | |
26 | 26 | def create(cls, f, request, params, model, model_admin): |
… |
… |
class RelatedFilterSpec(FilterSpec):
|
78 | 78 | 'query_string': cl.get_query_string({self.lookup_kwarg: pk_val}), |
79 | 79 | 'display': val} |
80 | 80 | |
81 | | FilterSpec.register(lambda f: bool(f.rel), RelatedFilterSpec) |
82 | | |
83 | 81 | class ChoicesFilterSpec(FilterSpec): |
84 | 82 | def __init__(self, f, request, params, model, model_admin): |
85 | 83 | super(ChoicesFilterSpec, self).__init__(f, request, params, model, model_admin) |
… |
… |
class ChoicesFilterSpec(FilterSpec):
|
95 | 93 | 'query_string': cl.get_query_string({self.lookup_kwarg: k}), |
96 | 94 | 'display': v} |
97 | 95 | |
98 | | FilterSpec.register(lambda f: bool(f.choices), ChoicesFilterSpec) |
99 | | |
100 | 96 | class DateFieldFilterSpec(FilterSpec): |
101 | 97 | def __init__(self, f, request, params, model, model_admin): |
102 | 98 | super(DateFieldFilterSpec, self).__init__(f, request, params, model, model_admin) |
… |
… |
class DateFieldFilterSpec(FilterSpec):
|
130 | 126 | 'query_string': cl.get_query_string(param_dict, [self.field_generic]), |
131 | 127 | 'display': title} |
132 | 128 | |
133 | | FilterSpec.register(lambda f: isinstance(f, models.DateField), DateFieldFilterSpec) |
134 | | |
135 | 129 | class BooleanFieldFilterSpec(FilterSpec): |
136 | 130 | def __init__(self, f, request, params, model, model_admin): |
137 | 131 | super(BooleanFieldFilterSpec, self).__init__(f, request, params, model, model_admin) |
… |
… |
class BooleanFieldFilterSpec(FilterSpec):
|
153 | 147 | 'query_string': cl.get_query_string({self.lookup_kwarg2: 'True'}, [self.lookup_kwarg]), |
154 | 148 | 'display': _('Unknown')} |
155 | 149 | |
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. |
161 | 150 | class AllValuesFilterSpec(FilterSpec): |
162 | 151 | def __init__(self, f, request, params, model, model_admin): |
163 | 152 | super(AllValuesFilterSpec, self).__init__(f, request, params, model, model_admin) |
… |
… |
class AllValuesFilterSpec(FilterSpec):
|
176 | 165 | yield {'selected': self.lookup_val == val, |
177 | 166 | 'query_string': cl.get_query_string({self.field.name: val}), |
178 | 167 | '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. |
179 | 172 | FilterSpec.register(lambda f: True, AllValuesFilterSpec) |
| 173 | FilterSpec.register(lambda f: isinstance(f, (models.BooleanField, models.NullBooleanField), BooleanFieldFilterSpec)) |
| 174 | FilterSpec.register(lambda f: isinstance(f, models.DateField), DateFieldFilterSpec) |
| 175 | FilterSpec.register(lambda f: bool(f.choices), ChoicesFilterSpec) |
| 176 | FilterSpec.register(lambda f: bool(f.rel), RelatedFilterSpec) |