You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

app.py 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. from django.conf.urls import url
  2. from django.conf import settings
  3. from haystack.query import SearchQuerySet
  4. from haystack.views import search_view_factory
  5. from oscar.core.application import Application
  6. from oscar.core.loading import get_class
  7. class SearchApplication(Application):
  8. name = 'search'
  9. search_view = get_class('search.views', 'FacetedSearchView')
  10. search_form = get_class('search.forms', 'SearchForm')
  11. def get_urls(self):
  12. # The form class has to be passed to the __init__ method as that is how
  13. # Haystack works. It's slightly different to normal CBVs.
  14. urlpatterns = [
  15. url(r'^$', search_view_factory(
  16. view_class=self.search_view,
  17. form_class=self.search_form,
  18. searchqueryset=self.get_sqs()),
  19. name='search'),
  20. ]
  21. return self.post_process_urls(urlpatterns)
  22. def get_sqs(self):
  23. """
  24. Return the SQS required by a the Haystack search view
  25. """
  26. # Build SQS based on the OSCAR_SEARCH_FACETS settings
  27. sqs = SearchQuerySet()
  28. for facet in settings.OSCAR_SEARCH_FACETS['fields'].values():
  29. options = facet.get('options', {})
  30. sqs = sqs.facet(facet['field'], **options)
  31. for facet in settings.OSCAR_SEARCH_FACETS['queries'].values():
  32. for query in facet['queries']:
  33. sqs = sqs.query_facet(facet['field'], query[1])
  34. return sqs
  35. application = SearchApplication()