-
Notifications
You must be signed in to change notification settings - Fork 5
/
open_poll_handler.py
48 lines (37 loc) · 1.43 KB
/
open_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
from basic_poll_handler import *
name = "Open poll"
desc = "Like basic poll, but you can see who voted for what."
def evaluation(poll):
message = "This is an open poll. People will see what you voted for.\n"
for i, option in enumerate(poll['options']):
message += "\n"
message += "*{}: {}*".format(option['text'], num_votes(poll, i))
users = get_users_voting_for(poll, option)
for user in users:
message += "\n "
message += user
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['data']) == str(callback_data['i']):
# remove old vote
pass
else:
votes[user] = {
'data': callback_data['i'],
'name': name
}
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['data']:
return "You voted for \"{}\".".format(option['text'])
return "Your vote was removed."
def num_votes(poll, i):
return [val['data'] for val in poll['votes'].values()].count(i) if 'votes' in poll else 0
def get_users_voting_for(poll, option):
return [val['name'] for val in poll['votes'].values() if val['data'] == option['index']] if 'votes' in poll else []