Attachment 'RatingStars-1.5.py'

Download

   1 #format python
   2 # -*- coding: iso-8859-1 -*-
   3 '''
   4     MoinMoin - RatingStars Macro
   5     Author: Nick King
   6     Date: September 2005, March 2006
   7   
   8     Purpose:
   9         This macro allows ratings of items using stars (based on Vote macro)
  10     Usage:
  11         [[RatingStars(UniqueName)]]
  12         e.g. 1. NiceShop [[RatingStars(shop1)]]
  13              1. EvenNicerShop [[RatingStars(shop2)]]
  14     Tested:
  15         Firefox 1.5 MoinMoin 1.5 Python 2.4             
  16 '''
  17 
  18 import os, StringIO
  19 from MoinMoin.parser import wiki
  20 from MoinMoin.Page import Page
  21 
  22 def countstar(fname):
  23     #saved in PageName/attachments and can be viewed as normal attachments
  24     try:
  25         f = open(fname, 'r')
  26         result = int(f.read())
  27         f.close()
  28     except:
  29         result = 0    
  30     return result
  31 
  32 def savestar(fname, starnum):
  33     try:
  34         f = open(fname, 'w')
  35         f.write(str(starnum))
  36         f.close()
  37         return True
  38     except:
  39         return False
  40 
  41 def execute(macro, args):
  42     thisPage = macro.formatter.page
  43     thisPageName = thisPage.page_name
  44     thisUrl = thisPage.url(macro.request)
  45     thisForm = macro.form
  46     fname = os.path.join(thisPage.getPagePath("attachments"), 'ratingstars-' + args + ".txt") 
  47 
  48     src_litstar = '/wiki/modern/img/star_on.png' 
  49     src_unlitstar = '/wiki/modern/img/star_off.png'
  50 
  51     formname = args + 'form'
  52     starval = args + 'val'
  53     result=''
  54 
  55     if thisForm.has_key(starval):
  56         starnum=int(thisForm[starval][0])
  57         if not savestar(fname,starnum):
  58             result += "Error saving file!"
  59     else:
  60         starnum=countstar(fname)
  61  
  62     result += '<a name="%s">' % args
  63     result += '<table width="200"><tr><form method="get" name=%(formname)s action="%(url)s#%(anc)s">' % {
  64         'url': thisUrl, 'anc': args, 'formname':formname}
  65     result += '<input type="hidden" name=%s value=1>' % starval
  66     btnstar = '<input type="image" src=%(isrc)s OnClick="document.%(form)s.%(name)s.value=%(value)s">'  
  67     for i in range(1,6):
  68         if i <= starnum:
  69             result += btnstar % {'isrc':src_litstar, 'form':formname, 'name':starval, 'value': i}
  70         if i> starnum:
  71             result += btnstar % {'isrc':src_unlitstar, 'form':formname, 'name':starval, 'value': i}
  72     if starnum == 0:
  73         result += ' not yet rated'
  74     result += '</form></tr></table></a>'
  75     return result

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] (2006-03-09 01:06:05, 2.3 KB) [[attachment:RatingStars-1.5.py]]
  • [get | view] (2005-09-28 03:14:39, 2.6 KB) [[attachment:RatingStars.py]]
 All files | Selected Files: delete move to page copy to page

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