comparison MoinMoin/macro/Hits.py @ 2453:adf5ec4874ed

fix ParameterParser, rewrite ParameterParser tests for py.test (and add more tests), refactor Hits macro
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 21 Jul 2007 21:30:47 +0200
parents 01f05e74aa9c
children fe71235f1f7c
comparison
equal deleted inserted replaced
2452:839b360413a9 2453:adf5ec4874ed
8 [[Hits([all=(0,1)],[filter=(VIEWPAGE,SAVEPAGE)]] 8 [[Hits([all=(0,1)],[filter=(VIEWPAGE,SAVEPAGE)]]
9 9
10 all: if set to 1 then cummulative hits over all wiki pages is returned. Default is 0 10 all: if set to 1 then cummulative hits over all wiki pages is returned. Default is 0
11 filter: if set to SAVEPAGE then the saved pages are counted. Default is VIEWPAGE. 11 filter: if set to SAVEPAGE then the saved pages are counted. Default is VIEWPAGE.
12 12
13 @copyright: 2004-2007 MoinMoin:ReimarBauer 13 @copyright: 2004-2007 MoinMoin:ReimarBauer,
14 2005 BenjaminVrolijk 14 2005 BenjaminVrolijk
15 @license: GNU GPL, see COPYING for details. 15 @license: GNU GPL, see COPYING for details.
16 """ 16 """
17
17 Dependencies = ['time'] # do not cache 18 Dependencies = ['time'] # do not cache
18 19
19 from MoinMoin import wikiutil 20 from MoinMoin import wikiutil
20 from MoinMoin.logfile import eventlog 21 from MoinMoin.logfile import eventlog
21 22
22 class Hits: 23 class Hits:
23 def __init__(self, macro, args): 24 def __init__(self, macro, args):
24 self.macro = macro
25 self.request = macro.request 25 self.request = macro.request
26 self.formatter = macro.formatter 26 self.formatter = macro.formatter
27 self.this_page = macro.formatter.page.page_name
27 argParser = wikiutil.ParameterParser("%(all)s%(filter)s") 28 argParser = wikiutil.ParameterParser("%(all)s%(filter)s")
28 try: 29 try:
29 self.arg_list, self.arg_dict = argParser.parse_parameters(args) 30 self.arg_list, self.arg_dict = argParser.parse_parameters(args)
30 except ValueError: 31 self.error = None
31 # TODO Set defaults until raise in ParameterParser.parse_parameters is changed 32 except ValueError, err:
32 self.arg_dict = {} 33 self.error = str(err)
33 self.arg_dict["filter"] = None
34 self.arg_dict["all"] = 0
35
36 self.count = 0
37 34
38 def renderInText(self): 35 def renderInText(self):
39 return self.formatter.text("%s" % (self.getHits())) 36 if self.error:
37 text = "Hits macro: %s" % self.error
38 else:
39 text = "%d" % self.getHits()
40 return self.formatter.text(text)
40 41
41 def getHits(self): 42 def getHits(self):
42 formatter = self.macro.formatter 43 event_filter = self.arg_dict["filter"]
43 kw = self.arg_dict 44 if not event_filter:
44 if not kw["filter"]: kw["filter"] = "VIEWPAGE" 45 event_filter = "VIEWPAGE"
46 count_all_pages = self.arg_dict["all"]
45 47
46 event_log = eventlog.EventLog(self.request) 48 event_log = eventlog.EventLog(self.request)
47 event_log.set_filter([kw["filter"]]) 49 event_log.set_filter([event_filter])
50 count = 0
48 for event in event_log.reverse(): 51 for event in event_log.reverse():
49 pagename = event[2].get('pagename', None) 52 pagename = event[2].get('pagename')
50 if not kw["all"]: 53 if count_all_pages or pagename == self.this_page:
51 if pagename == formatter.page.page_name: 54 count += 1
52 self.count += 1
53 else:
54 self.count += 1
55 55
56 return self.count 56 return count
57 57
58 def execute(macro, args): 58 def execute(macro, args):
59 """ Temporary glue code to use with moin current macro system """ 59 """ Temporary glue code to use with moin current macro system """
60 return Hits(macro, args).renderInText() 60 return Hits(macro, args).renderInText()
61 61