index document contents
This commit is contained in:
parent
62a42d4a0e
commit
dcd2d2bf4a
|
@ -12,6 +12,14 @@
|
|||
|
||||
<i18n:registerTranslations directory="locales" />
|
||||
|
||||
|
||||
<plone:behavior
|
||||
title="Document SearchableText indexer behavior"
|
||||
description="Enables the dynamic SearchableText indexer for Documents"
|
||||
provides="tabellio.documents.indexer.IDocumentIndexer"
|
||||
for="plone.dexterity.interfaces.IDexterityContent"
|
||||
/>
|
||||
|
||||
<plone:behavior
|
||||
title="Authors SearchableText indexer behavior"
|
||||
description="Enables the dynamic SearchableText indexer for Authors fields"
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
from zope.interface import Interface
|
||||
from ZODB.POSException import ConflictError
|
||||
from five import grok
|
||||
from Products.CMFCore.utils import getToolByName
|
||||
from plone.dexterity.utils import iterSchemata
|
||||
from plone.indexer import indexer
|
||||
from plone.z3cform import z2
|
||||
|
@ -84,6 +86,11 @@ grok.global_adapter(dossierInterveningPersonsIndexer, name="interveningPersonsDo
|
|||
grok.global_adapter(questionInterveningPersonsIndexer, name="interveningPersonsDoc")
|
||||
|
||||
|
||||
class IDocumentIndexer(Interface):
|
||||
"""Dexterity behavior interface for enabling the dynamic SearchableText
|
||||
indexer on Document objecgs."""
|
||||
|
||||
|
||||
class IAuthorsIndexer(Interface):
|
||||
"""Dexterity behavior interface for enabling the dynamic SearchableText
|
||||
indexer on Authors fields."""
|
||||
|
@ -98,8 +105,7 @@ class FakeView(object):
|
|||
self.request = request
|
||||
|
||||
|
||||
@indexer(IAuthorsIndexer)
|
||||
def dynamic_searchable_text_indexer(obj):
|
||||
def raw_author_dynamic_searchable_text_indexer(obj):
|
||||
"""Dynamic searchable text indexer.
|
||||
"""
|
||||
|
||||
|
@ -136,10 +142,49 @@ def dynamic_searchable_text_indexer(obj):
|
|||
return ' '.join(indexed)
|
||||
|
||||
|
||||
grok.global_adapter(dynamic_searchable_text_indexer,
|
||||
@indexer(IAuthorsIndexer)
|
||||
def author_dynamic_searchable_text_indexer(obj):
|
||||
return raw_author_dynamic_searchable_text_indexer(obj)
|
||||
|
||||
|
||||
grok.global_adapter(author_dynamic_searchable_text_indexer,
|
||||
name='SearchableText')
|
||||
|
||||
|
||||
@indexer(IDocumentIndexer)
|
||||
def document_dynamic_searchable_text_indexer(obj):
|
||||
"""Dynamic searchable text indexer.
|
||||
"""
|
||||
|
||||
title_and_authors = raw_author_dynamic_searchable_text_indexer(obj)
|
||||
|
||||
data = obj.file
|
||||
if not data or data.getSize() == 0:
|
||||
return title_and_authors
|
||||
|
||||
# if there is no path to text/plain, do nothing
|
||||
transforms = getToolByName(obj, 'portal_transforms')
|
||||
if not transforms._findPath(data.contentType, 'text/plain'):
|
||||
return title_and_authors
|
||||
|
||||
# convert it to text/plain
|
||||
print 'indexing pdf'
|
||||
try:
|
||||
datastream = transforms.convertTo(
|
||||
'text/plain', data.data, mimetype=data.contentType,
|
||||
filename=data.filename)
|
||||
return title_and_authors + ' ' + datastream.getData()
|
||||
except (ConflictError, KeyboardInterrupt):
|
||||
raise
|
||||
|
||||
return title_and_authors
|
||||
|
||||
|
||||
grok.global_adapter(document_dynamic_searchable_text_indexer,
|
||||
name='SearchableText')
|
||||
|
||||
|
||||
|
||||
def get_field_widget(obj, field):
|
||||
"""Returns the field widget of a field in display mode without
|
||||
touching any form.
|
||||
|
|
|
@ -23,7 +23,7 @@
|
|||
<!-- enabled behaviors -->
|
||||
<property name="behaviors">
|
||||
<element value="plone.app.content.interfaces.INameFromTitle" />
|
||||
<element value="tabellio.documents.indexer.IAuthorsIndexer" />
|
||||
<element value="tabellio.documents.indexer.IDocumentIndexer" />
|
||||
</property>
|
||||
|
||||
<!-- View information -->
|
||||
|
|
Reference in New Issue