Browse Source

Basic search endpoint.

feature/python-error-tracking
Drew Larson 9 years ago
parent
commit
99cd30ec25
  1. 9
      bikeshop_project/registration/search_indexes.py
  2. 2
      bikeshop_project/registration/templates/search/indexes/registration/member_text.txt
  3. 3
      bikeshop_project/registration/urls.py
  4. 21
      bikeshop_project/registration/views.py

9
bikeshop_project/registration/search_indexes.py

@ -0,0 +1,9 @@
from haystack import indexes
from .models import Member
class MemberIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.EdgeNgramField(document=True, use_template=True)
def get_model(self):
return Member

2
bikeshop_project/registration/templates/search/indexes/registration/member_text.txt

@ -0,0 +1,2 @@
{{ object.email }}
{{ object.get_full_name }}

3
bikeshop_project/registration/urls.py

@ -1,7 +1,8 @@
from django.conf.urls import url
from .views import MemberFormView
from .views import MemberFormView, MemberSearchView
urlpatterns = [
url(r'^new/$', MemberFormView.as_view(), name='signup'),
url(r'^search/(?P<query>[\w@\.\+]+)/$', MemberSearchView.as_view(), name='member_search'),
url(r'^edit/(?P<member_id>[0-9]+)/$', MemberFormView.as_view(), name='member_edit')
]

21
bikeshop_project/registration/views.py

@ -1,12 +1,17 @@
import logging
from django.contrib import messages
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.http import HttpResponse, HttpResponseRedirect
from django.template.response import TemplateResponse
from django.views.generic import View
from django.core.urlresolvers import reverse
import json
from haystack.query import SearchQuerySet
from .forms import MemberForm
from .models import Member
import logging
logger = logging.getLogger('bikeshop')
@ -46,3 +51,13 @@ class MemberFormView(View):
if member:
context['member'] = member
return TemplateResponse(request, 'member_form.html', context=context)
class MemberSearchView(View):
def get(self, request, query):
sqs = SearchQuerySet().models(Member).autocomplete(text=query)[:5]
results = [dict(name=result.object.get_full_name(), email=result.object.email, id=result.object.id) for result in sqs]
data = json.dumps(dict(results=results))
return HttpResponse(data, content_type='application/json')
Loading…
Cancel
Save