[CMF-checkins] SVN: CMF/trunk/C - CMFCalendar.CalendarTool: Added a new method getNextEvent to grab the

Jens Vagelpohl jens at dataflake.org
Mon Jun 11 10:54:28 EDT 2007


Log message for revision 76612:
  - CMFCalendar.CalendarTool: Added a new method getNextEvent to grab the
    next event relative to a given point in time.
    (http://www.zope.org/Collectors/CMF/462)
  

Changed:
  U   CMF/trunk/CHANGES.txt
  U   CMF/trunk/CMFCalendar/CalendarTool.py
  U   CMF/trunk/CMFCalendar/tests/test_Calendar.py

-=-
Modified: CMF/trunk/CHANGES.txt
===================================================================
--- CMF/trunk/CHANGES.txt	2007-06-11 14:34:34 UTC (rev 76611)
+++ CMF/trunk/CHANGES.txt	2007-06-11 14:54:27 UTC (rev 76612)
@@ -2,6 +2,10 @@
 
   New Features
 
+    - CMFCalendar.CalendarTool: Added a new method getNextEvent to grab the
+      next event relative to a given point in time.
+      (http://www.zope.org/Collectors/CMF/462)
+
     - CMFCore.exportimport.skins: Added the ability to remove whole skin
       selections using a GS profile.
       (http://www.zope.org/Collectors/CMF/479)

Modified: CMF/trunk/CMFCalendar/CalendarTool.py
===================================================================
--- CMF/trunk/CMFCalendar/CalendarTool.py	2007-06-11 14:34:34 UTC (rev 76611)
+++ CMF/trunk/CMFCalendar/CalendarTool.py	2007-06-11 14:54:27 UTC (rev 76612)
@@ -30,6 +30,23 @@
 from interfaces import ICalendarTool
 from permissions import ManagePortal
 
+def sort_by_date(x, y):
+    """ Utility function for sorting by start times, falling back on end times
+    """
+    z = cmp(x.start, y.start)
+    if not z:
+        return cmp(x.end, y.end)
+    return z
+    
+def unique_results(results):
+    """ Utility function to create a sequence of unique calendar results
+    """
+    rids = {}
+    for result in results:
+        rids[result.getRID()] = result
+    return rids.values()
+
+
 class CalendarTool (UniqueObject, SimpleItem):
     """ A tool for encapsulating how calendars work and are displayed """
 
@@ -296,22 +313,10 @@
                          end={'query': last_date, 'range': 'min'} )
 
         # Unique the results
-        results = []
-        rids = []
-        for item in query:
-            rid = item.getRID()
-            if not rid in rids:
-                results.append(item)
-                rids.append(rid)
+        results = unique_results(query)
 
-        def sort_function(x,y):
-            z = cmp(x.start,y.start)
-            if not z:
-                return cmp(x.end,y.end)
-            return z
-
         # Sort by start date
-        results.sort(sort_function)
+        results.sort(sort_by_date)
 
         return results
 
@@ -356,6 +361,27 @@
         end = DateTime('%d/%02d/%02d 23:59:59' % (year, month, day))
 
         return (begin, end)
+        
+    security.declarePublic('getNextEvent')
+    def getNextEvent(self, start_date=None):
+        """ Get the next event that starts after start_date
+        
+        start_date is expected to be a DateTime instance
+        """
+        if start_date is None:
+            start_date = DateTime()
 
+        query = self.portal_catalog(
+                    portal_type=self.getCalendarTypes(),
+                    review_state=self.getCalendarStates(),
+                    start={'query': start_date, 'range': 'min'},
+                    sort_on='start')
+
+        results = unique_results(query)
+        if results:
+            results.sort(sort_by_date)
+            return results[0]
+
+
 InitializeClass(CalendarTool)
 registerToolInterface('portal_calendar', ICalendarTool)

Modified: CMF/trunk/CMFCalendar/tests/test_Calendar.py
===================================================================
--- CMF/trunk/CMFCalendar/tests/test_Calendar.py	2007-06-11 14:34:34 UTC (rev 76611)
+++ CMF/trunk/CMFCalendar/tests/test_Calendar.py	2007-06-11 14:54:27 UTC (rev 76612)
@@ -715,7 +715,39 @@
         events = caltool.catalog_getevents(2002, 5)
         self.assertEqual([events[e] for e in range(1, 8)], data)
 
+    def test_getNextEvent(self):
+        cal = self.app.site.portal_calendar
+        wf_tool = self.app.site.portal_workflow
+        start_one = DateTime('2002/05/01 19:30:00 GMT+1')
+        stop_one = DateTime('2002/05/01 22:00:00 GMT+1')
+        start_two = DateTime('2002/06/01 19:30:00 GMT+1')
+        stop_two = DateTime('2002/06/01 22:00:00 GMT+1')
 
+        test_day = DateTime('2002/07/01')
+
+        self.app.site.invokeFactory( 'Event'
+                                   , id='party1'
+                                   , start_date=start_one
+                                   , end_date=stop_one
+                                   )
+
+
+        self.app.site.invokeFactory( 'Event'
+                                   , id='party2'
+                                   , start_date=start_two
+                                   , end_date=start_two
+                                   )
+
+        wf_tool.doActionFor(self.app.site.party1, 'publish')
+        wf_tool.doActionFor(self.app.site.party2, 'publish')
+
+        # Check to see if we get only one event when back
+        self.assertEqual(cal.getNextEvent(stop_one).start, start_two)
+
+        # Check to see that we don't have events after July 2002
+        self.failIf(cal.getNextEvent(test_day))
+
+
 def test_suite():
     return unittest.TestSuite((
         unittest.makeSuite(CalendarTests),



More information about the CMF-checkins mailing list