From 9c8f0adb8bb6c8b86517798d70c2c8dcd198e721 Mon Sep 17 00:00:00 2001 From: Marcin Koziej Date: Mon, 27 Aug 2012 14:18:38 +0200 Subject: [PATCH] haystack ignoring fields? --- doc/schema.xml | 160 +++++++++++++++++++++++++ migdal/helpers.py | 3 +- migdal/models.py | 1 + migdal/search_indexes.py | 40 +++++++ migdal/templates/search/search.html | 25 ++++ prawokultury/settings.d/30-apps.conf | 1 + prawokultury/settings.d/35-search.conf | 9 ++ prawokultury/urls.py | 3 +- requirements.txt | 3 + scripts/make-tags | 13 ++ 10 files changed, 256 insertions(+), 2 deletions(-) create mode 100644 doc/schema.xml create mode 100644 migdal/search_indexes.py create mode 100644 migdal/templates/search/search.html create mode 100644 prawokultury/settings.d/35-search.conf create mode 100755 scripts/make-tags diff --git a/doc/schema.xml b/doc/schema.xml new file mode 100644 index 0000000..236b417 --- /dev/null +++ b/doc/schema.xml @@ -0,0 +1,160 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + id + + + + + + + + diff --git a/migdal/helpers.py b/migdal/helpers.py index 1fdbdd4..39f5a60 100644 --- a/migdal/helpers.py +++ b/migdal/helpers.py @@ -34,8 +34,9 @@ def add_translatable(model, fields, languages=None): for name, field in fields.items(): for lang_code, lang_name in languages: new_field = copy(field) - if field.verbose_name: + if hasattr(field, 'verbose_name') and field.verbose_name: new_field.verbose_name = string_concat(field.verbose_name, ' [%s]' % lang_code) + new_field.contribute_to_class(model, "%s_%s" % (name, lang_code)) setattr(model, name, field_getter(name)) # add setter? diff --git a/migdal/models.py b/migdal/models.py index 83a1865..9a52fe2 100644 --- a/migdal/models.py +++ b/migdal/models.py @@ -9,6 +9,7 @@ from migdal.helpers import add_translatable from migdal import settings + class Category(models.Model): taxonomy = models.CharField(_('taxonomy'), max_length=32, choices=settings.TAXONOMIES) diff --git a/migdal/search_indexes.py b/migdal/search_indexes.py new file mode 100644 index 0000000..6223b6e --- /dev/null +++ b/migdal/search_indexes.py @@ -0,0 +1,40 @@ +import datetime +from haystack import indexes +from migdal.models import Entry +from django.conf import settings +from copy import copy + + +class EntryIndex(indexes.SearchIndex, indexes.Indexable): + date = indexes.DateTimeField(indexed=True) + author = indexes.CharField() + + def get_model(self): + return Entry + + def index_queryset(self): + """Used when the entire index for model is updated.""" + return self.get_model().objects.filter(date__lte=datetime.datetime.now()) + + +def add_translatable(index_class, fields, languages=None): + """Adds some translatable fields to a search index, and a getter.""" + if languages is None: + languages = settings.LANGUAGES + for name, field in fields.items(): + for lang_code, lang_name in languages: + new_field = copy(field) + fname = "%s_%s" % (name, lang_code) + new_field.index_fieldname = fname + setattr(index_class, fname, new_field) + index_class.fields[fname] = new_field + + +add_translatable(EntryIndex, { + 'title': indexes.CharField(indexed=True, document=False), + 'lead': indexes.CharField(indexed=True, document=False), + 'body': indexes.CharField(indexed=True, document=False) + }) + + +getattr(EntryIndex, "body_%s" % settings.LANGUAGE_CODE).document = True diff --git a/migdal/templates/search/search.html b/migdal/templates/search/search.html new file mode 100644 index 0000000..f38a4e6 --- /dev/null +++ b/migdal/templates/search/search.html @@ -0,0 +1,25 @@ +{% extends "base.html" %} +{% load url from future %} +{% load i18n %} +{% load migdal_tags %} + + +{% block "body" %} +

{% trans "Search results" %}

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

{% trans "No results found." %}

+{% endfor %} + +{% if page.has_previous or page.has_next %} +
+ {% if page.has_previous %}{% endif %}{% trans "« Previous" %}{% if page.has_previous %}{% endif %} + | + {% if page.has_next %}{% endif %}{% trans "Next »" %}{% if page.has_next %}{% endif %} +
+{% else %} +{# Show some example queries to run, maybe query syntax, something else? #} +{% endif %} +{% endblock "body" %} diff --git a/prawokultury/settings.d/30-apps.conf b/prawokultury/settings.d/30-apps.conf index 931cb41..3824631 100755 --- a/prawokultury/settings.d/30-apps.conf +++ b/prawokultury/settings.d/30-apps.conf @@ -8,6 +8,7 @@ INSTALLED_APPS = ( 'django.contrib.comments', 'django_comments_xtd', 'pipeline', + 'haystack', 'django.contrib.auth', 'django.contrib.contenttypes', diff --git a/prawokultury/settings.d/35-search.conf b/prawokultury/settings.d/35-search.conf new file mode 100644 index 0000000..62b5799 --- /dev/null +++ b/prawokultury/settings.d/35-search.conf @@ -0,0 +1,9 @@ +HAYSTACK_CONNECTIONS = { + 'default': { + 'ENGINE': 'haystack.backends.solr_backend.SolrEngine', + 'URL': 'http://127.0.0.1:8983/solr/prawokultury' + }, +} + +from django.conf import settings +HAYSTACK_DOCUMENT_FIELD = "body_%s" % settings.LANGUAGE_CODE diff --git a/prawokultury/urls.py b/prawokultury/urls.py index f074158..6da0510 100644 --- a/prawokultury/urls.py +++ b/prawokultury/urls.py @@ -19,10 +19,11 @@ urlpatterns = patterns('', url(r'^media/(?P.*)$', 'django.views.static.serve', { 'document_root': settings.MEDIA_ROOT, }), + url(r'^search/', include('haystack.urls')), ) + i18n_patterns('', url(string_concat(r'^', _('events'), r'/'), include('events.urls')), url(r'^comments/', include('django_comments_xtd.urls')), ) + migdal_urlpatterns -urlpatterns += staticfiles_urlpatterns() \ No newline at end of file +urlpatterns += staticfiles_urlpatterns() diff --git a/requirements.txt b/requirements.txt index 8d580d9..7efd852 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,3 +8,6 @@ django-markupfield django-gravatar django_comments_xtd django-pipeline + +-e git+https://github.com/toastdriven/django-haystack.git@master#egg=django-haystack +pysolr diff --git a/scripts/make-tags b/scripts/make-tags new file mode 100755 index 0000000..30dc096 --- /dev/null +++ b/scripts/make-tags @@ -0,0 +1,13 @@ +#!/bin/bash + +ROOT=$(git rev-parse --show-toplevel) + +find $ROOT -name '*.py' | xargs etags -o ${ROOT}/TAGS +if [ -n "$VIRTUAL_ENV" ]; then + find ${VIRTUAL_ENV}/lib -name '*.py' |xargs etags -a -o ${ROOT}/TAGS +else + echo "No Virtual env enabled, will not add it to TAGS" +fi + +find $ROOT/prawokultury/static/css -name '*.css' |xargs etags -a -o ${ROOT}/TAGS +#find $ROOT/prawokultury/static/js -name '*.js' |xargs etags -a -o ${ROOT}/TAGS -- 2.20.1