Edgewall Software

MacroBazaar: TracPoll.py

File TracPoll.py, 2.1 KB (added by athomas, 8 years ago)

TracPoll? macro

Line 
1import re
2import neo_util
3import pickle
4from os import mkdir
5
6# Set this to where you want the poll state to be stored
7polldir = "/var/state/trac/polls"
8
9def title2label(title):
10        fix = re.compile('[^a-zA-Z0-9]')
11        return fix.sub('', title)
12
13class Poll:
14        def __init__(self, title, options):
15                self.label = title2label(title)
16                self.title = title
17                self.options = []
18                for option in options:
19                        self.options.append([option, {}])
20
21        def render(self, hdf):
22                html = "<form action='#%s' method='get'>\n" % self.label
23                html += "<fieldset>\n<legend>Poll</legend>\n"
24                html += "<input type='hidden' name='poll' value='%s'>\n" % self.label
25
26                user = hdf.getValue("trac.authname", "")
27
28                error = ""
29
30                html += "<a name='%s'><strong>%s</strong></a><p/>\n" % (self.label, self.title)
31
32                # Check for existing vote
33                if hdf.getValue("args.poll", ""):
34                        for i, option in enumerate(self.options):
35                                if hdf.getValue("trac.authname", "") in option[1] and hdf.getValue("args.pollvalue", "") != option[0]:
36                                        error = "<div class='system-message'><strong>Changed your vote.</strong></div>\n"
37                                        del(self.options[i][1][user])
38                       
39                for i, option in enumerate(self.options):
40                        label = title2label(option[0])
41                        checked = ""
42                        if hdf.getValue("args.poll", "") == self.label and hdf.getValue("args.pollvalue", "") == option[0]:
43                                self.options[i][1][user] = 1
44                        if hdf.getValue("trac.authname", "") in option[1]:
45                                checked = "checked"
46                        voters = ""
47                        if len(option[1]):
48                                voters = " (%s)" % ", ".join(option[1].keys())
49                        html += "<input type='radio' name='pollvalue' value='%s'%s> %s%s<br>\n" % (option[0], checked, option[0], voters)
50                html += "<br>\n<input type='submit' value='Vote'>\n"
51                html += error
52                html += "</fieldset>\n"
53                html += "</form>\n"
54                return html
55
56def execute(hdf, txt, env):
57        args = re.split('\s*,\s*', txt)
58        path = "%s/%s" % (polldir, title2label(hdf.getValue("project.name.encoded", "default")))
59        try:
60                mkdir(path)
61        except:
62                pass
63        path += "/%s.p" % title2label(args[0])
64        html = ""
65        try:
66                poll = pickle.load(open(path, "r"))
67        except:
68                poll = Poll(args.pop(0), args)
69        html += poll.render(hdf)
70        pickle.dump(poll, open(path, "w"))
71        return html# + "<pre>" + hdf.dump() + "</pre>"