src/p4l/search/index.py
author ymh <ymh.work@gmail.com>
Wed, 25 Sep 2013 22:14:51 +0200
changeset 117 0a4e7d6ebe80
parent 114 93b45b4f423c
child 126 a345f1a67bf1
permissions -rw-r--r--
- Do not create object for new - optimise object loading on edit (avoid double object query) - optimize sparql queries configuration - rename templates - update trnslations
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
113
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
'''
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
Created on Sep 20, 2013
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
@author: ymh
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
'''
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
from haystack import indexes
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
from p4l.models import Record
114
93b45b4f423c add corporate authors and small adjustments
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    11
from p4l.utils import strip_accents, get_labels_for_uris, safe_cache_key
93b45b4f423c add corporate authors and small adjustments
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    12
93b45b4f423c add corporate authors and small adjustments
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    13
from django.core.cache import get_cache
93b45b4f423c add corporate authors and small adjustments
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    14
from django.conf import settings
93b45b4f423c add corporate authors and small adjustments
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    15
import logging
93b45b4f423c add corporate authors and small adjustments
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    16
93b45b4f423c add corporate authors and small adjustments
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    17
logger = logging.getLogger(__name__)
93b45b4f423c add corporate authors and small adjustments
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    18
93b45b4f423c add corporate authors and small adjustments
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    19
def get_organizations_label(uris):
93b45b4f423c add corporate authors and small adjustments
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    20
    cache = get_cache('indexation')
93b45b4f423c add corporate authors and small adjustments
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    21
    
93b45b4f423c add corporate authors and small adjustments
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    22
    res = {}
93b45b4f423c add corporate authors and small adjustments
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    23
    missing_uris = []
93b45b4f423c add corporate authors and small adjustments
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    24
    
93b45b4f423c add corporate authors and small adjustments
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    25
    for uri in uris: 
93b45b4f423c add corporate authors and small adjustments
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    26
        label = cache.get(safe_cache_key(uri))
93b45b4f423c add corporate authors and small adjustments
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    27
        if label is not None:
93b45b4f423c add corporate authors and small adjustments
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    28
            res[uri] = label
93b45b4f423c add corporate authors and small adjustments
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    29
        else:
93b45b4f423c add corporate authors and small adjustments
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    30
            missing_uris.append(uri)
93b45b4f423c add corporate authors and small adjustments
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    31
            
117
0a4e7d6ebe80 - Do not create object for new
ymh <ymh.work@gmail.com>
parents: 114
diff changeset
    32
    new_labels = get_labels_for_uris(missing_uris, settings.RDF_SCHEMES['organizations'], None, True)
114
93b45b4f423c add corporate authors and small adjustments
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    33
    for k,v in new_labels.iteritems():
93b45b4f423c add corporate authors and small adjustments
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    34
        cache.set(safe_cache_key(k),v)
93b45b4f423c add corporate authors and small adjustments
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    35
        res[k] = v
93b45b4f423c add corporate authors and small adjustments
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    36
    
93b45b4f423c add corporate authors and small adjustments
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    37
    return res
113
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
114
93b45b4f423c add corporate authors and small adjustments
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    40
class RecordIndex(indexes.SearchIndex, indexes.Indexable):    
113
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
    text = indexes.CharField(document=True, use_template=True, stored=False)
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
    identifier = indexes.CharField(model_attr="identifier", stored=True)
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
    titles = indexes.MultiValueField(model_attr="get_titles", stored=False)
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
    titles_src = indexes.MultiValueField(model_attr="get_titles", stored=True, indexed=False)
114
93b45b4f423c add corporate authors and small adjustments
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    46
    authors = indexes.MultiValueField(model_attr="all_authors", stored=False)
113
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
    
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
    years = indexes.MultiValueField(model_attr="get_imprints_years", indexed=False, stored=True)
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
    
114
93b45b4f423c add corporate authors and small adjustments
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    50
    def prepare(self, obj):
93b45b4f423c add corporate authors and small adjustments
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    51
        authors = obj.get_authors() + get_organizations_label(obj.get_corporate_authors()).values()
93b45b4f423c add corporate authors and small adjustments
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    52
        obj.all_authors = [strip_accents(unicode(v)) for v in authors] 
93b45b4f423c add corporate authors and small adjustments
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    53
        return indexes.SearchIndex.prepare(self, obj)
93b45b4f423c add corporate authors and small adjustments
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    54
    
113
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
    def prepare_titles(self, obj):
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
        return [strip_accents(v) for v in obj.get_titles()]
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
    def get_model(self):
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
        return Record
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
    def get_updated_field(self):
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
        return "modification_date"
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
    
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
    def index_queryset(self, using=None):
114
93b45b4f423c add corporate authors and small adjustments
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    65
        return Record.objects.using(using).all().prefetch_related("imprints","authors", "titles", "corporateAuthors")