From 22d6a3d2cd793028a3b6baad6849e62247fb110a Mon Sep 17 00:00:00 2001 From: Nick Sergeant Date: Tue, 17 Apr 2012 00:42:01 -0400 Subject: [PATCH] Working on search --- requirements.txt | 2 + settings.py | 9 +++++ snipts/search_indexes.py | 18 +++++++++ .../search/indexes/snipts/snipt_text.txt | 5 +++ templates/search/search.html | 39 +++++++++++++++++++ urls.py | 3 ++ 6 files changed, 76 insertions(+) create mode 100644 snipts/search_indexes.py create mode 100644 templates/search/indexes/snipts/snipt_text.txt create mode 100644 templates/search/search.html diff --git a/requirements.txt b/requirements.txt index 1ef5efc..a32d11d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -14,6 +14,7 @@ johnny-cache lxml psycopg2 Pygments +pysolr python-memcached python-postmark pyyaml @@ -22,5 +23,6 @@ uuid versiontools Werkzeug +git+https://github.com/toastdriven/django-haystack.git@master#egg=django-haystack hg+https://bitbucket.org/ubernostrum/django-registration#egg=django-registration git+https://github.com/toastdriven/django-tastypie.git#egg=django-tastypie diff --git a/settings.py b/settings.py index cafb303..cf72503 100644 --- a/settings.py +++ b/settings.py @@ -143,6 +143,7 @@ INSTALLED_APPS = ( 'compressor', 'django_bcrypt', + 'haystack', 'pagination', 'postmark', 'registration', @@ -234,6 +235,14 @@ COMPRESS_PRECOMPILERS = ( ('text/x-scss', 'sass --scss {infile} {outfile}'), ) +# Search +HAYSTACK_CONNECTIONS = { + 'default': { + 'ENGINE': 'haystack.backends.solr_backend.SolrEngine', + 'URL': 'http://127.0.0.1:8983/solr' + }, +} + # Extensions if DEBUG: INSTALLED_APPS += ('django_extensions',) diff --git a/snipts/search_indexes.py b/snipts/search_indexes.py new file mode 100644 index 0000000..8c88bf9 --- /dev/null +++ b/snipts/search_indexes.py @@ -0,0 +1,18 @@ +import datetime +from haystack import indexes +from snipts.models import Snipt + + +class SniptIndex(indexes.RealTimeSearchIndex, indexes.Indexable): + text = indexes.CharField(document=True, use_template=True) + author = indexes.CharField(model_attr='user') + pub_date = indexes.DateTimeField(model_attr='created') + public = indexes.BooleanField(model_attr='public') + typ = indexes.CharField(model_attr='lexer') + + def get_model(self): + return Snipt + + def index_queryset(self): + """Used when the entire index for model is updated.""" + return self.get_model().objects.filter(created__lte=datetime.datetime.now()) diff --git a/templates/search/indexes/snipts/snipt_text.txt b/templates/search/indexes/snipts/snipt_text.txt new file mode 100644 index 0000000..8ba4c6d --- /dev/null +++ b/templates/search/indexes/snipts/snipt_text.txt @@ -0,0 +1,5 @@ +{{ object.title }} +{{ object.user.username }} +{{ object.tags_list }} +{{ object.lexer }} +{{ object.code }} diff --git a/templates/search/search.html b/templates/search/search.html new file mode 100644 index 0000000..566f1c4 --- /dev/null +++ b/templates/search/search.html @@ -0,0 +1,39 @@ +{% extends 'base.html' %} + +{% block content %} +

Search

+ +
+ + {{ form.as_table }} + + + + +
  + +
+ + {% if query %} +

Results

+ + {% for result in page.object_list %} +

+ {{ result.object.title }} +

+ {% empty %} +

No results found.

+ {% endfor %} + + {% if page.has_previous or page.has_next %} +
+ {% if page.has_previous %}{% endif %}« Previous{% if page.has_previous %}{% endif %} + | + {% if page.has_next %}{% endif %}Next »{% if page.has_next %}{% endif %} +
+ {% endif %} + {% else %} + {# Show some example queries to run, maybe query syntax, something else? #} + {% endif %} +
+{% endblock %} diff --git a/urls.py b/urls.py index 205eae1..ba39ea1 100644 --- a/urls.py +++ b/urls.py @@ -8,6 +8,7 @@ from snipts.api import * import admin as custom_admin + admin.autodiscover() public_api = Api(api_name='public') @@ -35,6 +36,8 @@ urlpatterns = patterns('', url(r'^api/', include(public_api.urls)), url(r'^api/', include(private_api.urls)), + url(r'^search/', include('haystack.urls')), + url(r'^register/$', lambda x: HttpResponseRedirect('/signup/')), url(r'^signup/$', 'registration.views.register', {