New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added generic class based search views #1130
Merged
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
da7cc0b
added generic class based search views.
troygrosfield fc027ba
Added unicode_literals import for py 2/3 compat.
troygrosfield 81b845a
test for the elasticsearch client.
troygrosfield 4869be0
Adding py34-django1.7 to travis.
troygrosfield 99fb662
Adding fix for previously test for django 1.7.
troygrosfield 4a200db
fix for django version check.
troygrosfield 721b942
Hopefully last fix for django version checking.
troygrosfield b12d5b4
Added tests for the generic search view.
troygrosfield 85ff8f3
minor update.
troygrosfield 22f6437
removed "expectedFailure".
troygrosfield File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
from __future__ import unicode_literals | ||
|
||
from django.conf import settings | ||
from django.core.paginator import Paginator | ||
from django.views.generic import FormView | ||
from django.views.generic.edit import FormMixin | ||
from django.views.generic.list import MultipleObjectMixin | ||
|
||
from .forms import FacetedSearchForm | ||
from .forms import ModelSearchForm | ||
from .query import SearchQuerySet | ||
|
||
|
||
RESULTS_PER_PAGE = getattr(settings, 'HAYSTACK_SEARCH_RESULTS_PER_PAGE', 20) | ||
|
||
|
||
class SearchMixin(MultipleObjectMixin, FormMixin): | ||
""" | ||
A mixin that allows adding in Haystacks search functionality into | ||
another view class. | ||
|
||
This mixin exhibits similar end functionality as the base Haystack search | ||
view, but with some important distinctions oriented around greater | ||
compatibility with Django's built-in class based views and mixins. | ||
|
||
Normal flow: | ||
|
||
self.request = request | ||
|
||
self.form = self.build_form() | ||
self.query = self.get_query() | ||
self.results = self.get_results() | ||
|
||
return self.create_response() | ||
|
||
This mixin should: | ||
|
||
1. Make the form | ||
2. Get the queryset | ||
3. Return the paginated queryset | ||
|
||
""" | ||
template_name = 'search/search.html' | ||
load_all = True | ||
form_class = ModelSearchForm | ||
queryset = SearchQuerySet() | ||
context_object_name = None | ||
paginate_by = RESULTS_PER_PAGE | ||
paginate_orphans = 0 | ||
paginator_class = Paginator | ||
page_kwarg = 'page' | ||
form_name = 'form' | ||
search_field = 'q' | ||
object_list = None | ||
|
||
def get_form_kwargs(self): | ||
""" | ||
Returns the keyword arguments for instantiating the form. | ||
""" | ||
kwargs = {'initial': self.get_initial()} | ||
if self.request.method == 'GET': | ||
kwargs.update({ | ||
'data': self.request.GET, | ||
}) | ||
kwargs.update({'searchqueryset': self.get_queryset()}) | ||
return kwargs | ||
|
||
def form_invalid(self, form): | ||
context = self.get_context_data(**{ | ||
self.form_name: form, | ||
'object_list': self.get_queryset() | ||
}) | ||
return self.render_to_response(context) | ||
|
||
def form_valid(self, form): | ||
self.queryset = form.search() | ||
context = self.get_context_data(**{ | ||
self.form_name: form, | ||
'query': form.cleaned_data.get(self.search_field), | ||
'object_list': self.queryset | ||
}) | ||
return self.render_to_response(context) | ||
|
||
|
||
class FacetedSearchMixin(SearchMixin): | ||
""" | ||
A mixin that allows adding in a Haystack search functionality with search | ||
faceting. | ||
""" | ||
form_class = FacetedSearchForm | ||
|
||
def get_form_kwargs(self): | ||
kwargs = super(SearchMixin, self).get_form_kwargs() | ||
kwargs.update({ | ||
'selected_facets': self.request.GET.getlist("selected_facets") | ||
}) | ||
return kwargs | ||
|
||
def get_context_data(self, **kwargs): | ||
context = super(FacetedSearchMixin, self).get_context_data(**kwargs) | ||
context.update({'facets': self.results.facet_counts()}) | ||
return context | ||
|
||
|
||
class SearchView(SearchMixin, FormView): | ||
"""A view class for searching a Haystack managed search index""" | ||
|
||
def get(self, request, *args, **kwargs): | ||
""" | ||
Handles GET requests and instantiates a blank version of the form. | ||
""" | ||
form_class = self.get_form_class() | ||
form = self.get_form(form_class) | ||
|
||
if form.is_valid(): | ||
return self.form_valid(form) | ||
else: | ||
return self.form_invalid(form) | ||
|
||
|
||
class FacetedSearchView(FacetedSearchMixin, SearchView): | ||
""" | ||
A view class for searching a Haystack managed search index with | ||
facets | ||
""" | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://docs.djangoproject.com/en/1.7/releases/1.7/#introspecting-applications
This is also the first part of the fix to #1069.