diff --git a/plone/app/discussion/contentrules.py b/plone/app/discussion/contentrules.py index 0dd6681..4f2b58e 100644 --- a/plone/app/discussion/contentrules.py +++ b/plone/app/discussion/contentrules.py @@ -1,8 +1,101 @@ """ Content rules handlers """ -from plone.app.contentrules.handlers import execute +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'') diff --git a/plone/app/discussion/contentrules.zcml b/plone/app/discussion/contentrules.zcml index 8b8edc6..c3d1108 100644 --- a/plone/app/discussion/contentrules.zcml +++ b/plone/app/discussion/contentrules.zcml @@ -57,4 +57,40 @@ + + + + + + + + + + + diff --git a/plone/app/discussion/events.py b/plone/app/discussion/events.py index 31253b7..30d49a1 100644 --- a/plone/app/discussion/events.py +++ b/plone/app/discussion/events.py @@ -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 """