選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

forms.py 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. from django import forms
  2. from django.utils.translation import ugettext_lazy as _
  3. from django.forms.widgets import Input
  4. from haystack.forms import FacetedSearchForm
  5. class SearchInput(Input):
  6. '''
  7. Defining a search type widget
  8. This is an HTML5 thing and works nicely with Safari, other browsers default
  9. back to using the default "text" type
  10. '''
  11. input_type = 'search'
  12. class PriceRangeSearchForm(FacetedSearchForm):
  13. def search(self):
  14. # We replace the 'search' method from FacetedSearchForm, so that we can
  15. # handle range queries
  16. # Note, we call super on a parent class
  17. sqs = super(FacetedSearchForm, self).search()
  18. # We need to process each facet to ensure that the field name and the
  19. # value are quoted correctly and separately:
  20. for facet in self.selected_facets:
  21. if ":" not in facet:
  22. continue
  23. field, value = facet.split(":", 1)
  24. if value:
  25. if field == 'price_exact':
  26. # Don't wrap value in speech marks and don't clean value
  27. sqs = sqs.narrow(u'%s:%s' % (field, value))
  28. else:
  29. sqs = sqs.narrow(u'%s:"%s"' % (field, sqs.query.clean(value)))
  30. return sqs
  31. class MultiFacetedSearchForm(FacetedSearchForm):
  32. '''
  33. An extension of the regular faceted search form to alow for multiple facets
  34. '''
  35. # Use a tabindex of 1 so that users can hit tab on any page and it will
  36. # focus on the search widget.
  37. q = forms.CharField(
  38. required=False, label=_('Search'),
  39. widget=SearchInput({"placeholder": _('Search'),
  40. "tabindex": "1"}))
  41. def search(self):
  42. '''
  43. Overriding the search method to allow for multiple facets
  44. '''
  45. sqs = super(FacetedSearchForm, self).search()
  46. if hasattr(self, 'cleaned_data') and 'selected_facets' in self.cleaned_data:
  47. for f in self.cleaned_data['selected_facets'].split("|"):
  48. sqs = sqs.narrow(f)
  49. return sqs