[Checkins] SVN: lovely.rating/trunk/ Added events when ratings are added or removed

Juergen Kartnaller juergen at kartnaller.at
Fri Sep 7 08:10:05 EDT 2007


Log message for revision 79518:
  Added events when ratings are added or removed
  

Changed:
  U   lovely.rating/trunk/CHANGES.txt
  D   lovely.rating/trunk/setup.cfg
  U   lovely.rating/trunk/setup.py
  U   lovely.rating/trunk/src/lovely/rating/README.txt
  U   lovely.rating/trunk/src/lovely/rating/manager.py
  U   lovely.rating/trunk/src/lovely/rating/tests.py

-=-
Modified: lovely.rating/trunk/CHANGES.txt
===================================================================
--- lovely.rating/trunk/CHANGES.txt	2007-09-07 09:57:52 UTC (rev 79517)
+++ lovely.rating/trunk/CHANGES.txt	2007-09-07 12:10:03 UTC (rev 79518)
@@ -2,6 +2,13 @@
  Changes for lovely.rating package
 ===================================
 
+
+2007/09/07 0.3.0
+=================
+
+- Added events when ratings are added or removed
+
+
 After 0.1 (trunk is 0.2)
 ========================
 

Deleted: lovely.rating/trunk/setup.cfg
===================================================================
--- lovely.rating/trunk/setup.cfg	2007-09-07 09:57:52 UTC (rev 79517)
+++ lovely.rating/trunk/setup.cfg	2007-09-07 12:10:03 UTC (rev 79518)
@@ -1,2 +0,0 @@
-[egg_info]
-tag_svn_revision = 1
\ No newline at end of file

Modified: lovely.rating/trunk/setup.py
===================================================================
--- lovely.rating/trunk/setup.py	2007-09-07 09:57:52 UTC (rev 79517)
+++ lovely.rating/trunk/setup.py	2007-09-07 12:10:03 UTC (rev 79518)
@@ -2,7 +2,7 @@
 from setuptools import setup, find_packages
 
 setup(name='lovely.rating',
-      version='0.2',
+      version='0.3.0',
       author = "Lovelysystems",
       author_email = "office at lovelysystems.com",
       description = "A rating engine for zope 3",

Modified: lovely.rating/trunk/src/lovely/rating/README.txt
===================================================================
--- lovely.rating/trunk/src/lovely/rating/README.txt	2007-09-07 09:57:52 UTC (rev 79517)
+++ lovely.rating/trunk/src/lovely/rating/README.txt	2007-09-07 12:10:03 UTC (rev 79518)
@@ -71,6 +71,9 @@
   >>> zope.component.provideUtility(
   ...     usability, IRatingDefinition, name='usability')
 
+  >>> from zope.component import eventtesting
+  >>> eventtesting.clearEvents()
+
 We are finally ready to rate KDE for usability, note that the rate
 method returns True if a change occured:
 
@@ -79,6 +82,15 @@
   >>> manager.rate('usability', u'Okay', u'kartnaller')
   True
 
+We get events when we rate:
+
+  >>> from pprint import pprint
+  >>> pprint(eventtesting.getEvents())
+  [<zope.app.container.contained.ObjectAddedEvent object at ...>,
+   <zope.app.container.contained.ObjectAddedEvent object at ...>,
+   <zope.app.container.contained.ObjectAddedEvent object at ...>,
+   <zope.app.container.contained.ObjectAddedEvent object at ...>]
+
 The ``rate()`` method's arguments are the id of the rating definition, the
 value and the user id of the user making the rating. Note that you cannot add
 invalid ratings:
@@ -202,12 +214,20 @@
   >>> manager.getRating('usability', u'badcarma')
   <Rating u'Crap' by u'badcarma'>
 
+  >>> eventtesting.clearEvents()
+
   >>> manager.remove('usability', 'badcarma')
   True
   >>> manager.remove('usability', 'badcarma')
   False
   >>> manager.getRating('usability', u'badcarma')
 
+We also get events if a rating is removed.
+
+  >>> pprint(eventtesting.getEvents())
+  [<zope.app.container.contained.ObjectRemovedEvent object at ...>,
+   <zope.app.container.contained.ObjectRemovedEvent object at ...>]
+
 Finally, the manager also provides some basic statistical features:
 
   >>> manager.computeAverage('usability')

Modified: lovely.rating/trunk/src/lovely/rating/manager.py
===================================================================
--- lovely.rating/trunk/src/lovely/rating/manager.py	2007-09-07 09:57:52 UTC (rev 79517)
+++ lovely.rating/trunk/src/lovely/rating/manager.py	2007-09-07 12:10:03 UTC (rev 79518)
@@ -16,16 +16,24 @@
 $Id$
 """
 __docformat__ = "reStructuredText"
+
 import persistent
+
+from zope import annotation
 import zope.component
 import zope.interface
+import zope.event
+
 from BTrees import OOBTree
-from zope import annotation
+
+from zope.app.container.contained import ObjectAddedEvent, ObjectRemovedEvent
+
 from zope.app.container import contained
 
 from lovely.rating import IRatable, IRatingsManager, IRatingDefinition, rating
 import itertools
 
+
 class RatingsManager(contained.Contained, persistent.Persistent):
     zope.interface.implements(IRatingsManager)
     zope.component.adapts(IRatable)
@@ -55,7 +63,9 @@
         if existing is not None and existing.value == value:
             # do nothing if no change
             return False
-        self._storage[id][user] = rating.Rating(id, value, user)
+        value = rating.Rating(id, value, user)
+        self._storage[id][user] = value
+        zope.event.notify(ObjectAddedEvent(value))
         return True
 
     def remove(self, id, user):
@@ -65,6 +75,8 @@
 
         if id not in self._storage or user not in self._storage[id]:
             return False
+        value = self._storage[id][user]
+        zope.event.notify(ObjectRemovedEvent(value))
         del self._storage[id][user]
         if len(self._storage[id]) == 0:
             del self._storage[id]
@@ -124,7 +136,6 @@
         """See interfaces.IRatingManager"""
         ratings = list(self._storage.get(id, {}).values())
         return len(ratings)
-        
 
     def __repr__(self):
         return '<%s for %r>' %(self.__class__.__name__, self.__parent__)

Modified: lovely.rating/trunk/src/lovely/rating/tests.py
===================================================================
--- lovely.rating/trunk/src/lovely/rating/tests.py	2007-09-07 09:57:52 UTC (rev 79517)
+++ lovely.rating/trunk/src/lovely/rating/tests.py	2007-09-07 12:10:03 UTC (rev 79518)
@@ -16,14 +16,20 @@
 $Id$
 """
 __docformat__ = "reStructuredText"
+
 import doctest
 import unittest
+
 from zope.testing.doctestunit import DocFileSuite
+from zope.component import eventtesting
+
 from zope.app.testing import setup
 
+
 def setUp(test):
     root = setup.placefulSetUp(True)
     test.globs['root'] = root
+    eventtesting.setUp()
 
 def tearDown(test):
     setup.placefulTearDown()



More information about the Checkins mailing list