-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelga_contrib_updates.py
78 lines (60 loc) · 2.18 KB
/
helga_contrib_updates.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
import re
from datetime import datetime
from helga import log
from helga.db import db
from helga.plugins import command, match, PRIORITY_HIGH
logger = log.getLogger(__name__)
def _updates_command(client, channel, nick, message, cmd, args):
who = None
when = datetime.utcnow().date()
# We can search by date
if len(args) == 2:
who = args[0]
when = datetime.strptime(args[1], '%Y-%m-%d').date()
else:
try:
who = args[0]
except IndexError:
pass
else:
if re.match(r'[\d]{4}-[\d]{2}-[\d]{2}', who):
when, who = who, None
when = datetime.strptime(when, '%Y-%m-%d').date()
# Handle if the 'who' is actually a channel
if who and who.startswith('#'):
who, where = None, who
else:
where = channel
search = {
'where': where,
'when': {
'$gte': datetime(when.year, when.month, when.day, 0, 0, 0),
'$lte': datetime(when.year, when.month, when.day, 23, 59, 59),
}
}
client.me(channel, 'whispers to {0}'.format(nick))
if who:
client.msg(nick, 'Updates by {who} for {when}'.format(who=who, when=when))
search['who'] = who
else:
client.msg(nick, 'Updates for {when}'.format(when=when))
updates = db.updates.find(search)
for update in updates:
client.msg(nick, '({who}) {what}'.format(who=update['who'],
what=update['what']))
def _updates_match(client, channel, nick, message, matches):
logger.info('Adding a new standup update for {0}.'.format(nick))
db.updates.insert({
'who': nick,
'what': message,
'when': datetime.utcnow(),
'where': channel,
})
@match(r'^(?i)update[^\w]', priority=PRIORITY_HIGH)
@command('updates', help=('List standup updates. Usage: helga updates '
'[<nick>|<channel>] [YYYY-MM-DD]'))
def updates(client, channel, nick, message, *args):
if len(args) == 1:
return _updates_match(client, channel, nick, message, args[0])
else:
return _updates_command(client, channel, nick, message, args[0], args[1])