[Checkins] SVN: lovely.rating/trunk/ - added rating specific events
Juergen Kartnaller
juergen at kartnaller.at
Mon Sep 10 05:15:30 EDT 2007
Log message for revision 79549:
- added rating specific events
- reverted change from version 0.3.0
Changed:
U lovely.rating/trunk/CHANGES.txt
U lovely.rating/trunk/setup.py
U lovely.rating/trunk/src/lovely/rating/README.txt
U lovely.rating/trunk/src/lovely/rating/interfaces.py
U lovely.rating/trunk/src/lovely/rating/manager.py
-=-
Modified: lovely.rating/trunk/CHANGES.txt
===================================================================
--- lovely.rating/trunk/CHANGES.txt 2007-09-10 06:30:15 UTC (rev 79548)
+++ lovely.rating/trunk/CHANGES.txt 2007-09-10 09:15:29 UTC (rev 79549)
@@ -3,8 +3,16 @@
===================================
+2007/09/07 0.3.1
+================
+
+- added rating specific events
+
+- reverted change from version 0.3.0
+
+
2007/09/07 0.3.0
-=================
+================
- Added events when ratings are added or removed
Modified: lovely.rating/trunk/setup.py
===================================================================
--- lovely.rating/trunk/setup.py 2007-09-10 06:30:15 UTC (rev 79548)
+++ lovely.rating/trunk/setup.py 2007-09-10 09:15:29 UTC (rev 79549)
@@ -2,7 +2,7 @@
from setuptools import setup, find_packages
setup(name='lovely.rating',
- version='0.3.0',
+ version='0.3.1',
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-10 06:30:15 UTC (rev 79548)
+++ lovely.rating/trunk/src/lovely/rating/README.txt 2007-09-10 09:15:29 UTC (rev 79549)
@@ -86,11 +86,30 @@
>>> 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 ...>]
+ [<lovely.rating.interfaces.RatingAddedEvent object at ...>,
+ <lovely.rating.interfaces.RatingAddedEvent object at ...>,
+ <lovely.rating.interfaces.RatingAddedEvent object at ...>,
+ <lovely.rating.interfaces.RatingAddedEvent object at ...>]
+Setting a rating with the aready existing value doesn't fire an event.
+
+ >>> eventtesting.clearEvents()
+ >>> manager.rate('usability', u'Okay', u'srichter')
+ False
+ >>> pprint(eventtesting.getEvents())
+ []
+
+Changing a rating fires an event.
+
+ >>> manager.rate('usability', u'Good', u'srichter')
+ True
+ >>> pprint(eventtesting.getEvents())
+ [<lovely.rating.interfaces.RatingChangedEvent object at ...>,
+ <lovely.rating.interfaces.RatingChangedEvent object at ...>]
+
+ >>> manager.rate('usability', u'Okay', u'srichter')
+ True
+
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:
@@ -225,8 +244,8 @@
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 ...>]
+ [<lovely.rating.interfaces.RatingRemovedEvent object at ...>,
+ <lovely.rating.interfaces.RatingRemovedEvent object at ...>]
Finally, the manager also provides some basic statistical features:
Modified: lovely.rating/trunk/src/lovely/rating/interfaces.py
===================================================================
--- lovely.rating/trunk/src/lovely/rating/interfaces.py 2007-09-10 06:30:15 UTC (rev 79548)
+++ lovely.rating/trunk/src/lovely/rating/interfaces.py 2007-09-10 09:15:29 UTC (rev 79549)
@@ -123,7 +123,7 @@
The result will be a list of tuples of the type ``(score,
amount)``. ``score`` is in turn a tuple of ``(name, value)``.
"""
-
+
def countAmountRatings(id, dtMin=None, dtMax=None):
"""Counts the total amount of ratings for one definition"""
@@ -131,6 +131,11 @@
class IRating(zope.interface.Interface):
"""A single rating for a definition and user."""
+ id = zope.schema.TextLine(
+ title=u'Id',
+ description=u'The id of the rating used.',
+ required=True)
+
value = zope.schema.Object(
title=u'Value',
description=u'A scoresystem-valid score that represents the rating.',
@@ -145,3 +150,74 @@
timestamp = zope.schema.Datetime(
title=u'Timestamp',
description=u'The time the rating was given.')
+
+
+class IRatingEvent(zope.interface.Interface):
+ """An event for ratings"""
+
+ obj = zope.schema.Object(
+ title=u'Object',
+ description=u'The rated object.',
+ schema=zope.interface.Interface,
+ required=True)
+
+ user = zope.schema.TextLine(
+ title=u'User',
+ description=u'The id of the entity having made the rating.',
+ required=True)
+
+
+class RatingEvent(object):
+
+ def __init__(self, id, obj, user):
+ self.id = id
+ self.obj = obj
+ self.user = user
+
+
+class IRatingAddedEvent(IRatingEvent):
+ """A rating was added"""
+
+ value = zope.schema.Object(
+ title=u'Value',
+ description=u'A scoresystem-valid score that represents the rating.',
+ schema=zope.interface.Interface,
+ required=True)
+
+
+class RatingAddedEvent(RatingEvent):
+ """A rating was added to an object"""
+ zope.interface.implements(IRatingAddedEvent)
+
+ def __init__(self, id, obj, user, value):
+ super(RatingAddedEvent, self).__init__(id, obj, user)
+ self.value = value
+
+
+class IRatingChangedEvent(IRatingEvent):
+ """A rating was changed"""
+
+ value = zope.schema.Object(
+ title=u'Value',
+ description=u'A scoresystem-valid score that represents the rating.',
+ schema=zope.interface.Interface,
+ required=True)
+
+
+class RatingChangedEvent(RatingEvent):
+ """A rating was changed"""
+ zope.interface.implements(IRatingChangedEvent)
+
+ def __init__(self, id, obj, user, value):
+ super(RatingChangedEvent, self).__init__(id, obj, user)
+ self.value = value
+
+
+class IRatingRemovedEvent(IRatingEvent):
+ """A rating was removed"""
+
+
+class RatingRemovedEvent(RatingEvent):
+ """A rating was removed from an object"""
+ zope.interface.implements(IRatingRemovedEvent)
+
Modified: lovely.rating/trunk/src/lovely/rating/manager.py
===================================================================
--- lovely.rating/trunk/src/lovely/rating/manager.py 2007-09-10 06:30:15 UTC (rev 79548)
+++ lovely.rating/trunk/src/lovely/rating/manager.py 2007-09-10 09:15:29 UTC (rev 79549)
@@ -17,6 +17,7 @@
"""
__docformat__ = "reStructuredText"
+import itertools
import persistent
from zope import annotation
@@ -31,9 +32,10 @@
from zope.app.container import contained
from lovely.rating import IRatable, IRatingsManager, IRatingDefinition, rating
-import itertools
+import interfaces
+
class RatingsManager(contained.Contained, persistent.Persistent):
zope.interface.implements(IRatingsManager)
zope.component.adapts(IRatable)
@@ -65,7 +67,12 @@
return False
value = rating.Rating(id, value, user)
self._storage[id][user] = value
- zope.event.notify(ObjectAddedEvent(value))
+ if existing is None:
+ zope.event.notify(interfaces.RatingAddedEvent(
+ id, self.__parent__, user, value))
+ else:
+ zope.event.notify(interfaces.RatingChangedEvent(
+ id, self.__parent__, user, value))
return True
def remove(self, id, user):
@@ -76,7 +83,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))
+ zope.event.notify(
+ interfaces.RatingRemovedEvent(id, self.__parent__, user))
del self._storage[id][user]
if len(self._storage[id]) == 0:
del self._storage[id]
More information about the Checkins
mailing list