Attachment 'RatingStars.py'

Download

   1 #format python
   2 # -*- coding: iso-8859-1 -*-
   3 '''
   4     MoinMoin - RatingStars Macro
   5     Author: Nick King
   6     Date: September 2005
   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.5b1 MoinMoin 1.35 Python 2.4             
  16 '''
  17 
  18 import os, StringIO, re
  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 getimgsrc(macro,smiley):
  42     o = StringIO.StringIO()
  43     macro.request.redirect(o)
  44     parser =  wiki.Parser(smiley, macro.request)
  45     parser.format (macro.formatter)
  46     f_o = o.getvalue()
  47     macro.request.redirect()
  48     del o
  49     result = re.search('"[\S]*"', f_o).group()
  50     return result
  51 
  52 def execute(macro, args):
  53     thisPage = macro.formatter.page
  54     thisPageName = thisPage.page_name
  55     thisUrl = thisPage.url(macro.request)
  56     thisForm = macro.form
  57     fname = os.path.join(thisPage.getPagePath("attachments"), 'ratingstars-' + args + ".txt") 
  58     src_litstar = getimgsrc(macro, '{*}')
  59     src_unlitstar = getimgsrc(macro, '{o}')
  60     formname = args + 'form'
  61     starval = args + 'val'
  62     result=''
  63 
  64     if thisForm.has_key(starval):
  65         starnum=int(thisForm[starval][0])
  66         if not savestar(fname,starnum):
  67             result += "Error saving file!"
  68     else:
  69         starnum=countstar(fname)
  70  
  71     result += '<a name="%s">' % args
  72     result += '<table width="200"><tr><form method="get" name=%(formname)s action="%(url)s#%(anc)s">' % {
  73         'url': thisUrl, 'anc': args, 'formname':formname}
  74     result += '<input type="hidden" name=%s value=1>' % starval
  75     btnstar = '<input type="image" src=%(isrc)s OnClick="document.%(form)s.%(name)s.value=%(value)s">'  
  76     for i in range(1,6):
  77         if i <= starnum:
  78             result += btnstar % {'isrc':src_litstar, 'form':formname, 'name':starval, 'value': i}
  79         if i> starnum:
  80             result += btnstar % {'isrc':src_unlitstar, 'form':formname, 'name':starval, 'value': i}
  81     if starnum == 0:
  82         result += ' not yet rated'
  83     result += '</form></tr></table></a>'
  84     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.