-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoll.py
292 lines (252 loc) · 9.81 KB
/
poll.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/usr/bin/env python3.6
"""Poll class used by Schedubot.
Attributes:
VERSION (str): Version string
"""
from uuid import uuid4
import parser
from telegram import ParseMode, InlineKeyboardButton, InlineKeyboardMarkup, error
VERSION = 0.3
class Poll:
"""Poll class used by Schedubot.
Attributes:
id (str): Uuid(4) of this poll.
name (str): The name for this poll.
days (int): Number of days/ columns.
description (str): The description for this poll.
creator_id (int): Id of the user that created the poll.
open (bool): Whether votes can be cast for this poll.
users (list of str): Every user that has voted on this poll.
longest_user (int): Longest username's length.
single_votes (dict of str: str): A list of every user's votes.
day_sum (list of int): How many users voted '+' for any given day.
messages (list of list of int): List of messages to keep track of.
Contains the chat_id and message_id.
version (str): The version of this file that created the poll.
"""
def __init__(self, name, description, creator_id, days):
"""__init__
Args:
name (str): The name for this poll.
description (str): The description for this poll.
creator_id (int): Id of the user that created the poll.
days (int): Number of days/ columns.
"""
self.version = VERSION
self.users = []
self.longest_user = 0
self.single_votes = dict()
self.name = name
self.description = description
self.creator_id = creator_id
self.days = int(days)
self.day_sum = [0] * self.days
self.messages = []
self.open = True
self.id = str(uuid4())
def __str__(self):
"""__str__
Returns:
str: A string representing this poll.
"""
return self.__repr__()
def __repr__(self):
"""__repr__
Returns:
str: A string representing this poll.
"""
representation = "{"
try:
representation += "'version': " + repr(self.version)
except AttributeError:
representation += "'version': " + "unknown"
try:
representation += ", 'users': " + repr(self.users)
except AttributeError:
representation += ", 'users': " + "unknown"
try:
representation += ", 'longest_user': " + repr(self.longest_user)
except AttributeError:
representation += ", 'longest_user': " + "unknown"
try:
representation += ", 'single_votes': " + repr(self.single_votes)
except AttributeError:
representation += ", 'single_votes': " + "unknown"
try:
representation += ", 'name': " + repr(self.name)
except AttributeError:
representation += ", 'name': " + "unknown"
try:
representation += ", 'description': " + repr(self.description)
except AttributeError:
representation += ", 'description': " + "unknown"
try:
representation += ", 'creator_if': " + repr(self.creator_id)
except AttributeError:
representation += ", 'creator_id': " + "unknown"
try:
representation += ", 'days': " + repr(self.days)
except AttributeError:
representation += ", 'days': " + "unknown"
try:
representation += ", 'day_sum': " + repr(self.day_sum)
except AttributeError:
representation += ", 'day_sum': " + "unknown"
try:
representation += ", 'messages': " + repr(self.messages)
except AttributeError:
representation += ", 'messages': " + "unknown"
try:
representation += ", 'open': " + repr(self.open)
except AttributeError:
representation += ", 'open': " + "unknown"
representation += "}"
return representation
def get_id(self):
"""Getter for this poll's uuid.
Returns:
str: This poll's uuid.
"""
return self.id
def new_id(self): # really?
"""Generate a new uuid for this poll.
Returns:
str: The new uuid.
"""
self.id = str(uuid4())
return self.id
def get_creator_id(self):
"""Getter for this poll's creator_id.
Returns:
str: This poll's creator_id.
"""
return self.creator_id
def get_name(self):
"""Getter for this poll's name.
Returns:
str: This poll's name.
"""
return self.name
def set_name(self, name):
"""Setter for this poll's name.
Args:
name (str): The new name.
"""
self.name = name
def get_description(self):
"""Getter for this poll's description.
Returns:
str: This poll's description.
"""
return self.description
def set_description(self, description):
"""Setter for this poll's description.
Args:
description (str): The new description.
"""
self.description = description
def get_days(self):
"""Getter for this poll's days.
Returns:
str: This poll's days.
"""
return self.days
def knows_msg(self, msg):
"""Check whether the poll is tracking this message.
Args:
msg (list of int): The chat_id and message_id to check.
Returns:
bool: Whether the poll is tracking this message
"""
return [msg.chat_id, msg.message_id] in self.messages
def is_open(self):
"""Check whether votes can be cast for this poll.
Returns:
bool: Whether votes can be cast for this poll.
"""
return self.open
def vote(self, user, str_):
"""Add a user's vote to this poll.
Args:
user (str): The user who is casting his vote.
str_ (str): The user's vote.
"""
if not self.open:
return
if not user in self.users:
self.users.append(user)
if len(user) > self.longest_user:
self.longest_user = len(user)
self.single_votes[user] = parser.reduce(str_, self.days)
def to_text(self):
"""Generate a tabulated view of the poll to show to users.
Returns:
str: A tabulated view of the poll to show to users.
"""
out = f"<b>{parser.html_safe(self.name)}</b>{' (closed)' if not self.open else ''} ({self.days})\n{parser.html_safe(self.description)}\n<pre>\n"
self.day_sum = [0] * self.days
for user in self.users:
user_htmlsafe = parser.html_safe(f'{user:{self.longest_user}}')
out += f"{user_htmlsafe}: {parser.parse(self.single_votes[user])}\n"
for i in range(self.days):
if self.single_votes[user][i:i+1] == '+':
self.day_sum[i] += 1
out += "\n"
out += " " * (self.longest_user + 2) + parser.parse(self.day_sum) + "</pre>"
return out
def update(self, bot):
"""Update the messages the poll is keeping track of.
Args:
bot (telegram.Bot): The Bot used to update the messages.
"""
if self.open:
for msg in self.messages:
kbd = InlineKeyboardMarkup([[InlineKeyboardButton("vote", url=f'{bot.get_me().link}?start={self.id}')]])
try:
bot.edit_message_text(self.to_text(), chat_id=msg[0], message_id=msg[1], parse_mode=ParseMode.HTML, reply_markup=kbd)
except error.BadRequest:
pass
else:
for msg in self.messages:
try:
bot.edit_message_text(self.to_text(), chat_id=msg[0], message_id=msg[1], parse_mode=ParseMode.HTML)
except error.BadRequest:
pass
def print(self, bot, chat, votable=True):
"""Print the poll to a chat returns that message.
Args:
bot (telegram.Bot): The Bot used to send the message.
chat (telegram.Chat): The Chat to print the message in.
votable (bool, optional): Whether the message should include a "vote" button if it is votable.
Returns:
list of int: The message sent.
"""
if(self.open and votable):
kbd = InlineKeyboardMarkup([[InlineKeyboardButton("vote", url=f'{bot.get_me().link}?start={self.id}')]])
msg = bot.send_message(chat.id, self.to_text(), parse_mode=ParseMode.HTML, reply_markup=kbd)
self.add_msg([msg.chat_id, msg.message_id])
else:
msg = bot.send_message(chat.id, self.to_text(), parse_mode=ParseMode.HTML, reply_markup=None)
self.add_msg([msg.chat_id, msg.message_id])
return msg
def add_msg(self, msg):
"""Add a message to keep track of.
Args:
msg (list of int): The message's chat_id and message_id.
"""
self.messages.append(msg)
def close(self, user_id, bot):
"""End voting on the poll.
Args:
user_id (int): The user trying to close the poll.
bot (telegram.Bot): The Bot used to update the tracked messages.
Returns:
bool: Returns True on success, false on failure.
"""
if user_id == self.creator_id:
self.open = False
self.update(bot)
self.messages = []
return True
else:
return False