From fa8909f95f314f1bea5f52b81381304c68a75417 Mon Sep 17 00:00:00 2001 From: Marcin Koziej Date: Tue, 28 Aug 2012 00:15:33 +0200 Subject: [PATCH] events search/realtime indexing --- events/search_indexes.py | 27 +++++++++++++++++++ events/templates/events/event_list.html | 15 +++-------- .../events/snippets/event_short.html | 13 +++++++++ events/templatetags/events_tags.py | 5 ++++ migdal/helpers.py | 18 +++++++++++++ migdal/search_indexes.py | 27 +++++-------------- migdal/templates/search/search.html | 7 ++++- prawokultury/urls.py | 4 +-- 8 files changed, 80 insertions(+), 36 deletions(-) create mode 100644 events/search_indexes.py create mode 100644 events/templates/events/snippets/event_short.html diff --git a/events/search_indexes.py b/events/search_indexes.py new file mode 100644 index 0000000..1321dcd --- /dev/null +++ b/events/search_indexes.py @@ -0,0 +1,27 @@ +from django.conf import settings +import datetime +from haystack import indexes +from events.models import Event +from migdal.helpers import add_translatable_index + + +class EventIndex(indexes.RealTimeSearchIndex, indexes.Indexable): + def get_model(self): + return Event + + def index_queryset(self): + """Used when the entire index for model is updated.""" + return self.get_model().objects.all() + + +add_translatable_index(EventIndex, { +# Haystack needs a main field to be the same across all indexes +# so we treat title of the event as this main content, named 'body' + 'body': indexes.CharField(model_attr='title'), + 'organizer': indexes.CharField(), + 'place': indexes.CharField() + }) + + +getattr(EventIndex, "body_%s" % settings.LANGUAGE_CODE).document = True + diff --git a/events/templates/events/event_list.html b/events/templates/events/event_list.html index 80dcffb..66c5184 100755 --- a/events/templates/events/event_list.html +++ b/events/templates/events/event_list.html @@ -1,23 +1,14 @@ {% extends "base.html" %} {% load url from future %} {% load i18n pagination_tags prevnext %} +{% load events_tags %} {% block "body" %}

{% trans "Events" %}

{% autopaginate object_list 10 %} {% for event in object_list %} -
- -

{{ event.date }}

{# just date #} - {{ event.title }} - -
- {{ event.date }}, {{ event.place }}
- {% trans "Organizer" %}: {{ event.organizer }}
-
-
-
+ {% event_short event %} {% endfor %}

@@ -33,4 +24,4 @@ {% endif %}

-{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/events/templates/events/snippets/event_short.html b/events/templates/events/snippets/event_short.html new file mode 100644 index 0000000..4e32ad9 --- /dev/null +++ b/events/templates/events/snippets/event_short.html @@ -0,0 +1,13 @@ +{% load i18n %} +
+ +

{{ event.date }}

{# just date #} + {{ event.title }} + +
+ {{ event.date }}, {{ event.place }}
+ {% trans "Organizer" %}: {{ event.organizer }}
+
+
+
+ diff --git a/events/templatetags/events_tags.py b/events/templatetags/events_tags.py index a51bda9..c9dd311 100644 --- a/events/templatetags/events_tags.py +++ b/events/templatetags/events_tags.py @@ -14,3 +14,8 @@ from datetime import datetime def events_box(context, limit=app_settings.BOX_LENGTH): objects = Event.objects.filter(date__gte=datetime.now())[:limit] return {'objects': objects} + + +@register.inclusion_tag('events/snippets/event_short.html') +def event_short(event): + return {'event': event} diff --git a/migdal/helpers.py b/migdal/helpers.py index 1fdbdd4..2de7ab0 100644 --- a/migdal/helpers.py +++ b/migdal/helpers.py @@ -71,3 +71,21 @@ def i18n_patterns(prefix, *args): return pattern_list return pattern_list + [MyLocaleRegexURLResolver(pattern_list)] + +def add_translatable_index(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 = new_field.index_fieldname \ + and "%s_%s" % (new_field.index_fieldname, lang_code) \ + or fname + new_field.model_attr = new_field.model_attr \ + and "%s_%s" % (new_field.model_attr, lang_code) \ + or fname + setattr(index_class, fname, new_field) + index_class.fields[fname] = new_field + diff --git a/migdal/search_indexes.py b/migdal/search_indexes.py index 333899f..63acc1c 100644 --- a/migdal/search_indexes.py +++ b/migdal/search_indexes.py @@ -2,11 +2,10 @@ from django.conf import settings import datetime from haystack import indexes from migdal.models import Entry +from migdal.helpers import add_translatable_index -from copy import copy - -class EntryIndex(indexes.SearchIndex, indexes.Indexable): +class EntryIndex(indexes.RealTimeSearchIndex, indexes.Indexable): date = indexes.DateTimeField(indexed=True, model_attr="date") author = indexes.CharField(model_attr="author") @@ -18,24 +17,10 @@ class EntryIndex(indexes.SearchIndex, indexes.Indexable): 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) +add_translatable_index(EntryIndex, { + 'title': indexes.CharField(), + 'lead': indexes.CharField(), + 'body': indexes.CharField() }) diff --git a/migdal/templates/search/search.html b/migdal/templates/search/search.html index 6d7b9c7..1c561a4 100644 --- a/migdal/templates/search/search.html +++ b/migdal/templates/search/search.html @@ -2,13 +2,18 @@ {% load url from future %} {% load i18n %} {% load migdal_tags %} +{% load events_tags %} {% block "body" %}

{% trans "Search results" %}

{% for result in page.object_list %} -{% entry_short result.object %} + {% if result.model_name == "entry" %} + {% entry_short result.object %} + {% elif result.model_name == "event" %} + {% event_short result.object %} + {% endif %} {% empty %}

{% trans "No results found." %}

{% endfor %} diff --git a/prawokultury/urls.py b/prawokultury/urls.py index 436cb0e..1a2faa8 100644 --- a/prawokultury/urls.py +++ b/prawokultury/urls.py @@ -9,7 +9,7 @@ from django.contrib.staticfiles.urls import staticfiles_urlpatterns from migdal.helpers import i18n_patterns from migdal.urls import urlpatterns as migdal_urlpatterns from django.utils.translation import ugettext_lazy as _, string_concat - +from events.urls import urlpatterns as events_urlpatterns admin.autodiscover() @@ -23,7 +23,7 @@ urlpatterns = patterns('', ) + i18n_patterns('', url(string_concat(r'^', _('events'), r'/'), include('events.urls')), url(r'^comments/', include('django_comments_xtd.urls')), -) + migdal_urlpatterns +) + migdal_urlpatterns if settings.DEBUG: urlpatterns += patterns('', -- 2.20.1