From: Marcin Koziej Date: Mon, 27 Aug 2012 14:27:35 +0000 (+0200) Subject: Merge branch 'search' X-Git-Url: https://git.mdrn.pl/prawokultury.git/commitdiff_plain/fb2d5b78a90d3042f7eb5e8e419fcbe6e62fd00d?hp=ce4104d701138f6e19107774ad06ecce69bde6f9 Merge branch 'search' --- diff --git a/migdal/models.py b/migdal/models.py index 3e0a8a3..97fbab3 100644 --- a/migdal/models.py +++ b/migdal/models.py @@ -9,6 +9,7 @@ from migdal.helpers import add_translatable from migdal import app_settings + class Category(models.Model): taxonomy = models.CharField(_('taxonomy'), max_length=32, choices=app_settings.TAXONOMIES) diff --git a/migdal/search_indexes.py b/migdal/search_indexes.py new file mode 100644 index 0000000..333899f --- /dev/null +++ b/migdal/search_indexes.py @@ -0,0 +1,42 @@ +from django.conf import settings +import datetime +from haystack import indexes +from migdal.models import Entry + +from copy import copy + + +class EntryIndex(indexes.SearchIndex, indexes.Indexable): + date = indexes.DateTimeField(indexed=True, model_attr="date") + author = indexes.CharField(model_attr="author") + + 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 + new_field.model_attr = 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..6d7b9c7 --- /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.object %} +{% 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 fdd916d..c950a4e 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', 'pagination', 'sorl.thumbnail', 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 0d9da8c..436cb0e 100644 --- a/prawokultury/urls.py +++ b/prawokultury/urls.py @@ -19,6 +19,7 @@ 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')), @@ -31,4 +32,4 @@ if settings.DEBUG: }), ) -urlpatterns += staticfiles_urlpatterns() \ No newline at end of file +urlpatterns += staticfiles_urlpatterns() diff --git a/requirements.txt b/requirements.txt index c290b2b..145192a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,6 +9,10 @@ django-markupfield django-gravatar django_comments_xtd django-pipeline>=1.2,<1.3 + #pyScss #git+git://github.com/Kronuz/pyScss.git@d8f4da23a3c87696a75b3830ed4ab49b75550a93#egg=pyScss #TODO: pyScss support, for now just install sass + +-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