/django/contrib/admin/filterspecs.py
Python | 477 lines | 475 code | 0 blank | 2 comment | 0 complexity | f35e4b32449438eccdea1fca432ff0e3 MD5 | raw file
Possible License(s): BSD-3-Clause
- """
- FilterSpec encapsulates the logic for displaying filters in the Django admin.
- Filters are specified in models with the "list_filter" option.
- Each filter subclass knows how to display a filter for a field that passes a
- certain test -- e.g. being a DateField or ForeignKey.
- """
- from django.db import models
- from django.utils.encoding import smart_unicode, iri_to_uri
- from django.utils.translation import ugettext as _
- from django.utils.html import escape
- from django.utils.safestring import mark_safe
- from django.contrib.admin.util import get_model_from_relation, \
- reverse_field_path, get_limit_choices_to_from_path
- import datetime
- class FilterSpec(object):
- HAS_OWN_OUTPUT = False
- filter_specs = []
- def __init__(self, f, request, params, model, model_admin,
- field_path=None):
- self.field = f
- self.params = params
- self.displaymulti = False
- self.multiselect=False
- self.multitag=''
- self.field_path = field_path
- if field_path is None:
- if isinstance(f, models.related.RelatedObject):
- self.field_path = f.var_name
- else:
- self.field_path = f.name
- def register(cls, test, factory):
- cls.filter_specs.append((test, factory))
- register = classmethod(register)
- def create(cls, f, request, params, model, model_admin, field_path=None):
- for test, factory in cls.filter_specs:
- if test(f):
- return factory(f, request, params, model, model_admin,
- field_path=field_path)
- create = classmethod(create)
- def has_output(self):
- return True
- def choices(self, cl):
- raise NotImplementedError()
- def title(self):
- return self.field.verbose_name
- def output(self, cl):
- t = []
- if self.has_output():
- t.append(_(u'<h3>By %s:</h3>\n<ul>\n') % escape(self.title()))
- for choice in self.choices(cl):
- t.append(u'<li%s><a href="%s">%s</a></li>\n' % \
- ((choice['selected'] and ' class="selected"' or ''),
- iri_to_uri(choice['query_string']),
- choice['display']))
- t.append('</ul>\n\n')
- return mark_safe("".join(t))
- class obj:
- def __init__(self, name):
- self.pk = name
- # this looks weird, but whatever is using it seems to work this way
- def __str__(self):
- return self.pk
- class DistantFieldFilterSpec(FilterSpec):
- def __init__(self, filter_term, request, params, model, model_admin, field_path=None):
- super(DistantFieldFilterSpec, self).__init__(None, request, params, model, model_admin, field_path=field_path)
- self.displaymulti=True
- assert '__' in filter_term
- filter_parts=filter_term.split("__")
- # Go through the filter parts (seperated by __) and traverse the model structure to find the final model to do a lookup on.
- mod = model
- self.endfield = None # does it end with a non joining field.
- for part in filter_parts:
- #print "PART",part
- fld = mod._meta.get_field_by_name(part)[0]
- if fld.rel:
- mod = fld.rel.to
- else:
- self.endfield = part
- #print "ENDFIELD",self.endfield
- getkeys = request.GET.keys()
- # if our filter term passed in ends in "exact"
- exact=True
- for term in getkeys:
- if term.startswith(filter_term):
- #this may be the filter we are interested in
- extension = term[len(filter_term):]
- if extension=="__in":
- exact = False
- #print "EXACT",exact
- self.lookup_title = mod._meta.verbose_name
- if exact:
- self.lookup_kwarg = '%s__exact' % filter_term
- #self.multitag = '%s__in' % filter_term + "="+str(request.GET.get(self.lookup_kwarg, None))
- self.multitag = request.META['QUERY_STRING'].replace(self.lookup_kwarg,self.lookup_kwarg.replace('__exact','__in'))
- self.multiselect = False
- else:
- self.lookup_kwarg = '%s__in' % filter_term
- self.multiselect = True
- # the multi link tag here has to be the full hyperlink, but with the present filters condition removed.
- outterms=[]
- for term in getkeys:
- if term.startswith(filter_term):
- #this may be the filter we are interested in
- extension = term[len(filter_term):]
- if extension!="__in":
- #this is not us. We should add it so we remember it
- outterms.append(term)
- else:
- outterms.append(term)
- self.multitag = "&".join([term+"="+request.GET[term] for term in outterms])
- self.lookup_val = request.GET.get(self.lookup_kwarg, None)
- if self.endfield==None:
- self.lookup_choices = mod.objects.all()
- else:
- # lets isolate the unique set of that field
- query = mod.objects.all()
- names=set([getattr(q,self.endfield) for q in query])
- self.lookup_choices = []
- for name in names:
- self.lookup_choices.append( obj(name) )
- # if this is None then "All" is selected. This means our "multi" link must switch EVERY filter option on.
- if self.lookup_val is None:
- # construct the 'in' list
- inlist = [a.pk for a in self.lookup_choices]
- self.multitag = "&".join([term+"="+request.GET[term] for term in getkeys])+"&"+self.lookup_kwarg.replace("__exact","__in")+"="+",".join([str(x) for x in inlist])
- #print self.lookup_kwarg,",",self.lookup_val,",",self.lookup_choices
- def has_output(self):
- return len(self.lookup_choices) > 1
- def title(self):
- return self.lookup_title
- def choices(self, cl):
- if not self.multiselect:
- yield {'selected': self.lookup_val is None,
- 'query_string': cl.get_query_string({}, [self.lookup_kwarg]),
- 'display': _('All')}
- lookupvals = []
- if self.multiselect and self.lookup_val != None:
- lookupvals = [smart_unicode(x) for x in self.lookup_val.split(",")]
- #print "LV:",lookupvals,"choices:",self.lookup_choices
- for val in sorted(self.lookup_choices,key=lambda x: str(x)):
- pk_val = self.lookup_val
- selected = False
- if self.multiselect:
- selected = smart_unicode(val.pk) in lookupvals
- else:
- selected = self.lookup_val == smart_unicode(val.pk)
- #print selected,smart_unicode(val.pk),lookupvals,self.lookup_kwarg,pk_val
- if self.endfield==None:
- v = val
- else:
- v= val.pk
- query=cl.get_query_string({self.lookup_kwarg: pk_val})
- #print query
- if pk_val!=None:
- if not selected:
- #print "A"
- if self.multiselect:
- query=cl.get_query_string({self.lookup_kwarg: smart_unicode(pk_val+u","+smart_unicode(val.pk))})
- else:
- query=cl.get_query_string({self.lookup_kwarg: smart_unicode(smart_unicode(val.pk))})
- else:
- #print "B"
- if self.multiselect:
- query=cl.get_query_string({self.lookup_kwarg: smart_unicode(",".join([x for x in pk_val.split(",") if x!=unicode(val.pk)]))})
- else:
- query=cl.get_query_string({self.lookup_kwarg: smart_unicode(smart_unicode(val.pk))})
- else:
- #print "C"
- query=cl.get_query_string({self.lookup_kwarg: str(val.pk)})
- yield {'selected': selected,
- 'query_string': query,
- 'display': v,
- }
- class FieldFilterSpec(FilterSpec):
- pass
- class RangeFilterSpec(FieldFilterSpec):
- HAS_OWN_OUTPUT = True
- def __init__(self, filter_term, f, request, params, model, model_admin, prefix="", field_path=None):
- super(RangeFilterSpec, self).__init__(f, request, params, model, model_admin, field_path=field_path)
- #print f, params, model, model_admin
- #assert isinstance(f, models.IntegerField) or isinstance(f, models.FloatField)
- self.lookup_title = f.verbose_name
- self.form_name = "range_%s"%f.name
- self.from_key = prefix+f.name+"__gte"
- self.to_key = prefix+f.name+"__lte"
- self.range_active = self.from_key in request.GET
- #print model.objects.all()
- self.min_value = request.GET[self.from_key] if self.range_active else ""
- self.max_value = request.GET[self.to_key] if self.range_active else ""
- self.querycopy=request.GET.copy()
- if self.range_active:
- #calculate the get line without these tags
- if self.from_key in self.querycopy:
- del self.querycopy[self.from_key]
- if self.to_key in self.querycopy:
- del self.querycopy[self.to_key]
- def has_output(self):
- return True
- def title(self):
- return "%s range"%self.lookup_title
- def choices(self, cl):
- raise NotImplementedError()
- def output(self, cl):
- t = []
- if self.has_output():
- t.append(_(u'<h3>By %s:</h3>\n') % escape(self.title()))
- t.append("<ul>\n")
- t.append(_(u'<li%s><a href=".?%s">All</a></li>\n'%(' class="selected"' if not self.range_active else '',self.querycopy.urlencode())))
- t.append(_(u'<li%s><form method="GET" action="." name="%s">%s<input name="%s" value="%s" size="3"/> - <input name="%s" value="%s" size="3"/> <a href="#" onClick="%s.submit();">Go</a></form></li>\n'%(' class="selected"' if self.range_active else '',self.form_name, cl.build_hidden_input_tags(), self.from_key, self.min_value, self.to_key, self.max_value, self.form_name)))
- t.append("</ul>\n")
- return mark_safe("".join(t))
- class RelatedFilterSpec(FilterSpec):
- def __init__(self, f, request, params, model, model_admin,
- field_path=None):
- super(RelatedFilterSpec, self).__init__(
- f, request, params, model, model_admin, field_path=field_path)
- other_model = get_model_from_relation(f)
- if isinstance(f, (models.ManyToManyField,
- models.related.RelatedObject)):
- # no direct field on this model, get name from other model
- self.lookup_title = other_model._meta.verbose_name
- else:
- self.lookup_title = f.verbose_name # use field name
- rel_name = other_model._meta.pk.name
- self.lookup_kwarg = '%s__%s__exact' % (self.field_path, rel_name)
- self.lookup_kwarg_isnull = '%s__isnull' % (self.field_path)
- self.lookup_val = request.GET.get(self.lookup_kwarg, None)
- self.lookup_val_isnull = request.GET.get(
- self.lookup_kwarg_isnull, None)
- self.lookup_choices = f.get_choices(include_blank=False)
- def has_output(self):
- if isinstance(self.field, models.related.RelatedObject) \
- and self.field.field.null or hasattr(self.field, 'rel') \
- and self.field.null:
- extra = 1
- else:
- extra = 0
- return len(self.lookup_choices) + extra > 1
- def title(self):
- return self.lookup_title
- def choices(self, cl):
- from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE
- yield {'selected': self.lookup_val is None
- and not self.lookup_val_isnull,
- 'query_string': cl.get_query_string(
- {},
- [self.lookup_kwarg, self.lookup_kwarg_isnull]),
- 'display': _('All')}
- for pk_val, val in self.lookup_choices:
- yield {'selected': self.lookup_val == smart_unicode(pk_val),
- 'query_string': cl.get_query_string(
- {self.lookup_kwarg: pk_val},
- [self.lookup_kwarg_isnull]),
- 'display': val}
- if isinstance(self.field, models.related.RelatedObject) \
- and self.field.field.null or hasattr(self.field, 'rel') \
- and self.field.null:
- yield {'selected': bool(self.lookup_val_isnull),
- 'query_string': cl.get_query_string(
- {self.lookup_kwarg_isnull: 'True'},
- [self.lookup_kwarg]),
- 'display': EMPTY_CHANGELIST_VALUE}
- FilterSpec.register(lambda f: (
- hasattr(f, 'rel') and bool(f.rel) or
- isinstance(f, models.related.RelatedObject)), RelatedFilterSpec)
- class BooleanFieldFilterSpec(FilterSpec):
- def __init__(self, f, request, params, model, model_admin,
- field_path=None):
- super(BooleanFieldFilterSpec, self).__init__(f, request, params, model,
- model_admin,
- field_path=field_path)
- self.lookup_kwarg = '%s__exact' % self.field_path
- self.lookup_kwarg2 = '%s__isnull' % self.field_path
- self.lookup_val = request.GET.get(self.lookup_kwarg, None)
- self.lookup_val2 = request.GET.get(self.lookup_kwarg2, None)
- def title(self):
- return self.field.verbose_name
- def choices(self, cl):
- for k, v in ((_('All'), None), (_('Yes'), '1'), (_('No'), '0')):
- yield {'selected': self.lookup_val == v and not self.lookup_val2,
- 'query_string': cl.get_query_string(
- {self.lookup_kwarg: v},
- [self.lookup_kwarg2]),
- 'display': k}
- if isinstance(self.field, models.NullBooleanField):
- yield {'selected': self.lookup_val2 == 'True',
- 'query_string': cl.get_query_string(
- {self.lookup_kwarg2: 'True'},
- [self.lookup_kwarg]),
- 'display': _('Unknown')}
- FilterSpec.register(lambda f: isinstance(f, models.BooleanField)
- or isinstance(f, models.NullBooleanField),
- BooleanFieldFilterSpec)
- class ChoicesFilterSpec(FilterSpec):
- def __init__(self, f, request, params, model, model_admin,
- field_path=None):
- super(ChoicesFilterSpec, self).__init__(f, request, params, model,
- model_admin,
- field_path=field_path)
- self.lookup_kwarg = '%s__exact' % self.field_path
- self.lookup_val = request.GET.get(self.lookup_kwarg, None)
- def choices(self, cl):
- yield {'selected': self.lookup_val is None,
- 'query_string': cl.get_query_string({}, [self.lookup_kwarg]),
- 'display': _('All')}
- for k, v in self.field.flatchoices:
- yield {'selected': smart_unicode(k) == self.lookup_val,
- 'query_string': cl.get_query_string(
- {self.lookup_kwarg: k}),
- 'display': v}
- FilterSpec.register(lambda f: bool(f.choices), ChoicesFilterSpec)
- class DateFieldFilterSpec(FilterSpec):
- def __init__(self, f, request, params, model, model_admin,
- field_path=None):
- super(DateFieldFilterSpec, self).__init__(f, request, params, model,
- model_admin,
- field_path=field_path)
- self.field_generic = '%s__' % self.field_path
- self.date_params = dict([(k, v) for k, v in params.items()
- if k.startswith(self.field_generic)])
- today = datetime.date.today()
- one_week_ago = today - datetime.timedelta(days=7)
- today_str = isinstance(self.field, models.DateTimeField) \
- and today.strftime('%Y-%m-%d 23:59:59') \
- or today.strftime('%Y-%m-%d')
- self.links = (
- (_('Any date'), {}),
- (_('Today'), {'%s__year' % self.field_path: str(today.year),
- '%s__month' % self.field_path: str(today.month),
- '%s__day' % self.field_path: str(today.day)}),
- (_('Past 7 days'), {'%s__gte' % self.field_path:
- one_week_ago.strftime('%Y-%m-%d'),
- '%s__lte' % self.field_path: today_str}),
- (_('This month'), {'%s__year' % self.field_path: str(today.year),
- '%s__month' % self.field_path: str(today.month)}),
- (_('This year'), {'%s__year' % self.field_path: str(today.year)})
- )
- def title(self):
- return self.field.verbose_name
- def choices(self, cl):
- for title, param_dict in self.links:
- yield {'selected': self.date_params == param_dict,
- 'query_string': cl.get_query_string(
- param_dict,
- [self.field_generic]),
- 'display': title}
- FilterSpec.register(lambda f: isinstance(f, models.DateField),
- DateFieldFilterSpec)
- # This should be registered last, because it's a last resort. For example,
- # if a field is eligible to use the BooleanFieldFilterSpec, that'd be much
- # more appropriate, and the AllValuesFilterSpec won't get used for it.
- class AllValuesFilterSpec(FilterSpec):
- def __init__(self, f, request, params, model, model_admin,
- field_path=None):
- super(AllValuesFilterSpec, self).__init__(f, request, params, model,
- model_admin,
- field_path=field_path)
- self.lookup_kwarg = self.field_path
- self.lookup_kwarg_isnull = '%s__isnull' % self.field_path
- self.lookup_val = request.GET.get(self.lookup_kwarg, None)
- self.lookup_val_isnull = request.GET.get(self.lookup_kwarg_isnull,
- None)
- parent_model, reverse_path = reverse_field_path(model, self.field_path)
- queryset = parent_model._default_manager.all()
- # optional feature: limit choices base on existing relationships
- # queryset = queryset.complex_filter(
- # {'%s__isnull' % reverse_path: False})
- limit_choices_to = get_limit_choices_to_from_path(model, field_path)
- queryset = queryset.filter(limit_choices_to)
- self.lookup_choices = \
- queryset.distinct().order_by(f.name).values_list(f.name, flat=True)
- def title(self):
- return self.field.verbose_name
- def choices(self, cl):
- from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE
- yield {'selected': self.lookup_val is None
- and self.lookup_val_isnull is None,
- 'query_string': cl.get_query_string(
- {},
- [self.lookup_kwarg, self.lookup_kwarg_isnull]),
- 'display': _('All')}
- include_none = False
- for val in self.lookup_choices:
- if val is None:
- include_none = True
- continue
- val = smart_unicode(val)
- yield {'selected': self.lookup_val == val,
- 'query_string': cl.get_query_string(
- {self.lookup_kwarg: val},
- [self.lookup_kwarg_isnull]),
- 'display': val}
- if include_none:
- yield {'selected': bool(self.lookup_val_isnull),
- 'query_string': cl.get_query_string(
- {self.lookup_kwarg_isnull: 'True'},
- [self.lookup_kwarg]),
- 'display': EMPTY_CHANGELIST_VALUE}
- FilterSpec.register(lambda f: True, AllValuesFilterSpec)