PageRenderTime 31ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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