plone.app.discussion/plone/app/discussion/catalog.py

167 lines
4.4 KiB
Python
Raw Normal View History

"""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.
"""
from DateTime import DateTime
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
from Products.CMFCore.interfaces import IContentish
from Products.CMFPlone.utils import safe_unicode
from Products.ZCatalog.interfaces import IZCatalog
2022-05-01 23:14:00 +02:00
MAX_DESCRIPTION = 25
# Conversation Indexers
2012-01-14 07:41:50 +01:00
@indexer(IContentish, IZCatalog)
def total_comments(object):
# Total number of comments on a conversation
# Indexers won't work on old discussion items
2022-05-01 23:14:09 +02:00
if object.meta_type != "Discussion Item":
try:
conversation = IConversation(object)
return conversation.total_comments()
2012-01-14 07:41:50 +01:00
except TypeError: # pragma: no cover
# The item is contentish but nobody
# implemented an adapter for it
pass
2012-01-14 07:41:50 +01:00
@indexer(IContentish, IZCatalog)
def last_comment_date(object):
# Date of the latest comment on a conversation
# Indexers won't work on old discussion items
2022-05-01 23:14:09 +02:00
if object.meta_type != "Discussion Item":
try:
conversation = IConversation(object)
return conversation.last_comment_date
2012-01-14 07:41:50 +01:00
except TypeError: # pragma: no cover
# The item is contentish but nobody
# implemented an adapter for it
pass
2012-01-14 07:41:50 +01:00
@indexer(IContentish, IZCatalog)
def commentators(object):
# List of commentators on a conversation
# Indexers won't work on old discussion items
2022-05-01 23:14:09 +02:00
if object.meta_type != "Discussion Item":
try:
conversation = IConversation(object)
return conversation.public_commentators
2012-01-14 07:41:50 +01:00
except TypeError: # pragma: no cover
# The item is contentish but nobody
# implemented an adapter for it
pass
2022-05-01 23:14:09 +02:00
# Comment Indexers
2012-01-14 07:41:50 +01:00
@indexer(IComment)
def title(object):
return object.Title()
2012-01-14 07:41:50 +01:00
@indexer(IComment)
def creator(object):
2018-05-05 20:25:07 +02:00
if not object.creator:
return
value = safe_unicode(object.creator)
return value
2012-01-14 07:41:50 +01:00
@indexer(IComment)
def description(object):
# Return the first 25 words of the comment text and append ' [...]'
2022-05-01 23:14:09 +02:00
text = " ".join(
object.getText(targetMimetype="text/plain").split()[:MAX_DESCRIPTION],
)
if len(object.getText().split()) > 25:
2022-05-01 23:14:09 +02:00
text += " [...]"
return text
2012-01-14 07:41:50 +01:00
@indexer(IComment)
def searchable_text(object):
2022-05-01 23:14:09 +02:00
return object.getText(targetMimetype="text/plain")
2012-01-14 07:41:50 +01:00
@indexer(IComment)
def in_response_to(object):
# Always returns the content object the comment is added to.
# Do not confuse this with the in_reply_to attribute of a comment!
return object.__parent__.__parent__.title_or_id()
2012-01-14 07:41:50 +01: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(
2022-05-01 23:14:09 +02:00
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",
)
2012-01-14 07:41:50 +01: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(
2022-05-01 23:14:09 +02:00
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",
)
2012-01-14 07:41:50 +01: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(
2022-05-01 23:14:09 +02:00
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",
)
2012-01-14 07:41:50 +01:00
# Override the conversation indexers for comments
2012-01-14 07:41:50 +01:00
@indexer(IComment)
def comments_total_comments(object):
return None
2012-01-14 07:41:50 +01:00
@indexer(IComment)
def comments_last_comment_date(object):
return None
2012-01-14 07:41:50 +01:00
@indexer(IComment)
def comments_commentators(object):
return None
2012-01-14 07:41:50 +01:00
# Make sure comments don't inherit their container's UID
@indexer(IComment)
def UID(object):
if IUUID:
return IUUID(object, None)