-
Notifications
You must be signed in to change notification settings - Fork 5
/
basic_poll_handler.py
50 lines (39 loc) · 1.38 KB
/
basic_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
from base_poll_handler import *
name = "Basic poll"
desc = "A straightforward first-past-the-post poll."
def options(poll):
buttons = []
for i, option in enumerate(poll['options']):
votes = num_votes(poll, i)
buttons.append([{
'text': "{}{}{}".format(option['text'],
" - " if votes > 0 else "",
votes if votes > 0 else ""),
'callback_data': {'i': i},
}])
return buttons
def evaluation(poll):
message = ""
for i, option in enumerate(poll['options']):
message += "\n"
message += "{}: {}".format(option['text'], num_votes(poll, i))
return message
def handle_vote(votes, user, name, callback_data):
old_vote = None
if user in votes:
old_vote = votes.pop(user)
if old_vote is not None and str(old_vote) == str(callback_data['i']):
# remove old vote
pass
else:
votes[user] = callback_data['i']
def get_confirmation_message(poll, user):
votes = poll['votes']
if user in votes:
vote = votes[user]
for option in poll['options']:
if option['index'] == vote:
return "You voted for \"{}\".".format(option['text'])
return "Your vote was removed."
def num_votes(poll, i):
return list(poll['votes'].values()).count(i) if 'votes' in poll else 0