1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/macros/LoginHint.py Sat Mar 01 21:45:56 2008 +0100
1.3 @@ -0,0 +1,22 @@
1.4 +"""
1.5 + MoinMoin - LoginHint
1.6 +
1.7 + Usage:
1.8 + [[LoginHint(Please login using the link at the top border!)]]
1.9 +
1.10 + @copyright: 2006 Thomas Waldmann
1.11 + @license: GNU GPL, see COPYING for details
1.12 +"""
1.13 +hint_style = "background-color: orange; text-align: center;"
1.14 +
1.15 +def execute(macro, args):
1.16 + request = macro.request
1.17 + if request.user.valid:
1.18 + hint = ''
1.19 + else:
1.20 + f = request.formatter
1.21 + hint = (f.div(1, id="LoginHint", style=hint_style) +
1.22 + f.text(args) +
1.23 + f.div(0))
1.24 + return hint
1.25 +
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/macros/VotingStars.py Sat Mar 01 21:45:56 2008 +0100
2.3 @@ -0,0 +1,144 @@
2.4 +#format python
2.5 +# -*- coding: iso-8859-1 -*-
2.6 +'''
2.7 + MoinMoin - VotingStars Macro
2.8 + Author: Travis Bailey
2.9 + Date: February 2006
2.10 +
2.11 + Purpose:
2.12 + This macro allows ratings of items using stars (based on Vote macro)
2.13 + Usage:
2.14 + [[VotingStars(UniqueName)]]
2.15 + e.g. 1. NiceShop [[VotingStars(shop1)]]
2.16 + 1. EvenNicerShop [[VotingStars(shop2)]]
2.17 + Tested:
2.18 + Firefox 1.5 MoinMoin 1.5.1 Python 2.3
2.19 +
2.20 + I basically just slapped this together from the Vote1_3.py script and the
2.21 + RatingStars.py script. I wanted to get the benefits of both. I wanted a
2.22 + Utility to only allow Registered users to submit, allow them to change
2.23 + their vote, and to average the results.
2.24 +
2.25 + So basically it uses the Vote1_3.py code to track one vote per user.
2.26 + It averages the total stars given and then displays the number of average
2.27 + number of rated stars.
2.28 +
2.29 + You can have multiple VotingStars on one page (so long as the UniqueNames are unique)
2.30 + Changing the UniqueName creates a new vote.
2.31 + Make sure not to conflict with any Vote names you are using
2.32 +
2.33 + VotingStars data is stored in data_dir/pages/pagename/votes
2.34 + (i.e. alongside the attachments directory for a page).
2.35 +
2.36 + You can customise the appearance by defining the following CSS styles:
2.37 + table.votestars
2.38 +
2.39 +'''
2.40 +import os, pickle
2.41 +from MoinMoin import wikiutil
2.42 +from MoinMoin.util import filesys
2.43 +
2.44 +def makeFilename(thisPage, voteName):
2.45 + votesDir = thisPage.getPagePath("votes")
2.46 + if not os.path.exists(votesDir):
2.47 + filesys.makeDirs(votesDir)
2.48 + voteName = wikiutil.quoteWikinameFS(voteName) + '.pik'
2.49 + return os.path.join(votesDir, voteName)
2.50 +
2.51 +def loadVotes(thisPage, voteName):
2.52 + votesFile = makeFilename(thisPage, voteName)
2.53 + try:
2.54 + f = open(votesFile, 'r')
2.55 + return pickle.load(f)
2.56 + except: # XXX
2.57 + return {}
2.58 +
2.59 +def saveVotes(thisPage, voteName, votes):
2.60 + votesFile = makeFilename(thisPage, voteName)
2.61 + f = open(votesFile, 'w')
2.62 + pickle.dump(votes, f)
2.63 +
2.64 +def countVotes(votes):
2.65 + results = {}
2.66 + for v in votes.values():
2.67 + results.setdefault(v, 0)
2.68 + results[v] += 1
2.69 + return results
2.70 +
2.71 +def execute(macro, args):
2.72 + args = args.split(",")
2.73 + args = map(unicode.strip, args)
2.74 +
2.75 + if len(args) < 1:
2.76 + return macro.formatter.rawHTML('<pre>[[Vote: Insufficient macro arguments]]</pre>')
2.77 +
2.78 + pageName = args[0]
2.79 + voteName = pageName
2.80 + candidates = ['1','2','3','4','5']
2.81 + src_litstar = '/wiki/modern/img/star_on.png'
2.82 + src_unlitstar = '/wiki/modern/img/star_off.png'
2.83 +
2.84 + form = macro.form
2.85 + request = macro.request
2.86 + if request.user.valid:
2.87 + voter = request.user.name
2.88 + else:
2.89 + voter = ''
2.90 + thisPage = macro.formatter.page
2.91 + votes = loadVotes(thisPage, voteName)
2.92 +
2.93 + # votes are stored in a dictionary as {user: candidate} to avoid duplicate votes
2.94 + # (a voter could switch their vote but this UI doesn't expose that facility)
2.95 + if form.has_key('vote') and form.has_key('voteName') and voter:
2.96 + if form['voteName'][0] == voteName:
2.97 + votes[voter] = form['vote'][0]
2.98 + try:
2.99 + saveVotes(thisPage, voteName, votes)
2.100 + except: # XXX
2.101 + return macro.formatter.rawHTML('<a id="voteform"><pre>[[Vote: failed to store vote]]</pre>')
2.102 +
2.103 + # generate dictionary {candidate: numvotes}
2.104 + results = countVotes(votes)
2.105 + onestar = results.setdefault(candidates[0],0)
2.106 + twostar = results.setdefault(candidates[1],0)
2.107 + threestar = results.setdefault(candidates[2],0)
2.108 + fourstar = results.setdefault(candidates[3],0)
2.109 + fivestar = results.setdefault(candidates[4],0)
2.110 + counts = (onestar+twostar+threestar+fourstar+fivestar)
2.111 +
2.112 + if counts == 0:
2.113 + average = 0
2.114 + else:
2.115 + average = (onestar*1+twostar*2+threestar*3+fourstar*4+fivestar*5)/counts
2.116 +
2.117 + hasVoted = voter in votes
2.118 +
2.119 + # spit out votes table (as part of a form if the user hasn't voted yet)
2.120 + html = '<a id="voteform"><table class="votestars"><tr><td>'
2.121 +
2.122 + if voter:
2.123 + html += '''
2.124 + <form method="get" name="%(formname)s" action="%(url)s#voteform">\n
2.125 + <input type="hidden" name="voteName" value="%(voteName)s">\n
2.126 + <input type="hidden" name="vote" value="0">
2.127 + ''' % {
2.128 + 'formname': voteName,
2.129 + 'url': thisPage.url(request),
2.130 + 'voteName': voteName,
2.131 + }
2.132 + btnstar = '<input type="image" src="%(isrc)s" OnClick="document.%(form)s.vote.value=%(value)s">\n'
2.133 + for i in range(1,6):
2.134 + if i <= average:
2.135 + html += btnstar % {'isrc':src_litstar, 'form':voteName, 'value': i}
2.136 + if i > average:
2.137 + html += btnstar % {'isrc':src_unlitstar, 'form':voteName, 'value': i}
2.138 + if average == 0:
2.139 + html += ' No Rating'
2.140 + #else:
2.141 + # html += ' %(avg)s ' % { 'avg': average }
2.142 + html += '</form></td></tr></table></a>'
2.143 + else:
2.144 + html += 'You must be logged in to Rate.</td></tr></table></a>'
2.145 +
2.146 + return macro.formatter.rawHTML(html)
2.147 +