Ticket #5833: filterspec_with_custom_queryset_against_1_0.diff
File filterspec_with_custom_queryset_against_1_0.diff, 11.3 KB (added by , 16 years ago) |
---|
-
django/contrib/admin/validation.py
6 6 from django.core.exceptions import ImproperlyConfigured 7 7 from django.db import models 8 8 from django.forms.models import BaseModelForm, BaseModelFormSet, fields_for_model 9 from django.contrib.admin.filterspecs import FieldFilterSpec 9 10 from django.contrib.admin.options import flatten_fieldsets, BaseModelAdmin 10 11 from django.contrib.admin.options import HORIZONTAL, VERTICAL 11 12 … … 54 55 # list_filter 55 56 if hasattr(cls, 'list_filter'): 56 57 check_isseq(cls, 'list_filter', cls.list_filter) 57 for idx, field in enumerate(cls.list_filter): 58 for idx, item in enumerate(cls.list_filter): 59 if callable(item): 60 #TODO find out whether item is of type FilterSpec 61 pass 62 else: 63 if type(item) is tuple: 64 (field, factory) = item 65 #TODO find out whether factory is of type (or a descendant of) FieldFilterSpec 66 #raise ImproperlyConfigured("'%s.list_filter[%d][1]' is not of type FieldFilterSpec." 67 # % (cls.__name__, idx)) 68 else: 69 field = item 70 # validate field 58 71 get_field(cls, model, opts, 'list_filter[%d]' % idx, field) 59 72 60 73 # list_per_page = 100 -
django/contrib/admin/filterspecs.py
14 14 import datetime 15 15 16 16 class FilterSpec(object): 17 filter_specs = [] 18 def __init__(self, f, request, params, model, model_admin): 19 self.field = f 17 18 def __init__(self, request, params, model, model_admin): 20 19 self.params = params 21 20 22 def register(cls, test, factory):23 cls.filter_specs.append((test, factory))24 register = classmethod(register)25 26 def create(cls, f, request, params, model, model_admin):27 for test, factory in cls.filter_specs:28 if test(f):29 return factory(f, request, params, model, model_admin)30 create = classmethod(create)31 32 21 def has_output(self): 33 22 return True 34 23 … … 36 25 raise NotImplementedError() 37 26 38 27 def title(self): 39 return self.field.verbose_name 28 raise NotImplementedError() 29 30 def get_query_set(self, cl, qs): 31 return qs 32 40 33 41 34 def output(self, cl): 42 35 t = [] … … 51 44 t.append('</ul>\n\n') 52 45 return mark_safe("".join(t)) 53 46 54 class RelatedFilterSpec(FilterSpec): 55 def __init__(self, f, request, params, model, model_admin): 56 super(RelatedFilterSpec, self).__init__(f, request, params, model, model_admin) 47 class FieldFilterSpec(FilterSpec): 48 field_filter_specs = [] 49 def __init__(self, request, params, model, model_admin, field): 50 super(FieldFilterSpec, self).__init__(request, params, model, model_admin) 51 self.field = field 52 53 def register(cls, test, factory): 54 cls.field_filter_specs.append((test, factory)) 55 register = classmethod(register) 56 57 def title(self): 58 return self.field.verbose_name 59 60 def create(cls, request, params, model, model_admin, field): 61 for test, factory in cls.field_filter_specs: 62 if test(field): 63 return factory(request, params, model, model_admin, field) 64 create = classmethod(create) 65 66 class RelatedFilterSpec(FieldFilterSpec): 67 def __init__(self, request, params, model, model_admin, f): 68 super(RelatedFilterSpec, self).__init__(f, request, params, model, model_admin, field) 57 69 if isinstance(f, models.ManyToManyField): 58 70 self.lookup_title = f.rel.to._meta.verbose_name 59 71 else: … … 78 90 'query_string': cl.get_query_string({self.lookup_kwarg: pk_val}), 79 91 'display': val} 80 92 81 Fi lterSpec.register(lambda f: bool(f.rel), RelatedFilterSpec)93 FieldFilterSpec.register(lambda f: bool(f.rel), RelatedFilterSpec) 82 94 83 class ChoicesFilterSpec(Fi lterSpec):84 def __init__(self, f, request, params, model, model_admin):85 super(ChoicesFilterSpec, self).__init__( f, request, params, model, model_admin)95 class ChoicesFilterSpec(FieldFilterSpec): 96 def __init__(self, request, params, model, model_admin, f): 97 super(ChoicesFilterSpec, self).__init__(request, params, model, model_admin, f) 86 98 self.lookup_kwarg = '%s__exact' % f.name 87 99 self.lookup_val = request.GET.get(self.lookup_kwarg, None) 88 100 … … 95 107 'query_string': cl.get_query_string({self.lookup_kwarg: k}), 96 108 'display': v} 97 109 98 Fi lterSpec.register(lambda f: bool(f.choices), ChoicesFilterSpec)110 FieldFilterSpec.register(lambda f: bool(f.choices), ChoicesFilterSpec) 99 111 100 class DateFieldFilterSpec(Fi lterSpec):101 def __init__(self, f, request, params, model, model_admin):102 super(DateFieldFilterSpec, self).__init__( f, request, params, model, model_admin)112 class DateFieldFilterSpec(FieldFilterSpec): 113 def __init__(self, request, params, model, model_admin, f): 114 super(DateFieldFilterSpec, self).__init__(request, params, model, model_admin, f) 103 115 104 116 self.field_generic = '%s__' % self.field.name 105 117 … … 130 142 'query_string': cl.get_query_string(param_dict, [self.field_generic]), 131 143 'display': title} 132 144 133 Fi lterSpec.register(lambda f: isinstance(f, models.DateField), DateFieldFilterSpec)145 FieldFilterSpec.register(lambda f: isinstance(f, models.DateField), DateFieldFilterSpec) 134 146 135 class BooleanFieldFilterSpec(Fi lterSpec):136 def __init__(self, f, request, params, model, model_admin):137 super(BooleanFieldFilterSpec, self).__init__( f, request, params, model, model_admin)147 class BooleanFieldFilterSpec(FieldFilterSpec): 148 def __init__(self, request, params, model, model_admin, f): 149 super(BooleanFieldFilterSpec, self).__init__(request, params, model, model_admin, f) 138 150 self.lookup_kwarg = '%s__exact' % f.name 139 151 self.lookup_kwarg2 = '%s__isnull' % f.name 140 152 self.lookup_val = request.GET.get(self.lookup_kwarg, None) … … 153 165 'query_string': cl.get_query_string({self.lookup_kwarg2: 'True'}, [self.lookup_kwarg]), 154 166 'display': _('Unknown')} 155 167 156 Fi lterSpec.register(lambda f: isinstance(f, models.BooleanField) or isinstance(f, models.NullBooleanField), BooleanFieldFilterSpec)168 FieldFilterSpec.register(lambda f: isinstance(f, models.BooleanField) or isinstance(f, models.NullBooleanField), BooleanFieldFilterSpec) 157 169 158 170 # This should be registered last, because it's a last resort. For example, 159 171 # if a field is eligible to use the BooleanFieldFilterSpec, that'd be much 160 172 # more appropriate, and the AllValuesFilterSpec won't get used for it. 161 class AllValuesFilterSpec(Fi lterSpec):162 def __init__(self, f, request, params, model, model_admin):163 super(AllValuesFilterSpec, self).__init__( f, request, params, model, model_admin)173 class AllValuesFilterSpec(FieldFilterSpec): 174 def __init__(self, request, params, model, model_admin, f): 175 super(AllValuesFilterSpec, self).__init__(request, params, model, model_admin, f) 164 176 self.lookup_val = request.GET.get(f.name, None) 165 177 self.lookup_choices = model_admin.queryset(request).distinct().order_by(f.name).values(f.name) 166 178 … … 176 188 yield {'selected': self.lookup_val == val, 177 189 'query_string': cl.get_query_string({self.field.name: val}), 178 190 'display': val} 179 Fi lterSpec.register(lambda f: True, AllValuesFilterSpec)191 FieldFilterSpec.register(lambda f: True, AllValuesFilterSpec) -
django/contrib/admin/views/main.py
1 1 from django.contrib.admin.filterspecs import FilterSpec 2 from django.contrib.admin.filterspecs import FieldFilterSpec 2 3 from django.contrib.admin.options import IncorrectLookupParameters 3 4 from django.contrib.admin.util import quote 4 5 from django.core.paginator import Paginator, InvalidPage … … 62 63 if ERROR_FLAG in self.params: 63 64 del self.params[ERROR_FLAG] 64 65 66 self.filter_specs, self.has_filters = self.get_filters(request) 67 65 68 self.order_field, self.order_type = self.get_ordering() 66 69 self.query = request.GET.get(SEARCH_VAR, '') 67 70 self.query_set = self.get_query_set() 68 71 self.get_results(request) 69 72 self.title = (self.is_popup and ugettext('Select %s') % force_unicode(self.opts.verbose_name) or ugettext('Select %s to change') % force_unicode(self.opts.verbose_name)) 70 self.filter_specs, self.has_filters = self.get_filters(request) 73 71 74 self.pk_attname = self.lookup_opts.pk.attname 72 75 73 76 def get_filters(self, request): 74 77 filter_specs = [] 75 78 if self.list_filter: 76 filter_fields = [self.lookup_opts.get_field(field_name) for field_name in self.list_filter] 77 for f in filter_fields: 78 spec = FilterSpec.create(f, request, self.params, self.model, self.model_admin) 79 for item in self.list_filter: 80 if callable(item): 81 spec = item(request, self.params, self.model, self.model_admin) 82 else: 83 if type(item) is tuple: 84 (name, factory) = item 85 else: 86 name = item 87 factory = FieldFilterSpec.create 88 f = self.lookup_opts.get_field(name) 89 spec = factory(request, self.params, self.model, self.model_admin, f) 79 90 if spec and spec.has_output(): 80 91 filter_specs.append(spec) 81 92 return filter_specs, bool(filter_specs) … … 172 183 173 184 def get_query_set(self): 174 185 qs = self.root_query_set 186 187 # let every filter do its filtering 188 for filter_spec in self.filter_specs: 189 tqs = filter_spec.get_query_set(self, qs) 190 if tqs != None: qs = tqs 191 175 192 lookup_params = self.params.copy() # a dictionary of the query string 176 193 for i in (ALL_VAR, ORDER_VAR, ORDER_TYPE_VAR, SEARCH_VAR, IS_POPUP_VAR): 177 194 if i in lookup_params: … … 181 198 # 'key' will be used as a keyword argument later, so Python 182 199 # requires it to be a string. 183 200 del lookup_params[key] 184 lookup_params[smart_str(key)] = value 201 key = smart_str(key) 202 lookup_params[key] = value 185 203 204 from django.db.models.sql.constants import LOOKUP_SEP 205 try: 206 f = self.opts.get_field(key.split(LOOKUP_SEP)[0]) 207 except models.FieldDoesNotExist: 208 del lookup_params[key] 209 continue 210 186 211 # if key ends with __in, split parameter into separate values 187 212 if key.endswith('__in'): 188 213 lookup_params[key] = value.split(',')