109 lines
3.4 KiB
Python
109 lines
3.4 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]
|
|
if ':' in v and v.split(':')[0] in ('deputy', 'ministry', 'ministry-collab'):
|
|
return v
|
|
elif ':' in v and v.split(':')[0] == 'contact':
|
|
contact_id = v.split(':')[1]
|
|
if not hasattr(contacts_dir, contact_id):
|
|
return contact_id.replace('-', ' ')
|
|
else:
|
|
return getattr(contacts_dir, contact_id).title
|
|
else:
|
|
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()
|
|
return src(obj).getTermByToken(v).title
|
|
else:
|
|
return v
|
|
return None
|
|
|
|
grok.global_adapter(contactFuzzyIndexer, name="MailContactFuzzy")
|
|
|