PageRenderTime 324ms CodeModel.GetById 56ms app.highlight 123ms RepoModel.GetById 72ms app.codeStats 34ms

/django/contrib/admin/filterspecs.py

https://code.google.com/p/mango-py/
Python | 477 lines | 475 code | 0 blank | 2 comment | 0 complexity | f35e4b32449438eccdea1fca432ff0e3 MD5 | raw file
  1"""
  2FilterSpec encapsulates the logic for displaying filters in the Django admin.
  3Filters are specified in models with the "list_filter" option.
  4
  5Each filter subclass knows how to display a filter for a field that passes a
  6certain test -- e.g. being a DateField or ForeignKey.
  7"""
  8
  9from django.db import models
 10from django.utils.encoding import smart_unicode, iri_to_uri
 11from django.utils.translation import ugettext as _
 12from django.utils.html import escape
 13from django.utils.safestring import mark_safe
 14from django.contrib.admin.util import get_model_from_relation, \
 15    reverse_field_path, get_limit_choices_to_from_path
 16import datetime
 17
 18class FilterSpec(object):
 19    HAS_OWN_OUTPUT = False
 20    filter_specs = []
 21    def __init__(self, f, request, params, model, model_admin,
 22                 field_path=None):
 23        self.field = f
 24        self.params = params
 25        self.displaymulti = False
 26        self.multiselect=False
 27        self.multitag=''
 28        self.field_path = field_path
 29        if field_path is None:
 30            if isinstance(f, models.related.RelatedObject):
 31                self.field_path = f.var_name
 32            else:
 33                self.field_path = f.name
 34
 35    def register(cls, test, factory):
 36        cls.filter_specs.append((test, factory))
 37    register = classmethod(register)
 38
 39    def create(cls, f, request, params, model, model_admin, field_path=None):
 40        for test, factory in cls.filter_specs:
 41            if test(f):
 42                return factory(f, request, params, model, model_admin,
 43                               field_path=field_path)
 44    create = classmethod(create)
 45
 46    def has_output(self):
 47        return True
 48
 49    def choices(self, cl):
 50        raise NotImplementedError()
 51
 52    def title(self):
 53        return self.field.verbose_name
 54
 55    def output(self, cl):
 56        t = []
 57        if self.has_output():
 58            t.append(_(u'<h3>By %s:</h3>\n<ul>\n') % escape(self.title()))
 59
 60            for choice in self.choices(cl):
 61                t.append(u'<li%s><a href="%s">%s</a></li>\n' % \
 62                    ((choice['selected'] and ' class="selected"' or ''),
 63                     iri_to_uri(choice['query_string']),
 64                     choice['display']))
 65            t.append('</ul>\n\n')
 66        return mark_safe("".join(t))
 67
 68class obj:
 69    def __init__(self, name):
 70        self.pk = name
 71        # this looks weird, but whatever is using it seems to work this way
 72        def __str__(self):
 73            return self.pk
 74
 75class DistantFieldFilterSpec(FilterSpec):
 76	def __init__(self, filter_term, request, params, model, model_admin, field_path=None):
 77		super(DistantFieldFilterSpec, self).__init__(None, request, params, model, model_admin, field_path=field_path)
 78
 79		self.displaymulti=True
 80
 81		assert '__' in filter_term
 82
 83		filter_parts=filter_term.split("__")
 84
 85		# Go through the filter parts (seperated by __) and traverse the model structure to find the final model to do a lookup on.
 86		mod = model
 87		self.endfield = None			# does it end with a non joining field.
 88		for part in filter_parts:
 89			#print "PART",part
 90			fld = mod._meta.get_field_by_name(part)[0]
 91			if fld.rel:
 92				mod = fld.rel.to
 93			else:
 94				self.endfield = part
 95
 96		#print "ENDFIELD",self.endfield
 97		getkeys = request.GET.keys()
 98
 99		# if our filter term passed in ends in "exact"
