-
Notifications
You must be signed in to change notification settings - Fork 5
/
multiple_options_poll_handler.py
72 lines (58 loc) · 1.92 KB
/
multiple_options_poll_handler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from functools import reduce
from base_poll_handler import *
name = "Multiple options poll"
desc = "Lets you vote for multiple options"
def options(poll):
buttons = [[{
'text': "Clear my votes",
'callback_data': {'i': "C"}
}]]
for opt in poll['options']:
votes = num_votes_on_option(poll, opt['index'])
buttons.append([{
'text': "{}{}{}".format(opt['text'],
" - " if votes > 0 else "",
votes if votes > 0 else ""),
'callback_data': {'i': opt['index']}
}])
return buttons
def evaluation(poll):
message = ""
for option in poll['options']:
message += "\n"
message += "{}: {}".format(option['text'], num_votes_on_option(poll, option['index']))
return message
def handle_vote(votes, user, name, callback_data):
old_vote = None
if user in votes:
old_vote = votes.pop(user)
if callback_data['i'] == 'C':
# remove vote
pass
elif old_vote is not None and callback_data['i'] in old_vote:
old_vote.remove(callback_data['i'])
if old_vote:
votes[user] = old_vote
elif old_vote is not None:
old_vote.append(callback_data['i'])
votes[user] = old_vote
else:
votes[user] = [callback_data['i']]
def get_confirmation_message(poll, user):
votes = poll['votes']
if user in votes:
vote = votes[user]
opts = poll['options']
vote_set = [opt['text'] for opt in opts if opt['index'] in vote]
string = ",".join(vote_set) if vote_set else "nothing"
return "You voted for {}.".format(string)
return "Your vote was removed."
def num_votes_on_option(poll, index):
if 'votes' not in poll:
return 0
votes = poll['votes']
num = 0
for cast_vote in votes.values():
if index in cast_vote:
num += 1
return num