Merge branch 'search'
authorMarcin Koziej <marcin.koziej@nowoczesnapolska.org.pl>
Mon, 27 Aug 2012 14:27:35 +0000 (16:27 +0200)
committerMarcin Koziej <marcin.koziej@nowoczesnapolska.org.pl>
Mon, 27 Aug 2012 14:27:35 +0000 (16:27 +0200)
migdal/models.py
migdal/search_indexes.py [new file with mode: 0644]
migdal/templates/search/search.html [new file with mode: 0644]
prawokultury/settings.d/30-apps.conf
prawokultury/settings.d/35-search.conf [new file with mode: 0644]
prawokultury/urls.py
requirements.txt
scripts/make-tags [new file with mode: 0755]

index 3e0a8a3..97fbab3 100644 (file)
@@ -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 (file)
index 0000000..333899f
--- /dev/null
@@ -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 (file)
index 0000000..6d7b9c7
--- /dev/null
@@ -0,0 +1,25 @@
+{% extends "base.html" %}
+{% load url from future %}
+{% load i18n %}
+{% load migdal_tags %}
+
+
+{% block "body" %}
+<h1>{% trans "Search results" %}</h1>
+
+{% for result in page.object_list %}
+{% entry_short result.object %}
+{% empty %}
+<p>{% trans "No results found." %}</p>
+{% endfor %}
+
+{% if page.has_previous or page.has_next %}
+<div>
+  {% if page.has_previous %}<a href="?q={{ query }}&amp;page={{ page.previous_page_number }}">{% endif %}{% trans "&laquo; Previous" %}{% if page.has_previous %}</a>{% endif %}
+  |
+  {% if page.has_next %}<a href="?q={{ query }}&amp;page={{ page.next_page_number }}">{% endif %}{% trans "Next &raquo;" %}{% if page.has_next %}</a>{% endif %}
+</div>
+{% else %}
+{# Show some example queries to run, maybe query syntax, something else? #}
+{% endif %}
+{% endblock "body" %}
index fdd916d..c950a4e 100755 (executable)
@@ -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 (file)
index 0000000..62b5799
--- /dev/null
@@ -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
index 0d9da8c..436cb0e 100644 (file)
@@ -19,6 +19,7 @@ urlpatterns = patterns('',
     url(r'^media/(?P<path>.*)$', '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()
index c290b2b..145192a 100644 (file)
@@ -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 (executable)
index 0000000..30dc096
--- /dev/null
@@ -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