Working on search
parent
e0b91256e2
commit
8935bdc9cd
|
@ -1,13 +1,21 @@
|
||||||
from django.shortcuts import get_object_or_404, render_to_response
|
from django.shortcuts import get_object_or_404, render_to_response
|
||||||
|
from django.core.paginator import Paginator, InvalidPage
|
||||||
from django.http import Http404, HttpResponseRedirect
|
from django.http import Http404, HttpResponseRedirect
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.template import RequestContext
|
from django.template import RequestContext
|
||||||
from annoying.decorators import render_to
|
from annoying.decorators import render_to
|
||||||
from snipts.models import Favorite, Snipt
|
from snipts.models import Favorite, Snipt
|
||||||
from django.db.models import Count
|
from django.db.models import Count
|
||||||
|
from django.conf import settings
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
from taggit.models import Tag
|
from taggit.models import Tag
|
||||||
|
|
||||||
|
from haystack.forms import ModelSearchForm
|
||||||
|
from haystack.query import EmptySearchQuerySet, SearchQuerySet
|
||||||
|
|
||||||
|
|
||||||
|
RESULTS_PER_PAGE = getattr(settings, 'HAYSTACK_SEARCH_RESULTS_PER_PAGE', 20)
|
||||||
|
|
||||||
def home(request):
|
def home(request):
|
||||||
if request.user.is_authenticated():
|
if request.user.is_authenticated():
|
||||||
return HttpResponseRedirect('/%s/' % request.user.username)
|
return HttpResponseRedirect('/%s/' % request.user.username)
|
||||||
|
@ -140,3 +148,41 @@ def rss(request, context):
|
||||||
context_instance=RequestContext(request),
|
context_instance=RequestContext(request),
|
||||||
mimetype="application/rss+xml"
|
mimetype="application/rss+xml"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def search(request, template='search/search.html', load_all=True, form_class=ModelSearchForm, searchqueryset=None, context_class=RequestContext, extra_context=None, results_per_page=None):
|
||||||
|
query = ''
|
||||||
|
results = EmptySearchQuerySet()
|
||||||
|
|
||||||
|
if request.GET.get('q'):
|
||||||
|
searchqueryset = SearchQuerySet().filter(Q(public=True) | Q(author=request.user)).order_by('-pub_date')
|
||||||
|
form = ModelSearchForm(request.GET, searchqueryset=searchqueryset, load_all=load_all)
|
||||||
|
|
||||||
|
if form.is_valid():
|
||||||
|
query = form.cleaned_data['q']
|
||||||
|
results = form.search()
|
||||||
|
else:
|
||||||
|
form = form_class(searchqueryset=searchqueryset, load_all=load_all)
|
||||||
|
|
||||||
|
paginator = Paginator(results, results_per_page or RESULTS_PER_PAGE)
|
||||||
|
|
||||||
|
try:
|
||||||
|
page = paginator.page(int(request.GET.get('page', 1)))
|
||||||
|
except InvalidPage:
|
||||||
|
raise Http404("No such page of results!")
|
||||||
|
|
||||||
|
context = {
|
||||||
|
'form': form,
|
||||||
|
'page': page,
|
||||||
|
'paginator': paginator,
|
||||||
|
'query': query,
|
||||||
|
'suggestion': None,
|
||||||
|
}
|
||||||
|
|
||||||
|
if results.query.backend.include_spelling:
|
||||||
|
context['suggestion'] = form.get_suggestion()
|
||||||
|
|
||||||
|
if extra_context:
|
||||||
|
context.update(extra_context)
|
||||||
|
|
||||||
|
return render_to_response(template, context, context_instance=context_class(request))
|
||||||
|
|
12
urls.py
12
urls.py
|
@ -1,11 +1,9 @@
|
||||||
from django.views.generic.simple import direct_to_template
|
from django.views.generic.simple import direct_to_template
|
||||||
from registration.forms import RegistrationFormUniqueEmail
|
from registration.forms import RegistrationFormUniqueEmail
|
||||||
from haystack.views import SearchView, search_view_factory
|
|
||||||
from django.http import HttpResponseRedirect
|
from django.http import HttpResponseRedirect
|
||||||
from haystack.forms import ModelSearchForm
|
|
||||||
from haystack.query import SearchQuerySet
|
|
||||||
from django.conf.urls.defaults import *
|
from django.conf.urls.defaults import *
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
|
from snipts.views import search
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
from tastypie.api import Api
|
from tastypie.api import Api
|
||||||
from snipts.api import *
|
from snipts.api import *
|
||||||
|
@ -26,8 +24,6 @@ private_api.register(PrivateTagResource())
|
||||||
private_api.register(PrivateUserResource())
|
private_api.register(PrivateUserResource())
|
||||||
private_api.register(PrivateFavoriteResource())
|
private_api.register(PrivateFavoriteResource())
|
||||||
|
|
||||||
sqs = SearchQuerySet().filter(Q(public=True))
|
|
||||||
|
|
||||||
urlpatterns = patterns('',
|
urlpatterns = patterns('',
|
||||||
|
|
||||||
url(r'^admin/', include(admin.site.urls)),
|
url(r'^admin/', include(admin.site.urls)),
|
||||||
|
@ -42,11 +38,7 @@ urlpatterns = patterns('',
|
||||||
url(r'^api/', include(public_api.urls)),
|
url(r'^api/', include(public_api.urls)),
|
||||||
url(r'^api/', include(private_api.urls)),
|
url(r'^api/', include(private_api.urls)),
|
||||||
|
|
||||||
url(r'^search/$', search_view_factory(
|
url(r'^search/$', search),
|
||||||
view_class=SearchView,
|
|
||||||
searchqueryset=sqs,
|
|
||||||
form_class=ModelSearchForm,
|
|
||||||
), name='haystack_search'),
|
|
||||||
|
|
||||||
url(r'^register/$', lambda x: HttpResponseRedirect('/signup/')),
|
url(r'^register/$', lambda x: HttpResponseRedirect('/signup/')),
|
||||||
url(r'^signup/$',
|
url(r'^signup/$',
|
||||||
|
|
Loading…
Reference in New Issue