[Checkins] SVN: lovely.rating/trunk/src/lovely/rating/ set tzinfo on ratings, do not create new rating if not modified, return booleans on rate and remove indicating changes, added evolve script that sets the timezone to utc on existing objects

Bernd Dorn bernd.dorn at lovelysystems.com
Fri Feb 9 08:10:16 EST 2007


Log message for revision 72482:
  set tzinfo on ratings, do not create new rating if not modified, return booleans on rate and remove indicating changes, added evolve script that sets the timezone to utc on existing objects

Changed:
  U   lovely.rating/trunk/src/lovely/rating/README.txt
  U   lovely.rating/trunk/src/lovely/rating/generations/__init__.py
  A   lovely.rating/trunk/src/lovely/rating/generations/evolve1.py
  U   lovely.rating/trunk/src/lovely/rating/manager.py
  U   lovely.rating/trunk/src/lovely/rating/rating.py

-=-
Modified: lovely.rating/trunk/src/lovely/rating/README.txt
===================================================================
--- lovely.rating/trunk/src/lovely/rating/README.txt	2007-02-09 10:58:48 UTC (rev 72481)
+++ lovely.rating/trunk/src/lovely/rating/README.txt	2007-02-09 13:10:15 UTC (rev 72482)
@@ -74,10 +74,13 @@
   >>> zope.component.provideUtility(
   ...     usability, IRatingDefinition, name='usability')
 
-We are finally ready to rate KDE for usability:
+We are finally ready to rate KDE for usability, note that the rate
+method returns True if a change occured:
 
-  >>> manager.rate('usability', u'Good', u'srichter')
+  >>> manager.rate('usability', u'Okay', u'srichter')
+  True
   >>> manager.rate('usability', u'Okay', u'kartnaller')
+  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
@@ -97,22 +100,38 @@
 you can ask for all ratings made for a rating definition:
 
   >>> sorted([rating.__repr__() for rating in manager.getRatings('usability')])
-  ["<Rating u'Good' by u'srichter'>", "<Rating u'Okay' by u'kartnaller'>"]
+  ["<Rating u'Okay' by u'kartnaller'>", "<Rating u'Okay' by u'srichter'>"]
 
 You can also ask for the rating of a particular user:
 
   >>> manager.getRating('usability', u'srichter')
-  <Rating u'Good' by u'srichter'>
+  <Rating u'Okay' by u'srichter'>
 
 The rating has the following attributes:
 
   >>> manager.getRating('usability', u'srichter').value
-  u'Good'
+  u'Okay'
   >>> manager.getRating('usability', u'srichter').user
   u'srichter'
-  >>> manager.getRating('usability', u'srichter').timestamp
-  datetime.datetime(...)
+  >>> ts = manager.getRating('usability', u'srichter').timestamp
+  >>> ts
+  datetime.datetime(..., tzinfo=<UTC>)
 
+Note that if a user rates an object with the same value again the
+timestamp is not changed.
+
+  >>> manager.rate('usability', u'Okay', u'srichter')
+  False
+  >>> ts == manager.getRating('usability', u'srichter').timestamp
+  True
+
+But if the value changes the timestamp is updated.
+
+  >>> manager.rate('usability', u'Good', u'srichter')
+  True
+  >>> ts == manager.getRating('usability', u'srichter').timestamp
+  False
+
 Note that the rating object is read-only:
 
   >>> manager.getRating('usability', u'srichter').value = u'Awesome'
@@ -123,16 +142,22 @@
 A new rating can be given by rating the application again:
 
   >>> manager.rate('usability', u'Awesome', u'srichter')
+  True
   >>> manager.getRating('usability', u'srichter')
   <Rating u'Awesome' by u'srichter'>
 
-Ratings are removed using the following:
+Ratings are removed using the following. This method also returns a
+boolean indicating if something has changed.:
 
   >>> manager.rate('usability', u'Crap', u'badcarma')
+  True
   >>> manager.getRating('usability', u'badcarma')
   <Rating u'Crap' by u'badcarma'>
 
   >>> manager.remove('usability', 'badcarma')
