# HG changeset patch # User ymh # Date 1379708508 -7200 # Node ID c055674048885980842c00bd2ac534ecfdffcb93 # Parent ba6056f585163a65f3c26f7eee90720d792b41d4 First version of indexation. Replace the list view by a search view diff -r ba6056f58516 -r c05567404888 src/p4l/config.py.tmpl --- a/src/p4l/config.py.tmpl Fri Sep 20 15:44:11 2013 +0200 +++ b/src/p4l/config.py.tmpl Fri Sep 20 22:21:48 2013 +0200 @@ -135,6 +135,14 @@ } } +HAYSTACK_CONNECTIONS = { + 'default': { + 'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine', + 'URL': 'http://127.0.0.1:9200/', + 'INDEX_NAME': 'p4l', + }, +} + NB_RECORDS_BY_PAGE = 20 SPARQL_QUERY_ENDPOINT = "http://localhost:8080/openrdf-sesame/repositories/plan4learning" diff -r ba6056f58516 -r c05567404888 src/p4l/forms.py --- a/src/p4l/forms.py Fri Sep 20 15:44:11 2013 +0200 +++ b/src/p4l/forms.py Fri Sep 20 22:21:48 2013 +0200 @@ -9,10 +9,8 @@ from django.contrib.auth.forms import (UserChangeForm as AuthUserChangeForm, UserCreationForm as AuthUserCreationForm) from django.core.exceptions import ValidationError -from django.forms import Form -from django.forms.fields import ChoiceField, CharField +from django.forms.fields import ChoiceField from django.utils.translation import ugettext as _ -from .models import Record User = get_user_model() @@ -37,14 +35,3 @@ class Meta: model = User - -class RecordFilterForm(Form): - title = CharField(required=True, min_length=1) - def get_filter_qs(self, qs=None): - if qs is None: - qs = Record.objects.select_related("language").prefetch_related('titles').distinct() # @UndefinedVariable - t = self.cleaned_data.get('title',None) - if t: - qs = qs.filter(titles__title__icontains=t) - return qs - diff -r ba6056f58516 -r c05567404888 src/p4l/models/data.py --- a/src/p4l/models/data.py Fri Sep 20 15:44:11 2013 +0200 +++ b/src/p4l/models/data.py Fri Sep 20 22:21:48 2013 +0200 @@ -171,6 +171,12 @@ modification_date = models.DateTimeField(auto_now=True, serialize=False) modified_by = models.ForeignKey(User, blank=True, null=True) + def get_titles(self): + return [t.title for t in self.titles.all()] + + def get_authors(self): + return [a.name for a in self.authors.all()] + def get_imprints_years(self): return sorted(set([i.imprintDate for i in self.imprints.all() if i.imprintDate])) diff -r ba6056f58516 -r c05567404888 src/p4l/search/__init__.py diff -r ba6056f58516 -r c05567404888 src/p4l/search/forms.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/p4l/search/forms.py Fri Sep 20 22:21:48 2013 +0200 @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- +''' +Created on Sep 20, 2013 + +@author: ymh +''' +from haystack.forms import SearchForm + +from p4l.utils import strip_accents + + +class RecordSearchForm(SearchForm): + + def __init__(self, *args, **kwargs): + SearchForm.__init__(self, *args, **kwargs) + + def no_query_found(self): + return self.searchqueryset.all() + + def search(self): + if not self.is_valid(): + return self.no_query_found() + + if not self.cleaned_data.get('q'): + return self.no_query_found() + + sqs = self.searchqueryset.auto_query(strip_accents(self.cleaned_data['q'])) + + if self.load_all: + sqs = sqs.load_all() + + return sqs diff -r ba6056f58516 -r c05567404888 src/p4l/search/index.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/p4l/search/index.py Fri Sep 20 22:21:48 2013 +0200 @@ -0,0 +1,37 @@ +# -*- coding: utf-8 -*- +''' +Created on Sep 20, 2013 + +@author: ymh +''' + +from haystack import indexes + +from p4l.models import Record +from p4l.utils import strip_accents + + +class RecordIndex(indexes.SearchIndex, indexes.Indexable): + + text = indexes.CharField(document=True, use_template=True, stored=False) + identifier = indexes.CharField(model_attr="identifier", stored=True) + titles = indexes.MultiValueField(model_attr="get_titles", stored=False) + titles_src = indexes.MultiValueField(model_attr="get_titles", stored=True, indexed=False) + authors = indexes.MultiValueField(model_attr="get_authors", stored=False) + + years = indexes.MultiValueField(model_attr="get_imprints_years", indexed=False, stored=True) + + def prepare_titles(self, obj): + return [strip_accents(v) for v in obj.get_titles()] + + def prepare_authors(self, obj): + return [strip_accents(v) for v in obj.get_authors()] + + def get_model(self): + return Record + + def get_updated_field(self): + return "modification_date" + + def index_queryset(self, using=None): + return Record.objects.using(using).all().prefetch_related("imprints","authors", "titles") \ No newline at end of file diff -r ba6056f58516 -r c05567404888 src/p4l/search/views.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/p4l/search/views.py Fri Sep 20 22:21:48 2013 +0200 @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +''' +Created on Sep 20, 2013 + +@author: ymh +''' +from django.conf import settings +from django.template.context import RequestContext +from haystack.query import SearchQuerySet +from haystack.views import SearchView, search_view_factory + +from p4l.search.forms import RecordSearchForm + + +class RecordSearchView(SearchView): + + def __init__(self, template=None, load_all=True, form_class=None, searchqueryset=None, context_class=RequestContext, results_per_page=None): + record_searchQuerySet = SearchQuerySet().order_by('identifier') + template = "p4l/p4l_home.html" + results_per_page= settings.NB_RECORDS_BY_PAGE + form_class = RecordSearchForm + SearchView.__init__(self, template=template, load_all=False, form_class=form_class, searchqueryset=record_searchQuerySet, context_class=context_class, results_per_page=results_per_page) + + @classmethod + def as_view(cls): + return search_view_factory(view_class=cls) \ No newline at end of file diff -r ba6056f58516 -r c05567404888 src/p4l/search_indexes.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/p4l/search_indexes.py Fri Sep 20 22:21:48 2013 +0200 @@ -0,0 +1,8 @@ +# -*- coding: utf-8 -*- +''' +Created on Sep 20, 2013 + +@author: ymh +''' + +from p4l.search.index import RecordIndex # @UnusedImport \ No newline at end of file diff -r ba6056f58516 -r c05567404888 src/p4l/settings.py --- a/src/p4l/settings.py Fri Sep 20 15:44:11 2013 +0200 +++ b/src/p4l/settings.py Fri Sep 20 22:21:48 2013 +0200 @@ -136,6 +136,7 @@ 'django_extensions', 'south', 'rest_framework', + 'haystack', 'p4l' ) @@ -173,6 +174,16 @@ } } +HAYSTACK_CONNECTIONS = { + 'default': { + 'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine', + 'URL': '', + 'INDEX_NAME': 'p4l', + }, +} + + + NB_RECORDS_BY_PAGE = 20 SPARQL_QUERY_ENDPOINT = "http://localhost:8080/openrdf-sesame/repositories/plan4learning" SPARQL_SUBJECT_QUERIES = { diff -r ba6056f58516 -r c05567404888 src/p4l/templates/p4l/p4l_home.html --- a/src/p4l/templates/p4l/p4l_home.html Fri Sep 20 15:44:11 2013 +0200 +++ b/src/p4l/templates/p4l/p4l_home.html Fri Sep 20 22:21:48 2013 +0200 @@ -3,7 +3,7 @@ {% load i18n %} {% load p4lstringfilters %} -{% block page_title %}{% trans 'Record List' %} - {% trans 'Page' %} {{ page_obj.number }}{% endblock %} +{% block page_title %}{% trans 'Record List' %} - {% trans 'Page' %} {{ page.number }}{% endblock %} {% block content %}
@@ -11,7 +11,7 @@
- +
@@ -20,36 +20,39 @@
-{% if is_paginated %} -