-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmanager.py
371 lines (310 loc) · 12.2 KB
/
manager.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
### Based on darrenwee/voglbot
#!/usr/bin/python
from datetime import datetime, timedelta
import time
from voglogger import logger
import pymongo
from settings_secret import HOSTNAME
from authorized import whoIs, number_of_clusters, number_of_cgs, cg_list, getCluster
import headmaster
import re
# establish connection to mongodb server
try:
connection = pymongo.MongoClient(HOSTNAME, 27017)
except pymongo.errors.ConnectionFailure as e:
logger.error('Failed to connect to MongoDB: %s' % e)
logger.error('ARIADNE exiting!')
sys.exit(1)
# database -> collection -> document
# database
db = connection['acglbot']
# collection
cgls = db['cgls'] # represents a user who is a cgl
cgs = db['cgs'] # represents the cg tally for attendance, one at a time.
tally = db['tally'] # represents the total tally for attendance, per a given event
state = db['state'] # represents an open/close state of an event
events = db['events'] # represents an event
events.create_index('end', expireAfterSeconds=0)
### helper functions ###
def cgIsValid(cg):
return cg in cg_list or cg == 'all'
def cglFieldIsValid(field):
fields = {
'name': True,
'cg': True,
'chatID': True,
}
return fields.get(field, False)
def cgFieldIsValid(field):
fields = {
'l': True, # Leaders
'ir': True, # Irregulars
'nc': True, # Newcomers
'f': True, # Freshies
'v': True, # Visitors
'nb': True, # New Believers
'rd': True, # Rededications
'total': True, # Total tally
}
return fields.get(field, False)
def enumerator(cursor, fields):
reply = ''
# catch size 0 cursor
if cursor.count() == 0:
return 'No records found.'
i = 1
for person in cursor:
reply += str(i) + '.\n'
for field in fields:
if field == 'cg':
reply += '%s: %s\n' % (field.title(), person[field][0].title())
else:
reply += '%s: %s\n' % (field.title(), person[field].title())
i += 1
reply += '\n'
return reply
def makeTimestamp():
return datetime.fromtimestamp(time.time()).strftime('%I:%M%p, %d %B')
# takes in a datetime object and adds count number of days to it
def daysFrom(dt, count):
return timedelta(days=count) + dt
# checks database of CGLs to see if they are registered
def exists(check_id):
if cgls.find_one( {'chatID': str(check_id)} ) == None:
return False
return True
### executive functions ###
# Retrieve a singular cgl dictionary
def retrieve(check_id):
return cgls.find_one( {'chatID': str(check_id)} )
def getMe(chatID):
cgl = retrieve(chatID)
return 'You are ' + cgl['name'].title() + ' from ' + cgl['cg'][0].upper() + '.\n\nContact @njyjn if not correct.'
def getName(chatID):
try:
return retrieve(chatID).get('name')
except:
return 'Unregistered user'
def getCG(chatID):
try:
return retrieve(chatID).get('cg')[0]
except:
return 'Unregistered user'
# /add
def add(cg, name, chatID):
if cgIsValid(cg):
# check for duplicate name
if cgls.find( {'name': name, 'cg': cg }).count() != 0:
logger.warn('/add failed due to duplicate entry')
return
timestamp = str(datetime.now())
logger.info('Adding \'%s\' from \'%s\' of id %s' % (name, cg, chatID))
cgl = {
'name': name,
'cg': [cg, 'all'],
'chatID': chatID
}
cgls.insert_one(cgl)
logger.info('Added %s of id \'%s\' to \'%s\' cg' % (name, chatID, cg))
return 'Thanks for waiting, %s. Welcome.' % name
logger.info('/add failed (CG does not exist)')
return
# /remove
def remove(cg, name, requester):
reply = 'Removed \'%s\' of \'%s\' cg from database.' % (name, cg)
name = name.title()
cg = cg.lower()
# perform remove
if cgls.find( {'name': name, 'cg': cg }).count() == 0:
return 'No such record found.'
elif cgls.find( {'name': name, 'cg': cg }).count() >= 1:
cgls.delete_one( {'name': name, 'cg': cg} )
logger.info(whoIs(requester) + ': ' + reply)
return reply
return 'Multiple records with the same name and cg found. Contact Justin for removal.'
def removeById(chatID):
logger.info('%s (%s) left.' % (whoIs(chatID), chatID))
return cgls.delete_one( {'chatID': str(chatID)} )
# /enum
def getEnumerate(cg, requester):
if cgIsValid(cg):
# cover the all case and single cg case
if cg == 'all':
cgs = cg_list
else:
cgs = [cg]
reply = ''
for target_cg in cgs:
reply += '=====================\n'
reply += target_cg.upper() + '\n\n'
# query for database cursor
results = cgls.find( {'cg': target_cg} )
# sort results
results.sort( [('name', 1)] )
#reply += status.title() + '\n'
# catch empty cg/mode query
if results.count() == 0:
reply += 'No records found.\n'
else:
# build the reply message
i = 1
for person in results:
reply += '%d. %-15s' % (i, person['name'].title())
i += 1
if (i+1)%2 == 0:
reply += '\n'
reply += '\n'
else:
# catch invalid parameters
logger.info('%s: /enum query failed (invalid parameters)' % whoIs(requester))
return 'Invalid cg or status. See \'/help enumerate\''
#logger.info('%s: Returning enumeration for \'%s\' in \'%s\'' % (whoIs(requester), status, cg))
logger.info('%s: Returning enumeration for \'%s\'' % (whoIs(requester), cg))
return reply
# /find
def find(cg, pattern, requester):
reply = ''
if cgIsValid(cg):
# query for database cursor
results = cgls.find( {'name': { '$regex': '.*' + pattern + '.*'}, 'cg': cg } )
# sort results
results.sort( [ ('name', 1) ] )
details = ['name', 'chatID']
# build the reply
reply += 'Finding any names containing \'%s\' for \'%s\'\n\n' % (pattern, cg)
reply += enumerator(results, details)
else:
# catch shitty parameters
logger.info('%s: /find query failed (invalid parameters)' % whoIs(requester))
return 'Invalid cg. See \'/help find\''
logger.info('%s: Returning find query for pattern \'%s\' for \'%s\'' % (whoIs(requester), pattern, cg))
return reply
# Prepare for yell
def prepareYell(audience):
# typeList = ['cgl', 'ps', 'member', 'cr', 'other']
if audience == 'all':
return cgls.find( {} )
else:
return cgls.find( { 'cg': { '$in': audience } } )
# /updater
def updater(cg, name, field, content, requester):
reply = 'Updating \'%s\' for \'%s\' in \'%s\' cg.\n\n' % (field, name, cg)
if cgIsValid(cg):
# check if result was found
if cgls.find( {'name': name, 'cg':cg} ) == None:
# no results
reply += 'Could not find \'%s\' from \'%s\' cg.' % (name, cg)
else:
# got results
if fieldIsValid(field) and field != 'cg':
logger.info('%s: Updating \'%s\' for \'%s\' in \'%s\' with: \'%s\'' % (whoIs(requester), field, name, cg, content))
cgls.update_one( {'name': name, 'cg': cg}, { '$set': { field: content } } ) # perform update
reply += 'Successfully updated \'%s\' field for \'%s\' in \'%s\' with: \'%s\'' % (field, name, cg, content)
else:
reply += 'Invalid field.'
return reply
# /newevent
# creates a new attendance taking event which lasts 3 days.
# you cannot create another event until then.
def raiseEvent(name):
if eventDoesNotExist():
begins = datetime.today()
expires = daysFrom(begins, 3)
expires_string = expires.strftime('%c')
events.save( { 'name': name, 'start': begins, 'end': expires, 'done': False, 'tally': 0, } )
return 'You have created a new attendance event \'%s\' which expires %s' % (name, expires_string)
else:
return 'A counting event is still ongoing/has been completed. If the latter you must wait three days like Jesus.'
def eventDoesNotExist():
return events.find_one( {} ) == None
def eventHasEnded():
return events.find_one( {} )['done'] == True
# /endevent
def forceEndEvent():
event_name = events.find_one( {} )['name']
events.update_one( { 'done': False }, { '$set': { 'done': True } } )
return 'Attendance event \'%s\' ended.' % event_name
def forceDeleteEvent():
reset()
events.delete_one( {} )
logger.info('Event manually deleted.')
return 'Event deleted.'
# reopen done event
def reopenEvent():
event_name = events.find( {} )[0]['name']
events.update_one( { 'done': True }, { '$set': { 'done': False } } )
return 'Attendance event \'%s\' reopened. Now accepting /count commands.' % event_name
# /getevents
def getEvents():
return events.find( {} )
def setAttendanceDoneForEvent(cg, isFirstTry):
logger.info('%s has added its attendance record.' % cg)
isit = False
# subsequent tries will not increment the tally count, allowing CGs to edit their attendance before all CGs have submitted.
if isFirstTry:
events.update_one( { 'done': False }, { '$inc': { 'tally': 1 } } )
if lastToSubmitAttendance():
isit = True
return isit
def lastToSubmitAttendance():
cursor = events.find_one( { 'done': False } )
#offset for other, pastor, cr, overseer
#return cursor['tally'] >= (len(cg_list)-1)-len(cg_cluster_dictionary))
return cursor['tally'] >= number_of_cgs
def submitClusterAttendance(cg):
cluster = getCluster(cg)
updateClusterAttendance(cluster)
updateTotalAttendance()
#return headmaster.printGrandTally()
## CG functions
# /updateAttendance
def updateAttendance(cg, field, number):
if cgIsValid(cg):
cluster = getCluster(cg)
cgs.update_one( { '$and': [ {'name': cg}, {'cluster': cluster} ] }, { '$set': { field: str(number) } }, upsert=True )
def isAllSubmitted(cluster):
results = cgs.find( { '$and': [ {'done': True }, {'cluster': cluster} ] } )
if results.count() == cgs.find( { 'cluster': cluster } ).count():
updateClusterAttendance(cluster)
reset()
return True
return False
def isFirstTry(cg):
return cgs.find_one( { 'name': cg } ) == None
def reset():
# fieldList = ['l','f','ir','nb','nc','v','total']
# for field in fieldList:
# cgs.update( {}, { '$set': { { field: '0' }, { 'done': False } } } )
cgs.remove()
tally.remove()
# /updateClusterAttendance
# This will update the total attendance for the cluster.
def updateClusterAttendance(cluster):
cgList = cgs.find( {'cluster': cluster} )
total = totalL = totalF = totalIR = totalNC = totalNB = totalRD = totalV = 0
for cg in cgList:
total += int(cg.get('total', 0))
totalL += int(cg.get('l', 0))
totalF += int(cg.get('f', 0))
totalIR += int(cg.get('ir', 0))
totalNC += int(cg.get('nc', 0))
totalNB += int(cg.get('nb', 0))
totalRD += int(cg.get('rd', 0))
totalV += int(cg.get('v', 0))
tally.update_one( { 'cluster': cluster }, { '$set': { 'total': total, 'l': totalL, 'f': totalF, 'ir': totalIR, 'nc': totalNC, 'nb': totalNB, 'rd': totalRD, 'v': totalV } }, upsert=True )
def updateTotalAttendance():
clusterList = tally.find( { 'cluster': { '$ne': 'all'} } )
# Only bother if all clusters have submitted attendance
if clusterList.count() < number_of_clusters - 1:
return
total = totalL = totalF = totalIR = totalNC = totalNB = totalRD = totalV = 0
for cluster in clusterList:
total += int(cluster.get('total', 0))
totalL += int(cluster.get('l', 0))
totalF += int(cluster.get('f', 0))
totalIR += int(cluster.get('ir', 0))
totalNC += int(cluster.get('nc', 0))
totalNB += int(cluster.get('nb', 0))
totalRD += int(cluster.get('rd', 0))
totalV += int(cluster.get('v', 0))
tally.update_one( { 'cluster': 'all' }, { '$set': { 'total': total, 'l': totalL, 'f': totalF, 'ir': totalIR, 'nc': totalNC, 'nb': totalNB, 'rd': totalRD, 'v': totalV } }, upsert=True )