+  True
+  >>> manager.remove('usability', 'badcarma')
+  False
   >>> manager.getRating('usability', u'badcarma')
 
 Finally, the manager also provides some basic statistical features:

Modified: lovely.rating/trunk/src/lovely/rating/generations/__init__.py
===================================================================
--- lovely.rating/trunk/src/lovely/rating/generations/__init__.py	2007-02-09 10:58:48 UTC (rev 72481)
+++ lovely.rating/trunk/src/lovely/rating/generations/__init__.py	2007-02-09 13:10:15 UTC (rev 72482)
@@ -14,6 +14,6 @@
 
 
 schemaManager = SchemaManager(
-    minimum_generation=0,
-    generation=0,
+    minimum_generation=1,
+    generation=1,
     package_name=pkg)

Added: lovely.rating/trunk/src/lovely/rating/generations/evolve1.py
===================================================================
--- lovely.rating/trunk/src/lovely/rating/generations/evolve1.py	2007-02-09 10:58:48 UTC (rev 72481)
+++ lovely.rating/trunk/src/lovely/rating/generations/evolve1.py	2007-02-09 13:10:15 UTC (rev 72482)
@@ -0,0 +1,15 @@
+from zope.app.zopeappgenerations import getRootFolder
+from zope.app.generations.utility import findObjectsProviding
+from lovely.rating.interfaces import IRatable, IRatingsManager
+from pytz import UTC
+
+def evolve(context):
+    """Add tzinfo to timestamp of ratings"""
+    for ratable in findObjectsProviding(getRootFolder(context), IRatable):
+        manager = IRatingsManager(ratable)
+        for d in manager._storage.values():
+            for rating in d.values():
+                if rating._timestamp.tzinfo is None:
+                    rating._timestamp = rating._timestamp.replace(
+                        tzinfo=UTC)
+                    


Property changes on: lovely.rating/trunk/src/lovely/rating/generations/evolve1.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Modified: lovely.rating/trunk/src/lovely/rating/manager.py
===================================================================
--- lovely.rating/trunk/src/lovely/rating/manager.py	2007-02-09 10:58:48 UTC (rev 72481)
+++ lovely.rating/trunk/src/lovely/rating/manager.py	2007-02-09 13:10:15 UTC (rev 72482)
@@ -50,9 +50,14 @@
         if id not in self._storage:
             self._storage[id] = OOBTree.OOBTree()
             contained.contained(self._storage[id], self._storage, id)
-
+        existing = self._storage[id].get(user)
+        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)
-        contained.contained(self._storage[id][user], self._storage[id], user)
+        contained.contained(self._storage[id][user],
+                            self._storage[id], user)
+        return True
 
     def remove(self, id, user):
         """See interfaces.IRatingsManager"""
@@ -60,10 +65,11 @@
         defn = self._getDefinition(id)
 
         if id not in self._storage or user not in self._storage[id]:
-            return
+            return False
         del self._storage[id][user]
         if len(self._storage[id]) == 0:
             del self._storage[id]
+        return True
 
     def getRatings(self, id):
         """See interfaces.IRatingsManager"""

Modified: lovely.rating/trunk/src/lovely/rating/rating.py
===================================================================
--- lovely.rating/trunk/src/lovely/rating/rating.py	2007-02-09 10:58:48 UTC (rev 72481)
+++ lovely.rating/trunk/src/lovely/rating/rating.py	2007-02-09 13:10:15 UTC (rev 72482)
@@ -22,8 +22,8 @@
 from zope.app.container import contained
 
 from lovely.rating import IRating
+from pytz import UTC
 
-
 class Rating(contained.Contained, persistent.Persistent):
     zope.interface.implements(IRating)
 
@@ -36,7 +36,7 @@
         self._id = id
         self._value = value
         self._user = user
-        self._timestamp = datetime.datetime.now()
+        self._timestamp = datetime.datetime.now(UTC)
 
     def __repr__(self):
         return '<%s %r by %r>' %(self.__class__.__name__, self.value, self.user)



More information about the Checkins mailing list