1.1 --- a/macros/VotingStars.py Sun Mar 02 03:08:36 2008 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,144 +0,0 @@
1.4 -#format python
1.5 -# -*- coding: iso-8859-1 -*-
1.6 -'''
1.7 - MoinMoin - VotingStars Macro
1.8 - Author: Travis Bailey
1.9 - Date: February 2006
1.10 -
1.11 - Purpose:
1.12 - This macro allows ratings of items using stars (based on Vote macro)
1.13 - Usage:
1.14 - [[VotingStars(UniqueName)]]
1.15 - e.g. 1. NiceShop [[VotingStars(shop1)]]
1.16 - 1. EvenNicerShop [[VotingStars(shop2)]]
1.17 - Tested:
1.18 - Firefox 1.5 MoinMoin 1.5.1 Python 2.3
1.19 -
1.20 - I basically just slapped this together from the Vote1_3.py script and the
1.21 - RatingStars.py script. I wanted to get the benefits of both. I wanted a
1.22 - Utility to only allow Registered users to submit, allow them to change
1.23 - their vote, and to average the results.
1.24 -
1.25 - So basically it uses the Vote1_3.py code to track one vote per user.
1.26 - It averages the total stars given and then displays the number of average
1.27 - number of rated stars.
1.28 -
1.29 - You can have multiple VotingStars on one page (so long as the UniqueNames are unique)
1.30 - Changing the UniqueName creates a new vote.
1.31 - Make sure not to conflict with any Vote names you are using
1.32 -
1.33 - VotingStars data is stored in data_dir/pages/pagename/votes
1.34 - (i.e. alongside the attachments directory for a page).
1.35 -'''
1.36 -import os, pickle
1.37 -from MoinMoin import wikiutil
1.38 -from MoinMoin.util import filesys
1.39 -
1.40 -def makeFilename(thisPage, voteName):
1.41 - votesDir = thisPage.getPagePath("votes")
1.42 - if not os.path.exists(votesDir):
1.43 - filesys.makeDirs(votesDir)
1.44 - voteName = wikiutil.quoteWikinameFS(voteName) + '.pik'
1.45 - return os.path.join(votesDir, voteName)
1.46 -
1.47 -def loadVotes(thisPage, voteName):
1.48 - votesFile = makeFilename(thisPage, voteName)
1.49 - try:
1.50 - f = open(votesFile, 'r')
1.51 - return pickle.load(f)
1.52 - except: # XXX
1.53 - return {}
1.54 -
1.55 -def saveVotes(thisPage, voteName, votes):
1.56 - votesFile = makeFilename(thisPage, voteName)
1.57 - f = open(votesFile, 'w')
1.58 - pickle.dump(votes, f)
1.59 -
1.60 -def countVotes(votes):
1.61 - results = {}
1.62 - for v in votes.values():
1.63 - results.setdefault(v, 0)
1.64 - results[v] += 1
1.65 - return results
1.66 -
1.67 -def execute(macro, args):
1.68 - fmt = macro.formatter
1.69 -
1.70 - args = args.split(",")
1.71 - args = map(unicode.strip, args)
1.72 -
1.73 - if len(args) < 1:
1.74 - return fmt.rawHTML('<pre>[[Vote: Insufficient macro arguments]]</pre>')
1.75 -
1.76 - pageName = args[0]
1.77 - voteName = pageName
1.78 - candidates = ['1','2','3','4','5']
1.79 - src_litstar = '/wiki/modern/img/star_on.png'
1.80 - src_unlitstar = '/wiki/modern/img/star_off.png'
1.81 -
1.82 - form = macro.form
1.83 - request = macro.request
1.84 - if request.user.valid:
1.85 - voter = request.user.name
1.86 - else:
1.87 - voter = ''
1.88 - thisPage = fmt.page
1.89 - votes = loadVotes(thisPage, voteName)
1.90 -
1.91 - # votes are stored in a dictionary as {user: candidate} to avoid duplicate votes
1.92 - # (a voter could switch their vote but this UI doesn't expose that facility)
1.93 - if form.has_key('vote') and form.has_key('voteName') and voter:
1.94 - if form['voteName'][0] == voteName:
1.95 - votes[voter] = form['vote'][0]
1.96 - try:
1.97 - saveVotes(thisPage, voteName, votes)
1.98 - except: # XXX
1.99 - return fmt.rawHTML('<pre>[[Vote: failed to store vote]]</pre>')
1.100 -
1.101 - # generate dictionary {candidate: numvotes}
1.102 - results = countVotes(votes)
1.103 - onestar = results.setdefault(candidates[0],0)
1.104 - twostar = results.setdefault(candidates[1],0)
1.105 - threestar = results.setdefault(candidates[2],0)
1.106 - fourstar = results.setdefault(candidates[3],0)
1.107 - fivestar = results.setdefault(candidates[4],0)
1.108 - counts = (onestar+twostar+threestar+fourstar+fivestar)
1.109 -
1.110 - if counts == 0:
1.111 - average = 0
1.112 - else:
1.113 - average = (onestar*1+twostar*2+threestar*3+fourstar*4+fivestar*5)/counts
1.114 -
1.115 - hasVoted = voter in votes
1.116 -
1.117 - # spit out votes table (as part of a form if the user hasn't voted yet)
1.118 - html = []
1.119 -
1.120 - if voter:
1.121 - html.append('''
1.122 - <form method="get" name="%(formname)s" action="%(url)s#%(formname)s">\n
1.123 - <input type="hidden" name="voteName" value="%(voteName)s">\n
1.124 - <input type="hidden" name="vote" value="0">
1.125 - ''' % {
1.126 - 'formname': voteName,
1.127 - 'url': thisPage.url(request),
1.128 - 'voteName': voteName,
1.129 - }
1.130 - )
1.131 - btnstar = '<input type="image" src="%(isrc)s" OnClick="document.%(form)s.vote.value=%(value)s">\n'
1.132 - for i in range(1,6):
1.133 - if i <= average:
1.134 - html.append(btnstar % {'isrc':src_litstar, 'form':voteName, 'value': i})
1.135 - if i > average:
1.136 - html.append(btnstar % {'isrc':src_unlitstar, 'form':voteName, 'value': i})
1.137 - if average == 0:
1.138 - html.append(' No Rating')
1.139 - #else:
1.140 - # html.append(' %(avg)s ' % { 'avg': average })
1.141 - html.append('</form>')
1.142 - else:
1.143 - html.append('Please log in to vote.')
1.144 -
1.145 - return fmt.rawHTML(''.join(html))
1.146 -
1.147 -# vim: tabstop=4 expandtab shiftwidth=4