Attachment 'Views-1.2.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - Views macro Version 1.2, 18.01.2013
   4     
   5     Syntax:
   6        <<Views>>; <<Views()>>
   7        <<Views(viewers='self'|'other'|'all')>> default is all
   8        
   9     Prerequisites:
  10     
  11         ViewLog class is imported from viewlog.py in 
  12         wiki/data/plugin/logfile. This folder must also contain the 
  13         standard __init__.py for plugin modules.
  14         
  15         <<ViewLog>> marco must run on the page for views to be logged. 
  16 
  17     History:
  18 
  19         Version 1.2 - 18.01.2013: minor fixes.
  20     
  21         Version 1.1 - 31.12.2012: minor fix for <<Views>> syntax.
  22     
  23         Version 1.0 - 18.12.2012: initial version.
  24 
  25     @copyright: 2012-2013 Ian Riley <ian.riley@internode.on.net>
  26     
  27     incorporting code from:
  28     
  29     @copyright: 2006-2008 MoinMoin:ThomasWaldmann,
  30                 2007 MoinMoin:ReimarBauer
  31 
  32     license: GNU GPL, see COPYING for details.
  33 """
  34 
  35 import os, time
  36 from MoinMoin import user, wikiutil
  37 from MoinMoin.Page import Page
  38 
  39 macroname = {'macroname': __name__.split('.')[-1]}
  40 
  41 def execute(macro, args):
  42     request = macro.request
  43     _ = request.getText
  44     
  45     ViewLog = wikiutil.importWikiPlugin(request.cfg, 'logfile', 'viewlog', 'ViewLog')
  46 
  47     args_dict = {'viewers': ''}
  48     if args:
  49         try:
  50             args_parser = wikiutil.ParameterParser('%(viewers)s')
  51             (count, args_dict) = args_parser.parse_parameters(args)
  52         except:
  53             return _('Error: %(macroname)s macro has bad arguments.') % macroname
  54     
  55     viewers = args_dict['viewers']
  56     if not viewers:
  57         viewers = 'all'
  58     if not viewers in ['self', 'other', 'all', ]:
  59         return _('Error: %(macroname)s macro argument not recognised.') % macroname
  60 
  61     user = request.user
  62     if user.valid:
  63         user_name = user.name
  64     else:
  65         user_name = 'anon'
  66             
  67     page = macro.formatter.page
  68     page_name = page.page_name
  69     pagelog = page.getPagePath('view-log', use_underlay=0, isfile=1)
  70     
  71     viewCount = 0
  72 
  73     if os.path.exists(pagelog):
  74         viewlog = ViewLog(request, filename=pagelog, uid_override=None)
  75         for view in viewlog.reverse():
  76             viewer = view.username
  77             if (viewers == 'self' and viewer == user_name) or \
  78                (viewers == 'other' and viewer != user_name) or \
  79                (viewers == 'all'): viewCount += 1
  80     
  81     return u'%d' % viewCount

You are not allowed to attach a file to this page.