From: Marcin Koziej Date: Mon, 27 Aug 2012 22:15:33 +0000 (+0200) Subject: events search/realtime indexing X-Git-Url: https://git.mdrn.pl/prawokultury.git/commitdiff_plain/fa8909f95f314f1bea5f52b81381304c68a75417?hp=ce7f059d284616e5bf6f62306c1def5d9aae5ab4 events search/realtime indexing --- 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('',