102 lines
3.1 KiB
Python
102 lines
3.1 KiB
Python
from DateTime import DateTime
|
|
from plone.indexer import indexer
|
|
from five import grok
|
|
import plone.dexterity.interfaces
|
|
from Products.CMFCore.utils import getToolByName
|
|
from ZODB.POSException import ConflictError
|
|
|
|
from themis.fields.vocabs import ContactsSource
|
|
|
|
@indexer(plone.dexterity.interfaces.IDexterityItem)
|
|
def dateIndexer(obj):
|
|
if obj.portal_type not in ('courrier_entrant', 'courrier_sortant'):
|
|
return None
|
|
for attr in ('date_reelle_courrier', 'date_reception'):
|
|
if not hasattr(obj, attr):
|
|
continue
|
|
if getattr(obj, attr):
|
|
return DateTime(getattr(obj, attr).isoformat())
|
|
return None
|
|
|
|
grok.global_adapter(dateIndexer, name="dateCourrier")
|
|
|
|
|
|
@indexer(plone.dexterity.interfaces.IDexterityItem)
|
|
def mail_dynamic_searchable_text_indexer(obj):
|
|
"""Dynamic searchable text indexer.
|
|
"""
|
|
if obj.portal_type not in ('courrier_entrant', 'courrier_sortant'):
|
|
return None
|
|
|
|
data = obj.fichier
|
|
if not data or data.getSize() == 0:
|
|
return obj.title
|
|
|
|
# if there is no path to text/plain, do nothing
|
|
transforms = getToolByName(obj, 'portal_transforms')
|
|
if not transforms._findPath(data.contentType, 'text/plain'):
|
|
return obj.title
|
|
|
|
# convert it to text/plain
|
|
try:
|
|
datastream = transforms.convertTo(
|
|
'text/plain', data.data, mimetype=data.contentType,
|
|
filename=data.filename)
|
|
data = datastream.getData()
|
|
except (ConflictError, KeyboardInterrupt):
|
|
raise
|
|
|
|
try:
|
|
data = unicode(datastream.getData(), 'utf-8')
|
|
except UnicodeDecodeError, e:
|
|
try:
|
|
data = unicode(datastream.getData()[:e.start], 'utf-8')
|
|
except UnicodeDecodeError:
|
|
# ok, forget it
|
|
data = ''
|
|
|
|
return obj.title + ' ' + data
|
|
|
|
grok.global_adapter(mail_dynamic_searchable_text_indexer, name='MailSearchableText')
|
|
|
|
@indexer(plone.dexterity.interfaces.IDexterityItem)
|
|
def contactIndexer(obj):
|
|
if obj.portal_type not in ('courrier_entrant', 'courrier_sortant'):
|
|
return None
|
|
contacts_dir = getattr(getToolByName(obj, 'portal_url').getPortalObject(), 'contacts')
|
|
for attr in ('expediteur', 'destinataire'):
|
|
if not hasattr(obj, attr):
|
|
continue
|
|
v = getattr(obj, attr)
|
|
if not v:
|
|
continue
|
|
v = v[0]
|
|
return v
|
|
return None
|
|
|
|
grok.global_adapter(contactIndexer, name="MailContact")
|
|
|
|
@indexer(plone.dexterity.interfaces.IDexterityItem)
|
|
def contactFuzzyIndexer(obj):
|
|
if obj.portal_type not in ('courrier_entrant', 'courrier_sortant'):
|
|
return None
|
|
for attr in ('expediteur', 'destinataire'):
|
|
if not hasattr(obj, attr):
|
|
continue
|
|
v = getattr(obj, attr)
|
|
if not v:
|
|
continue
|
|
v = v[0]
|
|
if ':' in v:
|
|
src = ContactsSource()
|
|
r = src.fastGetTitleByToken(obj, v)
|
|
if not type(r) is unicode:
|
|
r = unicode(r, 'utf-8')
|
|
return r
|
|
else:
|
|
return v
|
|
return None
|
|
|
|
grok.global_adapter(contactFuzzyIndexer, name="MailContactFuzzy")
|
|
|