paul@36 | 1 | # -*- coding: iso-8859-1 -*- |
paul@36 | 2 | """ |
paul@36 | 3 | MoinMoin - VoteRecorder Macro |
paul@36 | 4 | |
paul@36 | 5 | @copyright: 2008 by Paul Boddie <paul@boddie.org.uk> |
paul@36 | 6 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@36 | 7 | |
paul@36 | 8 | This is like the VotingStars macro except that votes are logged for later |
paul@36 | 9 | counting, and the current state of the poll is not shown during voting. |
paul@36 | 10 | """ |
paul@36 | 11 | |
paul@36 | 12 | from MoinMoin.util import filesys |
paul@36 | 13 | from MoinMoin import wikiutil |
paul@36 | 14 | import os |
paul@36 | 15 | |
paul@36 | 16 | Dependencies = [] |
paul@36 | 17 | |
paul@36 | 18 | def get_vote_pathname(page, name): |
paul@36 | 19 | votes_dir = page.getPagePath("poll") |
paul@36 | 20 | if not os.path.exists(votes_dir): |
paul@36 | 21 | filesys.makeDirs(votes_dir) |
paul@36 | 22 | filename = wikiutil.quoteWikinameFS(name) |
paul@36 | 23 | return os.path.join(votes_dir, filename) |
paul@36 | 24 | |
paul@36 | 25 | def save_vote(page, poll_name, username, vote): |
paul@36 | 26 | path = get_vote_pathname(page, username) |
paul@36 | 27 | f = open(path, "a") |
paul@36 | 28 | try: |
paul@36 | 29 | f.write(poll_name + "\t" + vote + "\n") |
paul@36 | 30 | finally: |
paul@36 | 31 | f.close() |
paul@36 | 32 | |
paul@36 | 33 | def get_votes(page, poll_name, username, vote): |
paul@36 | 34 | path = get_vote_pathname(page, username) |
paul@36 | 35 | f = open(path) |
paul@36 | 36 | try: |
paul@36 | 37 | voting = {} |
paul@36 | 38 | for line in f.xreadlines(): |
paul@36 | 39 | line = line.strip() |
paul@36 | 40 | if line: |
paul@36 | 41 | poll_name, vote = line.split("\t") |
paul@36 | 42 | voting[poll_name] = vote |
paul@36 | 43 | return voting |
paul@36 | 44 | finally: |
paul@36 | 45 | f.close() |
paul@36 | 46 | |
paul@36 | 47 | def execute(macro, args): |
paul@36 | 48 | request = macro.request |
paul@36 | 49 | form = macro.form |
paul@36 | 50 | fmt = macro.formatter |
paul@36 | 51 | page = fmt.page |
paul@36 | 52 | |
paul@36 | 53 | output = [] |
paul@36 | 54 | |
paul@36 | 55 | # Obtain the poll name for presentation purposes. |
paul@36 | 56 | |
paul@36 | 57 | args = args.split(",") |
paul@36 | 58 | if args: |
paul@36 | 59 | poll_name = args[0] |
paul@36 | 60 | else: |
paul@36 | 61 | output.append(fmt.sysmsg(on=1)) |
paul@36 | 62 | output.append(fmt.text("Please specify a poll name!")) |
paul@36 | 63 | output.append(fmt.sysmsg(on=0)) |
paul@36 | 64 | return ''.join(output) |
paul@36 | 65 | |
paul@36 | 66 | # Show a message if the user is not logged in. |
paul@36 | 67 | |
paul@36 | 68 | # Get the user, if known. |
paul@36 | 69 | |
paul@36 | 70 | if not request.user.valid: |
paul@36 | 71 | output.append(fmt.emphasis(on=1)) |
paul@36 | 72 | output.append(fmt.text("Please log in to vote!")) |
paul@36 | 73 | output.append(fmt.emphasis(on=0)) |
paul@36 | 74 | return ''.join(output) |
paul@36 | 75 | |
paul@36 | 76 | username = request.user.name |
paul@36 | 77 | |
paul@36 | 78 | # Handle any vote. |
paul@36 | 79 | |
paul@36 | 80 | if form and form.has_key("vote") and form.has_key("poll"): |
paul@36 | 81 | poll = form["poll"][0] |
paul@36 | 82 | if poll == poll_name: |
paul@36 | 83 | save_vote(page, poll, username, form["vote"][0]) |
paul@36 | 84 | output.append(fmt.emphasis(on=1)) |
paul@36 | 85 | output.append(fmt.text("Vote submitted!")) |
paul@36 | 86 | output.append(fmt.emphasis(on=0)) |
paul@36 | 87 | return ''.join(output) |
paul@36 | 88 | |
paul@36 | 89 | # Show the choices. |
paul@36 | 90 | |
paul@36 | 91 | values = { |
paul@36 | 92 | "poll" : poll_name, "url" : page.url(request), |
paul@36 | 93 | "on_image" : '/wiki/modern/img/star_on.png', |
paul@36 | 94 | "off_image" : '/wiki/modern/img/star_off.png' |
paul@36 | 95 | } |
paul@36 | 96 | choices = range(1, 6) |
paul@36 | 97 | |
paul@36 | 98 | output.append(fmt.rawHTML('<form method="get" name="%(poll)s" action="%(url)s#%(poll)s">' % values)) |
paul@36 | 99 | output.append(fmt.rawHTML('<input type="hidden" name="poll" value="%(poll)s">' % values)) |
paul@36 | 100 | for choice in choices: |
paul@36 | 101 | values["choice"] = choice |
paul@36 | 102 | output.append( |
paul@36 | 103 | fmt.rawHTML(''' |
paul@36 | 104 | <input type="image" name="vote" value="%(choice)s" src="%(off_image)s" |
paul@36 | 105 | onmouseover="this.src='%(on_image)s'" |
paul@36 | 106 | onmouseout="this.src='%(off_image)s'"> |
paul@36 | 107 | ''' % values)) |
paul@36 | 108 | output.append(fmt.rawHTML('</form>')) |
paul@36 | 109 | |
paul@36 | 110 | return ''.join(output) |
paul@36 | 111 | |
paul@36 | 112 | # vim: tabstop=4 expandtab shiftwidth=4 |