Browse Source

Merge branch 'feature/search-view' into development

feature/python-error-tracking
Drew Larson 9 years ago
parent
commit
4982094d50
  1. 9
      bikeshop_project/bikeshop/settings/base.py
  2. 9
      bikeshop_project/registration/search_indexes.py
  3. 2
      bikeshop_project/registration/templates/search/indexes/registration/member_text.txt
  4. 3
      bikeshop_project/registration/urls.py
  5. 21
      bikeshop_project/registration/views.py
  6. 2
      requirements/base.txt

9
bikeshop_project/bikeshop/settings/base.py

@ -29,7 +29,7 @@ INSTALLED_APPS = [
'django.contrib.sessions', 'django.contrib.sessions',
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'haystack',
'webpack_loader', 'webpack_loader',
'compressor', 'compressor',
@ -143,3 +143,10 @@ WEBPACK_LOADER = {
'IGNORE': ['.+\.hot-update.js', '.+\.map'] 'IGNORE': ['.+\.hot-update.js', '.+\.map']
} }
} }
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine',
'PATH': os.path.join(os.path.dirname(__file__), 'whoosh_index'),
},
}

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 django.conf.urls import url
from .views import MemberFormView from .views import MemberFormView, MemberSearchView
urlpatterns = [ urlpatterns = [
url(r'^new/$', MemberFormView.as_view(), name='signup'), 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') 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.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.template.response import TemplateResponse
from django.views.generic import View from django.views.generic import View
from django.core.urlresolvers import reverse
import json
from haystack.query import SearchQuerySet
from .forms import MemberForm from .forms import MemberForm
from .models import Member from .models import Member
import logging
logger = logging.getLogger('bikeshop') logger = logging.getLogger('bikeshop')
@ -46,3 +51,13 @@ class MemberFormView(View):
if member: if member:
context['member'] = member context['member'] = member
return TemplateResponse(request, 'member_form.html', context=context) 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')

2
requirements/base.txt

@ -5,3 +5,5 @@ psycopg2==2.6.1
django-libsass==0.6 django-libsass==0.6
django_compressor==2.0 django_compressor==2.0
python-dateutil==2.5.3 python-dateutil==2.5.3
whoosh==2.7.4
git+git://github.com/django-haystack/django-haystack.git
Loading…
Cancel
Save