Add custom content rules string substitutions

- Add content rules string substitutions for comment:
  - id
  - text
  - author email
  - author user name
  - author full name
This commit is contained in:
Alin Voinea 2014-04-17 18:38:44 +03:00
parent ab926d6a10
commit 13830715a7
3 changed files with 142 additions and 1 deletions

View File

@ -1,8 +1,101 @@
""" Content rules handlers
"""
from plone.app.discussion import PloneAppDiscussionMessageFactory as _
try:
from plone.stringinterp.adapters import BaseSubstitution
except ImportError:
class BaseSubstitution(object):
""" Fallback class if plone.stringinterp is not available
"""
def __init__(self, context, **kwargs):
self.context = context
try:
from plone.app.contentrules.handlers import execute
except ImportError:
execute = lambda context, event: False
def execute_comment(event):
""" Execute comment content rules
"""
execute(event.object, event)
class CommentSubstitution(BaseSubstitution):
""" Comment string substitution
"""
def __init__(self, context, **kwargs):
super(CommentSubstitution, self).__init__(context, **kwargs)
self._session = None
@property
def session(self):
""" User session
"""
if self._session is None:
sdm = getattr(self.context, 'session_data_manager', None)
self._session = sdm.getSessionData(create=False) if sdm else {}
return self._session
@property
def comment(self):
""" Get changed inline comment
"""
return self.session.get('comment', {})
class Id(CommentSubstitution):
""" Comment id string substitution
"""
category = _(u'Comments')
description = _(u'Comment id')
def safe_call(self):
""" Safe call
"""
return self.comment.get('comment_id', u'')
class Text(CommentSubstitution):
""" Comment text
"""
category = _(u'Comments')
description = _(u'Comment text')
def safe_call(self):
""" Safe call
"""
return self.comment.get('text', u'')
class AuthorUserName(CommentSubstitution):
""" Comment author user name string substitution
"""
category = _(u'Comments')
description = _(u'Comment author user name')
def safe_call(self):
""" Safe call
"""
return self.comment.get('author_username', u'')
class AuthorFullName(CommentSubstitution):
""" Comment author full name string substitution
"""
category = _(u'Comments')
description = _(u'Comment author full name')
def safe_call(self):
""" Safe call
"""
return self.comment.get('author_name', u'')
class AuthorEmail(CommentSubstitution):
""" Comment author email string substitution
"""
category = _(u'Comments')
description = _(u'Comment author email')
def safe_call(self):
""" Safe call
"""
return self.comment.get('author_email', u'')

View File

@ -57,4 +57,40 @@
</configure>
<!-- Content rules strings -->
<adapter
for="zope.interface.Interface"
provides="plone.stringinterp.interfaces.IStringSubstitution"
factory=".contentrules.Id"
name="comment_id"
/>
<adapter
for="zope.interface.Interface"
provides="plone.stringinterp.interfaces.IStringSubstitution"
factory=".contentrules.Text"
name="comment_text"
/>
<adapter
for="zope.interface.Interface"
provides="plone.stringinterp.interfaces.IStringSubstitution"
factory=".contentrules.AuthorUserName"
name="comment_user_id"
/>
<adapter
for="zope.interface.Interface"
provides="plone.stringinterp.interfaces.IStringSubstitution"
factory=".contentrules.AuthorFullName"
name="comment_user_fullname"
/>
<adapter
for="zope.interface.Interface"
provides="plone.stringinterp.interfaces.IStringSubstitution"
factory=".contentrules.AuthorEmail"
name="comment_user_email"
/>
</configure>

View File

@ -1,6 +1,7 @@
""" Custom discussion events
"""
from zope.interface import implements
from plone.app.discussion.interfaces import IComment
from plone.app.discussion.interfaces import IDiscussionEvent
from plone.app.discussion.interfaces import ICommentAddedEvent
from plone.app.discussion.interfaces import ICommentRemovedEvent
@ -18,6 +19,17 @@ class DiscussionEvent(object):
for key, value in kwargs.items():
setattr(self, key, value)
# Add comment on session to easily define content-rules dynamic strings
sdm = getattr(context, 'session_data_manager', None)
session = sdm.getSessionData(create=True) if sdm else None
if session:
sessionComment = dict(
(field, getattr(comment, field, None)) for field in IComment
if not field.startswith('_')
)
session.set('comment', sessionComment)
class CommentAddedEvent(DiscussionEvent):
""" Event to be triggered when a Comment is added
"""