Attachment 'UserList2.py'

Download

   1 #format python
   2 """
   3 MoinMoin macro to create a list of current users
   4 UserList.py
   5 ark  06/13/04  0.1
   6 
   7     ***********
   8     originally by Antti Kuntsi <antti@kuntsi.com>
   9     As far as the author is concerned, this code is released to the public
  10     domain, but some restrictions in the MoinMoin COPYING file may apply. Ask a
  11     lawyer.
  12     ***********
  13     This code has not been tested exhaustively. Use at your own risk.
  14     The multiple column support has been tested a little
  15     ***********
  16 
  17     This macro displays a list of currently known users.
  18 
  19     Usage:
  20         [[UserList2(....)]]
  21         parameters:
  22           columncount=INT
  23           width=PERCENTUAL WIDTH OF BOX
  24           grouplabel - whether render linkbar with initials
  25            
  26 
  27     Changes to suit to API changes of 1.3.x releases done by CyA, (c) 2005-02-28
  28     Multicolumn layout redesigned by CyA
  29 
  30 """
  31 
  32 
  33 import string, time, os, getopt
  34 from math import ceil
  35 from MoinMoin import wikiutil, multiconfig, user
  36   
  37 def create_realname(username):
  38     realname=""
  39     
  40     start=1
  41     for i in username:
  42         if start == 0 and i.isupper():
  43 	    realname+=" %s" % i
  44 	else:
  45 	    realname+=i
  46 	if start == 1:
  47 	    start = 0
  48     return realname
  49 
  50 def execute(macro, args):
  51     '''handle the UserList Macro. return the generated display HTML
  52     ark  06/13/04'''
  53     result = []
  54     columnlist=[]
  55     
  56     # get user from Wiki cache
  57     users = user.getUserList(macro.request)
  58     if not users:
  59       return
  60     else:
  61       #get identifications of users
  62       userlist=[]
  63       for uid in users:
  64         name = user.User(macro.request, id=uid).name
  65         userlist.append(name)      
  66       # create a dictionary with initials as keys and lists of usernames as values
  67       initials={}
  68       for u in userlist:
  69         initial=u[0]
  70         if not initial in initials.keys():
  71           initials[initial]=[]
  72         initials[initial].append(u)
  73 
  74     # define standard values
  75     rowsize=1
  76     grouplabel=0
  77     tablewidth=100
  78     
  79     # parse the arguments    
  80     args = _parse_arguments(args, ',')
  81 
  82     # use passed in options 
  83     if args:
  84       for o,a in args:
  85         # how many columns to use in output table
  86         if o in ("--columncount"):
  87           rowsize=int(a)
  88         
  89         # label each group of users which is started by new initial by special row  
  90         if o in ("--grouplabel"):
  91           grouplabel=1
  92     
  93         # use defined table width
  94         if o in ("--width"):
  95           # is it not relative?
  96           if a.find("%") < 0:
  97             if a<0:
  98               a=100
  99             if a>100:
 100               a=100
 101             a=a+"%"
 102           tablewidth=a
 103             
 104     result.append(macro.formatter.rawHTML('<table style="width: %s;">') % tablewidth)
 105     ik=initials.keys()
 106     ik.sort()
 107     for initial in ik:
 108       users=initials[initial]
 109       count=len(users)
 110       
 111       rows=ceil(count/float(rowsize)) # how much rows will take group of users with the same initials
 112       row=0
 113       col=0
 114       userindex=0
 115       
 116       if grouplabel == 1:
 117         result.append(macro.formatter.rawHTML('<tr><td width="100%%" colspan="%s"><a id="%s"><span style="text-transform: uppercase; font-weight: bold;">%s</span></a></td></tr>' %(rowsize,initial,initial)))
 118       while row < rows:
 119         result.append(macro.formatter.table_row(1))
 120         while col < rowsize:
 121           result.append(macro.formatter.table_cell(1))
 122           if userindex < count:
 123             name=users[userindex]
 124             pagename = macro.formatter.pagelink(1, name)
 125             realname = create_realname(name)
 126             content=pagename+realname+"</a>"
 127           else:
 128             content="&nbsp;"
 129           result.append(macro.formatter.rawHTML("%s") % content) 
 130           result.append(macro.formatter.table_cell(0))
 131           userindex+=1
 132           col+=1
 133         col=0
 134         result.append(macro.formatter.table_row(0))
 135         row+=1
 136     
 137     result.append(macro.formatter.table(0))
 138 
 139     if grouplabel==1:  
 140       anchorlinks=""
 141       for a in ik:
 142         anchor=macro.formatter.anchorlink(1,a)+a+"</a> "
 143         anchorlinks+=anchor
 144       anchorlinks=anchorlinks[:-1]
 145       result.insert(0, anchorlinks)
 146        
 147     result.append(macro.formatter.rawHTML('<br clear="both">'))
 148     return "\n".join(result)
 149 
 150 
 151 def _parse_arguments(argstring, delimchar=',', quotechar='"',macro=None,result=None):
 152     '''
 153         transform argument string into form suitable for getopt and parse it. 
 154         I'm lazy to finding out original ways when the standard one does exist. cya.
 155     '''
 156     
 157     if argstring:
 158       arguments=argstring.split(delimchar)
 159       args=[]
 160       for a in arguments:
 161         a='--'+a
 162         args.append(a)
 163       
 164       try:  
 165         opts, oargs = getopt.getopt(args, "", ["columncount=","width=","grouplabel"])
 166       except getopt.GetoptError:
 167         opts=None
 168     else:
 169       opts=None
 170           
 171     return opts

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2005-02-28 11:53:13, 4.8 KB) [[attachment:UserList2.py]]
  • [get | view] (2008-08-19 12:31:43, 4.9 KB) [[attachment:UserList2_1.7.py]]
 All files | Selected Files: delete move to page copy to page

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