Skip to content

Commit

Permalink
Add a marker interface to objects
Browse files Browse the repository at this point in the history
Apply it to objects that are reported to have stop words,
and only the ones that are within the scope of a contentrule that
has the ``collective.contentalerts.TextAlert`` condition on it.

This is the building block for a view to show all objects that have
stop words on it.
  • Loading branch information
gforcada committed Aug 14, 2015
1 parent 6543559 commit 5d7a90a
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/collective/contentalerts/contentrules.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
from OFS.SimpleItem import SimpleItem
from collective.contentalerts import _
from collective.contentalerts.interfaces import IAlert
from collective.contentalerts.interfaces import IHasStopWords
from collective.contentalerts.interfaces import ITextAlertCondition
from plone.app.contentrules.browser.formhelper import AddForm
from plone.app.contentrules.browser.formhelper import EditForm
from plone.contentrules.rule.interfaces import IRuleElementData
from plone.stringinterp.adapters import BaseSubstitution
from zope.component import getUtility
from zope.formlib import form
from zope.interface import alsoProvides
from zope.interface import implementer
from zope.interface import noLongerProvides


class TextAlertConditionExecutor(object):
Expand Down Expand Up @@ -45,7 +48,20 @@ def __call__(self):

alert_utility = getUtility(IAlert)

return alert_utility.has_stop_words(text, stop_words=stop_words)
ret_value = alert_utility.has_stop_words(text, stop_words=stop_words)
self._apply_marker_interface(ret_value)
return ret_value

def _apply_marker_interface(self, has_stop_words):
if getattr(self.event, 'comment', None):
obj = self.event.comment
else:
obj = self.event.object

if has_stop_words:
alsoProvides(obj, IHasStopWords)
elif IHasStopWords.providedBy(obj):
noLongerProvides(obj, IHasStopWords)


@implementer(ITextAlertCondition, IRuleElementData)
Expand Down
4 changes: 4 additions & 0 deletions src/collective/contentalerts/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,7 @@ class ITextAlertCondition(Interface):
),
required=False,
)


class IHasStopWords(Interface):
"""Marker interface attached to objects that have stop words."""
59 changes: 59 additions & 0 deletions src/collective/contentalerts/tests/test_contentrules.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from Testing.ZopeTestCase.utils import setupCoreSessions
from collective.contentalerts.contentrules import TextAlertCondition
from collective.contentalerts.contentrules import TextAlertConditionEditForm
from collective.contentalerts.interfaces import IHasStopWords
from collective.contentalerts.interfaces import IStopWords
from collective.contentalerts.testing import COLLECTIVE_CONTENTALERTS_DEXTERITY_INTEGRATION_TESTING # noqa
from collective.contentalerts.testing import COLLECTIVE_CONTENTALERTS_INTEGRATION_TESTING # noqa
Expand Down Expand Up @@ -312,6 +313,64 @@ def test_stop_words_not_in_request(self):
executable()
self.assertIsNone(self.request.get('stop_words'))

def test_has_stop_words_add_interface_comment(self):
comment = self._add_comment('one alert')
condition = TextAlertCondition()
condition.stop_words = u'one alert\nanother alert'

executable = getMultiAdapter(
(self.portal, condition, CommentDummyEvent(comment)),
IExecutable
)
executable()
self.assertTrue(IHasStopWords.providedBy(comment))

def test_has_stop_words_add_interface_document(self):
name = self.portal.invokeFactory(
id='doc2',
title='Document 1',
type_name='Document'
)
document = self.portal[name]
document.setText('this gives one alert')
condition = TextAlertCondition()
condition.stop_words = u'one alert\nanother alert'

executable = getMultiAdapter(
(self.portal, condition, ContentTypeDummyEvent(document)),
IExecutable
)
executable()
self.assertTrue(IHasStopWords.providedBy(document))

def test_no_stop_words_no_interface(self):
comment = self._add_comment('no alert')
condition = TextAlertCondition()
condition.stop_words = u'one alert\nanother alert'

executable = getMultiAdapter(
(self.portal, condition, CommentDummyEvent(comment)),
IExecutable
)
executable()
self.assertFalse(IHasStopWords.providedBy(comment))

def test_add_and_remove_interface(self):
comment = self._add_comment('one alert')
condition = TextAlertCondition()
condition.stop_words = u'one alert\nanother alert'

# adds the marker interface
executable = getMultiAdapter(
(self.portal, condition, CommentDummyEvent(comment)),
IExecutable
)
executable()

comment.text = 'no longer creating an alert'
executable()
self.assertFalse(IHasStopWords.providedBy(comment))


class DexterityTextAlertConditionTestCase(unittest.TestCase):
layer = COLLECTIVE_CONTENTALERTS_DEXTERITY_INTEGRATION_TESTING
Expand Down

0 comments on commit 5d7a90a

Please sign in to comment.