2016-02-05 01:39:53 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2009-05-11 18:52:16 +02:00
|
|
|
"""Catalog indexers, using plone.indexer. These will populate standard catalog
|
|
|
|
indexes with values based on the IComment interface.
|
|
|
|
|
|
|
|
Also provide event handlers to actually catalog the comments.
|
|
|
|
"""
|
2009-06-02 01:17:13 +02:00
|
|
|
from DateTime import DateTime
|
2016-02-05 01:39:53 +01:00
|
|
|
from plone.app.discussion.interfaces import IComment
|
|
|
|
from plone.app.discussion.interfaces import IConversation
|
|
|
|
from plone.indexer import indexer
|
|
|
|
from plone.uuid.interfaces import IUUID
|
2009-07-01 22:57:55 +02:00
|
|
|
from Products.CMFCore.interfaces import IContentish
|
2011-07-25 08:47:45 +02:00
|
|
|
from Products.CMFPlone.utils import safe_unicode
|
2009-07-01 22:57:55 +02:00
|
|
|
from Products.ZCatalog.interfaces import IZCatalog
|
2015-05-03 08:16:39 +02:00
|
|
|
from string import join
|
2011-04-22 16:59:59 +02:00
|
|
|
|
2010-12-15 21:39:55 +01:00
|
|
|
|
2010-08-28 21:48:40 +02:00
|
|
|
MAX_DESCRIPTION = 25
|
2009-06-01 11:59:07 +02:00
|
|
|
|
2009-07-01 22:57:55 +02:00
|
|
|
# Conversation Indexers
|
|
|
|
|
2012-01-14 07:41:50 +01:00
|
|
|
|
2009-07-01 22:57:55 +02:00
|
|
|
@indexer(IContentish, IZCatalog)
|
|
|
|
def total_comments(object):
|
2009-10-17 10:28:50 +02:00
|
|
|
# Total number of comments on a conversation
|
|
|
|
# Indexers won't work on old discussion items
|
|
|
|
if object.meta_type != 'Discussion Item':
|
2010-07-22 01:36:16 +02:00
|
|
|
try:
|
|
|
|
conversation = IConversation(object)
|
2015-02-16 10:07:02 +01:00
|
|
|
return conversation.total_comments()
|
2012-01-14 07:41:50 +01:00
|
|
|
except TypeError: # pragma: no cover
|
2010-07-22 01:36:16 +02:00
|
|
|
# The item is contentish but nobody
|
|
|
|
# implemented an adapter for it
|
|
|
|
pass
|
2009-07-01 22:57:55 +02:00
|
|
|
|
2012-01-14 07:41:50 +01:00
|
|
|
|
2009-07-01 22:57:55 +02:00
|
|
|
@indexer(IContentish, IZCatalog)
|
|
|
|
def last_comment_date(object):
|
2009-10-17 10:28:50 +02:00
|
|
|
# Date of the latest comment on a conversation
|
|
|
|
# Indexers won't work on old discussion items
|
|
|
|
if object.meta_type != 'Discussion Item':
|
2010-07-22 01:36:16 +02:00
|
|
|
try:
|
|
|
|
conversation = IConversation(object)
|
|
|
|
return conversation.last_comment_date
|
2012-01-14 07:41:50 +01:00
|
|
|
except TypeError: # pragma: no cover
|
2010-07-22 01:36:16 +02:00
|
|
|
# The item is contentish but nobody
|
|
|
|
# implemented an adapter for it
|
|
|
|
pass
|
2009-07-01 22:57:55 +02:00
|
|
|
|
2012-01-14 07:41:50 +01:00
|
|
|
|
2009-07-01 22:57:55 +02:00
|
|
|
@indexer(IContentish, IZCatalog)
|
|
|
|
def commentators(object):
|
2009-10-17 10:28:50 +02:00
|
|
|
# List of commentators on a conversation
|
|
|
|
# Indexers won't work on old discussion items
|
|
|
|
if object.meta_type != 'Discussion Item':
|
2010-07-22 01:36:16 +02:00
|
|
|
try:
|
|
|
|
conversation = IConversation(object)
|
2013-03-28 14:28:22 +01:00
|
|
|
return conversation.public_commentators
|
2012-01-14 07:41:50 +01:00
|
|
|
except TypeError: # pragma: no cover
|
2010-07-22 01:36:16 +02:00
|
|
|
# The item is contentish but nobody
|
|
|
|
# implemented an adapter for it
|
|
|
|
pass
|
2009-07-01 22:57:55 +02:00
|
|
|
|
|
|
|
# Comment Indexers
|
|
|
|
|
2012-01-14 07:41:50 +01:00
|
|
|
|
2009-05-31 19:55:46 +02:00
|
|
|
@indexer(IComment)
|
2009-06-02 01:45:49 +02:00
|
|
|
def title(object):
|
2010-09-29 09:56:36 +02:00
|
|
|
return object.Title()
|
2009-05-31 19:55:46 +02:00
|
|
|
|
2012-01-14 07:41:50 +01:00
|
|
|
|
2009-06-02 01:29:09 +02:00
|
|
|
@indexer(IComment)
|
2009-06-02 01:45:49 +02:00
|
|
|
def creator(object):
|
2011-07-25 10:12:04 +02:00
|
|
|
return object.creator and safe_unicode(object.creator).encode('utf-8')
|
2009-06-02 01:29:09 +02:00
|
|
|
|
2012-01-14 07:41:50 +01:00
|
|
|
|
2009-05-31 21:07:35 +02:00
|
|
|
@indexer(IComment)
|
2009-06-02 01:45:49 +02:00
|
|
|
def description(object):
|
2009-07-01 12:01:37 +02:00
|
|
|
# Return the first 25 words of the comment text and append ' [...]'
|
2017-07-29 11:35:15 +02:00
|
|
|
text = join(object.getText(targetMimetype='text/plain').split()[:MAX_DESCRIPTION])
|
2011-04-02 21:51:37 +02:00
|
|
|
if len(object.getText().split()) > 25:
|
2016-02-05 01:39:53 +01:00
|
|
|
text += ' [...]'
|
2009-07-01 12:01:37 +02:00
|
|
|
return text
|
2009-05-31 21:07:35 +02:00
|
|
|
|
2012-01-14 07:41:50 +01:00
|
|
|
|
2009-05-31 19:55:46 +02:00
|
|
|
@indexer(IComment)
|
2009-06-02 01:45:49 +02:00
|
|
|
def searchable_text(object):
|
2011-04-02 21:51:37 +02:00
|
|
|
return object.getText(targetMimetype='text/plain')
|
2009-05-31 19:55:46 +02:00
|
|
|
|
2012-01-14 07:41:50 +01:00
|
|
|
|
2009-07-03 10:03:09 +02:00
|
|
|
@indexer(IComment)
|
|
|
|
def in_response_to(object):
|
2009-10-16 14:11:58 +02:00
|
|
|
# Always returns the content object the comment is added to.
|
|
|
|
# Do not confuse this with the in_reply_to attribute of a comment!
|
2009-07-03 10:03:09 +02:00
|
|
|
return object.__parent__.__parent__.title_or_id()
|
|
|
|
|
2012-01-14 07:41:50 +01:00
|
|
|
|
2009-06-02 01:17:13 +02:00
|
|
|
@indexer(IComment)
|
|
|
|
def effective(object):
|
|
|
|
# the catalog index needs Zope DateTime instead of Python datetime
|
2017-07-28 17:58:35 +02:00
|
|
|
return DateTime(
|
|
|
|
object.creation_date.year,
|
|
|
|
object.creation_date.month,
|
|
|
|
object.creation_date.day,
|
|
|
|
object.creation_date.hour,
|
|
|
|
object.creation_date.minute,
|
|
|
|
object.creation_date.second,
|
|
|
|
'GMT',
|
|
|
|
)
|
2009-06-02 01:17:13 +02:00
|
|
|
|
2012-01-14 07:41:50 +01:00
|
|
|
|
2009-06-02 01:17:13 +02:00
|
|
|
@indexer(IComment)
|
|
|
|
def created(object):
|
|
|
|
# the catalog index needs Zope DateTime instead of Python datetime
|
2017-07-28 17:58:35 +02:00
|
|
|
return DateTime(
|
|
|
|
object.creation_date.year,
|
|
|
|
object.creation_date.month,
|
|
|
|
object.creation_date.day,
|
|
|
|
object.creation_date.hour,
|
|
|
|
object.creation_date.minute,
|
|
|
|
object.creation_date.second,
|
|
|
|
'GMT',
|
|
|
|
)
|
2009-06-02 01:17:13 +02:00
|
|
|
|
2012-01-14 07:41:50 +01:00
|
|
|
|
2009-06-02 01:17:13 +02:00
|
|
|
@indexer(IComment)
|
|
|
|
def modified(object):
|
|
|
|
# the catalog index needs Zope DateTime instead of Python datetime
|
2017-07-28 17:58:35 +02:00
|
|
|
return DateTime(
|
|
|
|
object.modification_date.year,
|
|
|
|
object.modification_date.month,
|
|
|
|
object.modification_date.day,
|
|
|
|
object.modification_date.hour,
|
|
|
|
object.modification_date.minute,
|
|
|
|
object.modification_date.second,
|
|
|
|
'GMT',
|
|
|
|
)
|
2009-07-06 18:56:09 +02:00
|
|
|
|
2012-01-14 07:41:50 +01:00
|
|
|
|
2009-07-06 18:56:09 +02:00
|
|
|
# Override the conversation indexers for comments
|
|
|
|
|
2012-01-14 07:41:50 +01:00
|
|
|
|
2009-07-06 18:56:09 +02:00
|
|
|
@indexer(IComment)
|
|
|
|
def comments_total_comments(object):
|
|
|
|
return None
|
|
|
|
|
2012-01-14 07:41:50 +01:00
|
|
|
|
2009-07-06 18:56:09 +02:00
|
|
|
@indexer(IComment)
|
|
|
|
def comments_last_comment_date(object):
|
|
|
|
return None
|
|
|
|
|
2012-01-14 07:41:50 +01:00
|
|
|
|
2009-07-06 18:56:09 +02:00
|
|
|
@indexer(IComment)
|
|
|
|
def comments_commentators(object):
|
|
|
|
return None
|
2010-12-15 21:39:55 +01:00
|
|
|
|
2012-01-14 07:41:50 +01:00
|
|
|
|
2010-12-15 21:39:55 +01:00
|
|
|
# Make sure comments don't inherit their container's UID
|
|
|
|
@indexer(IComment)
|
|
|
|
def UID(object):
|
|
|
|
if IUUID:
|
|
|
|
return IUUID(object, None)
|