100		exact=True
101		for term in getkeys:
102			if term.startswith(filter_term):
103				#this may be the filter we are interested in
104				extension = term[len(filter_term):]
105				if extension=="__in":
106					exact = False
107
108		#print "EXACT",exact
109
110		self.lookup_title = mod._meta.verbose_name
111
112		if exact:
113			self.lookup_kwarg = '%s__exact' % filter_term
114			#self.multitag = '%s__in' % filter_term + "="+str(request.GET.get(self.lookup_kwarg, None))
115			self.multitag = request.META['QUERY_STRING'].replace(self.lookup_kwarg,self.lookup_kwarg.replace('__exact','__in'))
116			self.multiselect = False
117		else:
118			self.lookup_kwarg = '%s__in' % filter_term
119			self.multiselect = True
120
121			# the multi link tag here has to be the full hyperlink, but with the present filters condition removed.
122			outterms=[]
123			for term in getkeys:
124				if term.startswith(filter_term):
125					#this may be the filter we are interested in
126					extension = term[len(filter_term):]
127					if extension!="__in":
128						#this is not us. We should add it so we remember it
129						outterms.append(term)
130				else:
131					outterms.append(term)
132			self.multitag = "&".join([term+"="+request.GET[term] for term in outterms])
133
134		self.lookup_val = request.GET.get(self.lookup_kwarg, None)
135
136		if self.endfield==None:
137			self.lookup_choices = mod.objects.all()
138		else:
139			# lets isolate the unique set of that field
140			query = mod.objects.all()
141			names=set([getattr(q,self.endfield) for q in query])
142			self.lookup_choices = []
143			for name in names:
144				self.lookup_choices.append( obj(name) )
145
146
147		# if this is None then "All" is selected. This means our "multi" link must switch EVERY filter option on.
148		if self.lookup_val is None:
149			# construct the 'in' list
150			inlist = [a.pk for a in self.lookup_choices]
151			self.multitag = "&".join([term+"="+request.GET[term] for term in getkeys])+"&"+self.lookup_kwarg.replace("__exact","__in")+"="+",".join([str(x) for x in inlist])
152
153		#print self.lookup_kwarg,",",self.lookup_val,",",self.lookup_choices
154
155	def has_output(self):
156		return len(self.lookup_choices) > 1
157
158	def title(self):
159		return self.lookup_title
160
161	def choices(self, cl):
162		if not self.multiselect:
163			yield {'selected': self.lookup_val is None,
164				'query_string': cl.get_query_string({}, [self.lookup_kwarg]),
165				'display': _('All')}
166
167		lookupvals = []
168		if self.multiselect and self.lookup_val != None:
169			lookupvals = [smart_unicode(x) for x in self.lookup_val.split(",")]
170
171		#print "LV:",lookupvals,"choices:",self.lookup_choices
172
173		for val in sorted(self.lookup_choices,key=lambda x: str(x)):
174			pk_val = self.lookup_val
175			selected = False
176
177			if self.multiselect:
178				selected = smart_unicode(val.pk) in lookupvals
179			else:
180				selected = self.lookup_val == smart_unicode(val.pk)
181			#print selected,smart_unicode(val.pk),lookupvals,self.lookup_kwarg,pk_val
182			if self.endfield==None:
183				v = val
184			else:
185				v= val.pk
186			query=cl.get_query_string({self.lookup_kwarg: pk_val})
187			#print query
188			if pk_val!=None:
189				if not selected:
190					#print "A"
191					if self.multiselect:
192						query=cl.get_query_string({self.lookup_kwarg: smart_unicode(pk_val+u","+smart_unicode(val.pk))})
193					else:
194						query=cl.get_query_string({self.lookup_kwarg: smart_unicode(smart_unicode(val.pk))})
195				else:
196					#print "B"
197					if self.multiselect:
198						query=cl.get_query_string({self.lookup_kwarg: smart_unicode(",".join([x for x in pk_val.split(",") if x!=unicode(val.pk)]))})
199					else:
200						query=cl.get_query_string({self.lookup_kwarg: smart_unicode(smart_unicode(val.pk))})
201			else:
202				#print "C"
203				query=cl.get_query_string({self.lookup_kwarg: str(val.pk)})
204
205			yield {'selected': selected,
206					'query_string': query,
207					'display': v,
208					}
209
210class FieldFilterSpec(FilterSpec):
211	pass
212
213class RangeFilterSpec(FieldFilterSpec):
214	HAS_OWN_OUTPUT = True
215
216	def __init__(self, filter_term, f, request, params, model, model_admin, prefix="", field_path=None):
217		super(RangeFilterSpec, self).__init__(f, request, params, model, model_admin, field_path=field_path)
218		#print f, params, model, model_admin
219
220		#assert isinstance(f, models.IntegerField) or isinstance(f, models.FloatField)
221		self.lookup_title = f.verbose_name
222
223		self.form_name = "range_%s"%f.name
224		self.from_key = prefix+f.name+"__gte"
225		self.to_key = prefix+f.name+"__lte"
226
227		self.range_active = self.from_key in request.GET
228
229		#print model.objects.all()
230		self.min_value = request.GET[self.from_key] if self.range_active else ""
231		self.max_value = request.GET[self.to_key] if self.range_active else ""
232
233		self.querycopy=request.GET.copy()
234		if self.range_active:
235			#calculate the get line without these tags
236			if self.from_key in self.querycopy:
237				del self.querycopy[self.from_key]
238			if self.to_key in self.querycopy:
239				del self.querycopy[self.to_key]
240
241
242
243	def has_output(self):
244		return True
245
246	def title(self):
247		return "%s range"%self.lookup_title
248
249	def choices(self, cl):
250		raise NotImplementedError()
251
252	def output(self, cl):
253		t = []
254		if self.has_output():
255			t.append(_(u'<h3>By %s:</h3>\n') % escape(self.title()))
256			t.append("<ul>\n")
257			t.append(_(u'<li%s><a href=".?%s">All</a></li>\n'%(' class="selected"' if not self.range_active else '',self.querycopy.urlencode())))
258			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"/>&nbsp;&nbsp;&nbsp;<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)))
259			t.append("</ul>\n")
260		return mark_safe("".join(t))
261
262class RelatedFilterSpec(FilterSpec):
263    def __init__(self, f, request, params, model, model_admin,
264                 field_path=None):
265        super(RelatedFilterSpec, self).__init__(
266            f, request, params, model, model_admin, field_path=field_path)
267
268        other_model = get_model_from_relation(f)
269        if isinstance(f, (models.ManyToManyField,
270                          models.related.RelatedObject)):
271            # no direct field on this model, get name from other model
272            self.lookup_title = other_model._meta.verbose_name
273        else:
274            self.lookup_title = f.verbose_name # use field name
275        rel_name = other_model._meta.pk.name
276        self.lookup_kwarg = '%s__%s__exact' % (self.field_path, rel_name)
277        self.lookup_kwarg_isnull = '%s__isnull' % (self.field_path)
278        self.lookup_val = request.GET.get(self.lookup_kwarg, None)
279        self.lookup_val_isnull = request.GET.get(
280                                      self.lookup_kwarg_isnull, None)
281        self.lookup_choices = f.get_choices(include_blank=False)
282
283    def has_output(self):
284        if isinstance(self.field, models.related.RelatedObject) \
285           and self.field.field.null or hasattr(self.field, 'rel') \
286           and self.field.null:
287            extra = 1
288        else:
289            extra = 0
290        return len(self.lookup_choices) + extra > 1
291
292    def title(self):
293        return self.lookup_title
294
295    def choices(self, cl):
296        from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE
297        yield {'selected': self.lookup_val is None
298                           and not self.lookup_val_isnull,
299               'query_string': cl.get_query_string(
300                               {},
301                               [self.lookup_kwarg, self.lookup_kwarg_isnull]),
302               'display': _('All')}
303        for pk_val, val in self.lookup_choices:
304            yield {'selected': self.lookup_val == smart_unicode(pk_val),
305                   'query_string': cl.get_query_string(
306                                   {self.lookup_kwarg: pk_val},
307                                   [self.lookup_kwarg_isnull]),
308                   'display': val}
309        if isinstance(self.field, models.related.RelatedObject) \
310           and self.field.field.null or hasattr(self.field, 'rel') \
311           and self.field.null:
312            yield {'selected': bool(self.lookup_val_isnull),
313                   'query_string': cl.get_query_string(
314                                   {self.lookup_kwarg_isnull: 'True'},
315                                   [self.lookup_kwarg]),
316                   'display': EMPTY_CHANGELIST_VALUE}
317
318FilterSpec.register(lambda f: (
319        hasattr(f, 'rel') and bool(f.rel) or
320        isinstance(f, models.related.RelatedObject)), RelatedFilterSpec)
321
322class BooleanFieldFilterSpec(FilterSpec):
323    def __init__(self, f, request, params, model, model_admin,
324                 field_path=None):
325        super(BooleanFieldFilterSpec, self).__init__(f, request, params, model,
326                                                     model_admin,
327                                                     field_path=field_path)
328        self.lookup_kwarg = '%s__exact' % self.field_path
329        self.lookup_kwarg2 = '%s__isnull' % self.field_path
330        self.lookup_val = request.GET.get(self.lookup_kwarg, None)
331        self.lookup_val2 = request.GET.get(self.lookup_kwarg2, None)
332
333    def title(self):
334        return self.field.verbose_name
335
336    def choices(self, cl):
337        for k, v in ((_('All'), None), (_('Yes'), '1'), (_('No'), '0')):
338            yield {'selected': self.lookup_val == v and not self.lookup_val2,
339                   'query_string': cl.get_query_string(
340                                   {self.lookup_kwarg: v},
341                                   [self.lookup_kwarg2]),
342                   'display': k}
343        if isinstance(self.field, models.NullBooleanField):
344            yield {'selected': self.lookup_val2 == 'True',
345                   'query_string': cl.get_query_string(
346                                   {self.lookup_kwarg2: 'True'},
347                                   [self.lookup_kwarg]),
348                   'display': _('Unknown')}
349
350FilterSpec.register(lambda f: isinstance(f, models.BooleanField)
351                              or isinstance(f, models.NullBooleanField),
352                                 BooleanFieldFilterSpec)
353
354class ChoicesFilterSpec(FilterSpec):
355    def __init__(self, f, request, params, model, model_admin,
356                 field_path=None):
357        super(ChoicesFilterSpec, self).__init__(f, request, params, model,
358                                                model_admin,
359                                                field_path=field_path)
360        self.lookup_kwarg = '%s__exact' % self.field_path
361        self.lookup_val = request.GET.get(self.lookup_kwarg, None)
362
363    def choices(self, cl):
364        yield {'selected': self.lookup_val is None,
365               'query_string': cl.get_query_string({}, [self.lookup_kwarg]),
366               'display': _('All')}
367        for k, v in self.field.flatchoices:
368            yield {'selected': smart_unicode(k) == self.lookup_val,
369                    'query_string': cl.get_query_string(
370                                    {self.lookup_kwarg: k}),
371                    'display': v}
372
373FilterSpec.register(lambda f: bool(f.choices), ChoicesFilterSpec)
374
375class DateFieldFilterSpec(FilterSpec):
376    def __init__(self, f, request, params, model, model_admin,
377                 field_path=None): 
378        super(DateFieldFilterSpec, self).__init__(f, request, params, model,
379                                                  model_admin,
380                                                  field_path=field_path)
381
382        self.field_generic = '%s__' % self.field_path
383
384        self.date_params = dict([(k, v) for k, v in params.items()
385                                 if k.startswith(self.field_generic)])
386
387        today = datetime.date.today()
388        one_week_ago = today - datetime.timedelta(days=7)
389        today_str = isinstance(self.field, models.DateTimeField) \
390                    and today.strftime('%Y-%m-%d 23:59:59') \
391                    or today.strftime('%Y-%m-%d')
392
393        self.links = (
394            (_('Any date'), {}),
395            (_('Today'), {'%s__year' % self.field_path: str(today.year),
396                       '%s__month' % self.field_path: str(today.month),
397                       '%s__day' % self.field_path: str(today.day)}),
398            (_('Past 7 days'), {'%s__gte' % self.field_path:
399                                    one_week_ago.strftime('%Y-%m-%d'),
400                             '%s__lte' % self.field_path: today_str}),
401            (_('This month'), {'%s__year' % self.field_path: str(today.year),
402                             '%s__month' % self.field_path: str(today.month)}),
403            (_('This year'), {'%s__year' % self.field_path: str(today.year)})
404        )
405
406    def title(self):
407        return self.field.verbose_name
408
409    def choices(self, cl):
410        for title, param_dict in self.links:
411            yield {'selected': self.date_params == param_dict,
412                   'query_string': cl.get_query_string(
413                                   param_dict,
414                                   [self.field_generic]),
415                   'display': title}
416
417FilterSpec.register(lambda f: isinstance(f, models.DateField),
418                              DateFieldFilterSpec)
419
420
421# This should be registered last, because it's a last resort. For example,
422# if a field is eligible to use the BooleanFieldFilterSpec, that'd be much
423# more appropriate, and the AllValuesFilterSpec won't get used for it.
424class AllValuesFilterSpec(FilterSpec):
425    def __init__(self, f, request, params, model, model_admin,
426                 field_path=None):
427        super(AllValuesFilterSpec, self).__init__(f, request, params, model,
428                                                  model_admin,
429                                                  field_path=field_path)
430        self.lookup_kwarg = self.field_path
431        self.lookup_kwarg_isnull = '%s__isnull' % self.field_path
432        self.lookup_val = request.GET.get(self.lookup_kwarg, None)
433        self.lookup_val_isnull = request.GET.get(self.lookup_kwarg_isnull,
434                                                 None)
435        parent_model, reverse_path = reverse_field_path(model, self.field_path)
436        queryset = parent_model._default_manager.all()
437        # optional feature: limit choices base on existing relationships
438        # queryset = queryset.complex_filter(
439        #    {'%s__isnull' % reverse_path: False})
440        limit_choices_to = get_limit_choices_to_from_path(model, field_path)
441        queryset = queryset.filter(limit_choices_to)
442
443        self.lookup_choices = \
444            queryset.distinct().order_by(f.name).values_list(f.name, flat=True)
445
446    def title(self):
447        return self.field.verbose_name
448
449    def choices(self, cl):
450        from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE
451        yield {'selected': self.lookup_val is None
452                           and self.lookup_val_isnull is None,
453               'query_string': cl.get_query_string(
454                               {},
455                               [self.lookup_kwarg, self.lookup_kwarg_isnull]),
456               'display': _('All')}
457        include_none = False
458
459        for val in self.lookup_choices:
460            if val is None:
461                include_none = True
462                continue
463            val = smart_unicode(val)
464
465            yield {'selected': self.lookup_val == val,
466                   'query_string': cl.get_query_string(
467                                   {self.lookup_kwarg: val},
468                                   [self.lookup_kwarg_isnull]),
469                   'display': val}
470        if include_none:
471            yield {'selected': bool(self.lookup_val_isnull),
472                    'query_string': cl.get_query_string(
473                                    {self.lookup_kwarg_isnull: 'True'},
474                                    [self.lookup_kwarg]),
475                    'display': EMPTY_CHANGELIST_VALUE}
476
477FilterSpec.register(lambda f: True, AllValuesFilterSpec)