From ee28528f556a1e44dc7f65a5c5715ba6e2510718 Mon Sep 17 00:00:00 2001 From: Vincent Fretin Date: Sat, 20 Sep 2014 14:14:19 +0200 Subject: [PATCH] Don't use session at all for content rules, set the event in the request to be able to access comment attributes. This fixes 'InvalidObjectReference: A new object is reachable from multiple databases.' if comment contains a RichTextValue which contains a RawValueHolder (persistent object). --- plone/app/discussion/contentrules.py | 22 ++++++++----------- plone/app/discussion/events.py | 14 ++++-------- .../app/discussion/tests/test_contentrules.py | 8 ------- 3 files changed, 13 insertions(+), 31 deletions(-) diff --git a/plone/app/discussion/contentrules.py b/plone/app/discussion/contentrules.py index 4f2b58e..dfa00fc 100644 --- a/plone/app/discussion/contentrules.py +++ b/plone/app/discussion/contentrules.py @@ -28,22 +28,18 @@ class CommentSubstitution(BaseSubstitution): """ def __init__(self, context, **kwargs): super(CommentSubstitution, self).__init__(context, **kwargs) - self._session = None @property - def session(self): - """ User session + def event(self): + """ event that triggered the content rule """ - 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 + return self.context.REQUEST.get('event') @property def comment(self): """ Get changed inline comment """ - return self.session.get('comment', {}) + return self.event.comment class Id(CommentSubstitution): """ Comment id string substitution @@ -54,7 +50,7 @@ class Id(CommentSubstitution): def safe_call(self): """ Safe call """ - return self.comment.get('comment_id', u'') + return getattr(self.comment, 'comment_id', u'') class Text(CommentSubstitution): """ Comment text @@ -65,7 +61,7 @@ class Text(CommentSubstitution): def safe_call(self): """ Safe call """ - return self.comment.get('text', u'') + return getattr(self.comment, 'text', u'') class AuthorUserName(CommentSubstitution): """ Comment author user name string substitution @@ -76,7 +72,7 @@ class AuthorUserName(CommentSubstitution): def safe_call(self): """ Safe call """ - return self.comment.get('author_username', u'') + return getattr(self.comment, 'author_username', u'') class AuthorFullName(CommentSubstitution): """ Comment author full name string substitution @@ -87,7 +83,7 @@ class AuthorFullName(CommentSubstitution): def safe_call(self): """ Safe call """ - return self.comment.get('author_name', u'') + return getattr(self.comment, 'author_name', u'') class AuthorEmail(CommentSubstitution): """ Comment author email string substitution @@ -98,4 +94,4 @@ class AuthorEmail(CommentSubstitution): def safe_call(self): """ Safe call """ - return self.comment.get('author_email', u'') + return getattr(self.comment, 'author_email', u'') diff --git a/plone/app/discussion/events.py b/plone/app/discussion/events.py index 30d49a1..804077a 100644 --- a/plone/app/discussion/events.py +++ b/plone/app/discussion/events.py @@ -19,16 +19,10 @@ 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) + # Add event to the request to be able to access comment attributes + # in content-rules dynamic strings + request = context.REQUEST + request.set('event', self) class CommentAddedEvent(DiscussionEvent): """ Event to be triggered when a Comment is added diff --git a/plone/app/discussion/tests/test_contentrules.py b/plone/app/discussion/tests/test_contentrules.py index 16618e2..a9e9bf1 100644 --- a/plone/app/discussion/tests/test_contentrules.py +++ b/plone/app/discussion/tests/test_contentrules.py @@ -1,9 +1,7 @@ # -*- coding: utf-8 -*- import unittest2 as unittest -from Testing import ZopeTestCase as ztc from zope.component import createObject, getAdapter -from zope.event import notify from plone.app.testing import TEST_USER_ID, setRoles from plone.contentrules.rule.interfaces import IRuleEventType @@ -25,9 +23,6 @@ class CommentContentRulesTest(unittest.TestCase): layer = PLONE_APP_DISCUSSION_INTEGRATION_TESTING def setUp(self): - # Setup session manager - ztc.utils.setupCoreSessions(self.layer['app']) - # Setup sandbox self.portal = self.layer['portal'] self.request = self.layer['request'] @@ -94,9 +89,6 @@ class ReplyContentRulesTest(unittest.TestCase): layer = PLONE_APP_DISCUSSION_INTEGRATION_TESTING def setUp(self): - # Setup session manager - ztc.utils.setupCoreSessions(self.layer['app']) - # Setup sandbox self.portal = self.layer['portal'] self.request = self.layer['request']