changeset 3422:b9c90178b93a

macro.Hits: refactored
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Sun, 23 Mar 2008 13:18:03 +0100
parents d71aa6092994
children 6fa56b0f0de4 aa3890b17212
files MoinMoin/macro/Hits.py MoinMoin/macro/_tests/test_Hits.py
diffstat 2 files changed, 14 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/macro/Hits.py	Sun Mar 23 00:54:09 2008 +0100
+++ b/MoinMoin/macro/Hits.py	Sun Mar 23 13:18:03 2008 +0100
@@ -11,7 +11,7 @@
              Default is 0/False/no.
         filter: if set to SAVEPAGE then the saved pages are counted. Default is VIEWPAGE.
 
-   @copyright: 2004-2007 MoinMoin:ReimarBauer,
+   @copyright: 2004-2008 MoinMoin:ReimarBauer,
                2005 BenjaminVrolijk
    @license: GNU GPL, see COPYING for details.
 """
@@ -20,15 +20,12 @@
 
 from MoinMoin.logfile import eventlog
 
-
-def macro_Hits(macro, all=False, filter=(u'VIEWPAGE', u'SAVEPAGE')):
-    this_page = macro.formatter.page.page_name
+def macro_Hits(macro, all=False, event_type=(u'VIEWPAGE', u'SAVEPAGE')):
+    pagename = macro.formatter.page.page_name
     event_log = eventlog.EventLog(macro.request)
-    event_log.set_filter([str(filter)])
-    count = 0
-    for event in event_log:
-        pagename = event[2].get('pagename')
-        if all or pagename == this_page:
-            count += 1
+    if not all:
+        test = filter(lambda line: line[1] in event_type and line[2]['pagename'] == pagename, event_log)
+    else:
+        test = filter(lambda line: line[1] in event_type, event_log)
 
-    return u'%d' % count
+    return u'%d' % len(test)
--- a/MoinMoin/macro/_tests/test_Hits.py	Sun Mar 23 00:54:09 2008 +0100
+++ b/MoinMoin/macro/_tests/test_Hits.py	Sun Mar 23 13:18:03 2008 +0100
@@ -74,7 +74,7 @@
         assert result == expected
 
     def testHitsForAll(self):
-        """ macro Hits test: 'all=1' for Hits (all pages are counted for VIEWPAGE) """
+        """ macro Hits test: 'all=True' for Hits (all pages are counted for VIEWPAGE) """
         self.shouldDeleteTestPage = False
         self._createTestPage('This is an example to test a macro with parameters')
 
@@ -82,25 +82,25 @@
         eventlog.EventLog(self.request).add(self.request, 'VIEWPAGE', {'pagename': self.pagename})
         eventlog.EventLog(self.request).add(self.request, 'VIEWPAGE', {'pagename': self.pagename})
 
-        result = self._test_macro(u'Hits', u'all=1')
+        result = self._test_macro(u'Hits', u'all=True')
         expected = "6"
         assert result == expected
 
     def testHitsForFilter(self):
-        """ macro Hits test: 'all=1, filter=SAVEPAGE' for Hits (SAVEPAGE counted for current page)"""
+        """ macro Hits test: 'all=True, event_type=SAVEPAGE' for Hits (SAVEPAGE counted for current page)"""
         self.shouldDeleteTestPage = False
 
         # simulate a log entry SAVEPAGE for WikiSandBox to destinguish current page
         eventlog.EventLog(self.request).add(self.request, 'SAVEPAGE', {'pagename': 'WikiSandBox'})
-        result = self._test_macro(u'Hits', u'filter=SAVEPAGE')
+        result = self._test_macro(u'Hits', u'event_type=SAVEPAGE')
         expected = "2"
         assert result == expected
 
     def testHitsForAllAndFilter(self):
-        """ macro test: 'all=1, filter=SAVEPAGE' for Hits (all pages are counted for SAVEPAGE)"""
+        """ macro test: 'all=True, event_type=SAVEPAGE' for Hits (all pages are counted for SAVEPAGE)"""
         self.shouldDeleteTestPage = True
 
-        result = self._test_macro(u'Hits', u'all=1, filter=SAVEPAGE')
+        result = self._test_macro(u'Hits', u'all=True, event_type=SAVEPAGE')
         expected = "3"
         assert result == expected