# HG changeset patch # User Paul Boddie # Date 1204419896 -3600 # Node ID 0fb526c272ae67a595bcbb6d5fc82bd3f5cd94cb # Parent 1657592ee6373fa317fa7a99c805f017b67a06f3 Changed the form name in VotingStars. Added a new VoteRecorder macro which does not show the state of the poll, and which records votes in separate files for each user. diff -r 1657592ee637 -r 0fb526c272ae macros/VoteRecorder.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/macros/VoteRecorder.py Sun Mar 02 02:04:56 2008 +0100 @@ -0,0 +1,112 @@ +# -*- coding: iso-8859-1 -*- +""" + MoinMoin - VoteRecorder Macro + + @copyright: 2008 by Paul Boddie + @license: GNU GPL (v2 or later), see COPYING.txt for details. + + This is like the VotingStars macro except that votes are logged for later + counting, and the current state of the poll is not shown during voting. +""" + +from MoinMoin.util import filesys +from MoinMoin import wikiutil +import os + +Dependencies = [] + +def get_vote_pathname(page, name): + votes_dir = page.getPagePath("poll") + if not os.path.exists(votes_dir): + filesys.makeDirs(votes_dir) + filename = wikiutil.quoteWikinameFS(name) + return os.path.join(votes_dir, filename) + +def save_vote(page, poll_name, username, vote): + path = get_vote_pathname(page, username) + f = open(path, "a") + try: + f.write(poll_name + "\t" + vote + "\n") + finally: + f.close() + +def get_votes(page, poll_name, username, vote): + path = get_vote_pathname(page, username) + f = open(path) + try: + voting = {} + for line in f.xreadlines(): + line = line.strip() + if line: + poll_name, vote = line.split("\t") + voting[poll_name] = vote + return voting + finally: + f.close() + +def execute(macro, args): + request = macro.request + form = macro.form + fmt = macro.formatter + page = fmt.page + + output = [] + + # Obtain the poll name for presentation purposes. + + args = args.split(",") + if args: + poll_name = args[0] + else: + output.append(fmt.sysmsg(on=1)) + output.append(fmt.text("Please specify a poll name!")) + output.append(fmt.sysmsg(on=0)) + return ''.join(output) + + # Show a message if the user is not logged in. + + # Get the user, if known. + + if not request.user.valid: + output.append(fmt.emphasis(on=1)) + output.append(fmt.text("Please log in to vote!")) + output.append(fmt.emphasis(on=0)) + return ''.join(output) + + username = request.user.name + + # Handle any vote. + + if form and form.has_key("vote") and form.has_key("poll"): + poll = form["poll"][0] + if poll == poll_name: + save_vote(page, poll, username, form["vote"][0]) + output.append(fmt.emphasis(on=1)) + output.append(fmt.text("Vote submitted!")) + output.append(fmt.emphasis(on=0)) + return ''.join(output) + + # Show the choices. + + values = { + "poll" : poll_name, "url" : page.url(request), + "on_image" : '/wiki/modern/img/star_on.png', + "off_image" : '/wiki/modern/img/star_off.png' + } + choices = range(1, 6) + + output.append(fmt.rawHTML('
' % values)) + output.append(fmt.rawHTML('' % values)) + for choice in choices: + values["choice"] = choice + output.append( + fmt.rawHTML(''' + + ''' % values)) + output.append(fmt.rawHTML('
')) + + return ''.join(output) + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 1657592ee637 -r 0fb526c272ae macros/VotingStars.py --- a/macros/VotingStars.py Sat Mar 01 21:57:01 2008 +0100 +++ b/macros/VotingStars.py Sun Mar 02 02:04:56 2008 +0100 @@ -29,10 +29,6 @@ VotingStars data is stored in data_dir/pages/pagename/votes (i.e. alongside the attachments directory for a page). - - You can customise the appearance by defining the following CSS styles: - table.votestars - ''' import os, pickle from MoinMoin import wikiutil @@ -120,7 +116,7 @@ if voter: html.append(''' -
\n + \n \n ''' % {