-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathschema.py
396 lines (318 loc) · 13.3 KB
/
schema.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
from collections import defaultdict
import datetime
import logging
import random
from utils import Utils
from google.appengine.ext import ndb
from oauth2client.appengine import CredentialsNDBProperty
class Gamenight(ndb.Model):
"""Gamenights that have been scheduled."""
invitation = ndb.KeyProperty('a', kind='Invitation')
event = ndb.StringProperty('e')
status = ndb.StringProperty('s', choices=['Yes', 'Probably', 'Maybe', 'No'])
lastupdate = ndb.DateTimeProperty('u')
# denormalized from invitation for datastore efficiency
date = ndb.DateProperty('d')
time = ndb.TimeProperty('t')
owner = ndb.KeyProperty('o', kind='User')
location = ndb.StringProperty('l', indexed=False)
notes = ndb.StringProperty('n', indexed=False)
def _pre_put_hook(self):
creds = Auth.get()
if not creds:
logging.warn('No credentials found, event not updated.')
return
service = Utils.get_service(Auth)
calendar_id = self._get_config('calendar_id')
if self.status == 'Yes':
if self.event:
logging.info('Updating event for %s.', self.date)
event = service.events().get(calendarId=calendar_id,
eventId=self.event).execute()
event.update(self._make_event())
service.events().update(calendarId=calendar_id,
eventId=self.event,
body=event).execute()
else:
logging.info('Creating new event for %s.', self.date)
event = self._make_event()
newevent = service.events().insert(calendarId=calendar_id,
body=event).execute()
self.event = newevent['id']
logging.info('New event: %r.', newevent)
else:
if self.event:
logging.info('Deleting event for %s.', self.date)
service.events().delete(
calendarId=calendar_id, eventId=event).execute()
@classmethod
def _pre_delete_hook(cls, key):
gn = key.get()
eventid = gn.event
if not eventid:
return
service = Utils.get_service(Auth)
calendar_id = gn._get_config('calendar_id')
logging.info('Deleting event: %s', eventid)
service.events().delete(
calendarId=calendar_id, eventId=eventid).execute()
def _get_config(self, key):
conf = Config.query(Config.name==key).get()
if conf:
return conf.value
else:
return None
def _make_event(self):
if not self.time:
self.time = datetime.time(20, 0, 0)
start = datetime.datetime.combine(self.date, self.time)
event = {
'start': { 'dateTime': start.strftime('%Y-%m-%dT%H:%M:%S'),
'timeZone': 'America/Los_Angeles' },
'end': { 'dateTime': self.date.strftime('%Y-%m-%dT23:59:59'),
'timeZone': 'America/Los_Angeles' },
'description': self.notes,
'location': self.location,
'summary': 'Gamenight: %s' % self.status.upper(),
}
return event
@classmethod
def schedule(cls, date=None, status=None, fallback='Probably', priority=None):
# If no date is specified, look for the next invitation that is no
# later than saturday
if date is None:
upcoming = Invitation.query(Invitation.date >= Utils.now().date()).\
order(Invitation.date).get()
if upcoming is None or upcoming.date > Utils.saturday().date():
logging.info('No future invitation found.')
return None
else:
date = upcoming.date
schedule = cls.query(cls.date==date).filter(cls.status=='Yes').get() or \
Invitation.resolve(when=date, priority=priority) or \
cls.query(cls.date==date).get() or \
Gamenight(status=fallback,
date=date,
lastupdate=Utils.now())
if status is not None and schedule.status != 'Yes':
schedule.status = status
schedule.put()
logging.info('Scheduling new gamenight: %r', schedule)
return schedule
@classmethod
def reset(cls, date=None):
if date is None:
date = Utils.saturday()
schedule = Invitation.resolve(when=date, priority='Insist') or \
cls.query(cls.date==date).get() or \
Gamenight(status='Probably',
date=date,
lastupdate=Utils.now())
schedule.put()
logging.info('Resetting gamenight page: %r', schedule)
return schedule
@classmethod
def future(cls, limit):
return cls.query(cls.date >= Utils.now()).order(cls.date).fetch(limit)
@classmethod
def this_week(cls):
"""Get this week's gamenight."""
g = cls.future(1)
if g and g[0].is_this_week():
return g[0]
else:
return None
def update(self):
if not self.invitation:
return False
invite = self.invitation.get()
self.time = invite.time
self.location = invite.location
self.notes = invite.notes
self.put()
return True
def is_this_week(self):
return self.date - Utils.now().today().date() < datetime.timedelta(7)
class Invitation(ndb.Model):
"""Entries of offers to host."""
date = ndb.DateProperty('d')
time = ndb.TimeProperty('t')
owner = ndb.KeyProperty('o', kind='User')
location = ndb.StringProperty('l', indexed=False)
notes = ndb.StringProperty('n', indexed=False)
priority = ndb.StringProperty('p', choices=['Can', 'Want', 'Insist'])
def text_date(self):
date = datetime.datetime.combine(self.date, self.time)
#if self.date == Utils.now().date().today():
# return self.time.strftime('Today, %I:%M %p')
if datetime.timedelta(0) < date - Utils.now() < datetime.timedelta(6):
return date.strftime('%A, %I:%M %p')
return date.strftime('%b %d, %I:%M %p')
datetext = ndb.ComputedProperty(text_date)
text_pri = { 'Can': 'Happy to host',
'Want': 'Want to host',
'Insist': 'Would really want to host' }
priority_text = ndb.ComputedProperty(lambda self: self.text_pri[self.priority])
@classmethod
def get(cls, key):
return ndb.Key(cls, 'root', cls, int(key)).get()
@classmethod
def dummy(cls):
return ndb.Key(cls, 'root')
@classmethod
def resolve(cls, when=Utils.saturday(), history=4, priority=None):
"""Figure out where GN should be at the given date.
By default, consider the last 4 to give preference to people who
haven't been hosting."""
if type(when) != datetime.date:
when = when.date()
logging.info('Resolving gamenight for %s', when)
invitations = cls.query(cls.date == when)
logging.debug('Query: %r' % invitations)
if priority is not None:
priorities = (priority,)
else:
priorities = ('Insist', 'Want', 'Can')
candidates = []
# check each level separately
for pri in priorities:
candidates = dict([(x.owner.id(), x) for x in
invitations.filter(cls.priority == pri).fetch()])
# no matches at this priority
if not candidates:
logging.debug('none at priority %s' % pri)
continue
# no need to look at lower levels
logging.debug('Candidates(%s): %r' % (pri, candidates))
break
# no one wants to host :(
if not candidates:
logging.debug('none found.')
return None
# more than one option, filter out the recent hosts until we run out of
# recent hosts, or have just one option left.
logging.debug('Candidates: %r' % candidates.keys())
if len(candidates) > 1:
if history:
old_nights = Gamenight.query(Gamenight.date < Utils.now()).\
order(Gamenight.date).fetch(history)
while old_nights and len(candidates) > 1:
owner = old_nights.pop().owner.id()
if owner in candidates:
logging.debug('removing previous host %s' % owner)
del candidates[owner]
logging.debug('Not recent candidates: %r' % candidates.keys())
# pick one at random, return the invitation object
selected = random.choice(candidates.keys())
logging.debug('Selected invitation: %s\n%r' %
(selected, candidates[selected]))
return candidates[selected].make_gamenight()
@classmethod
def summary(cls):
"""Get upcoming saturdays, and who has invited.
Returns a dictionary of dates, with a list of (who, priority) for each.
"""
invitations = cls.query(cls.date >= Utils.now()).\
filter(cls.date < Utils.now() +
datetime.timedelta(weeks=8)).\
order(cls.date)
res = {}
invlist = defaultdict(list)
for invite in invitations.iter():
invlist[invite.date].append(invite.owner.get().name)
for date, invites in invlist.iteritems():
res[date] = ', '.join(name for name in invites)
return res
@classmethod
def create(cls, args):
"""Create or update an invitation.
Any given owner can have just invite per date."""
invite = Invitation.query(Invitation.date == args['when']).\
filter(Invitation.owner == args['owner']).get()
if invite:
invite.location = args['where']
invite.notes = args['notes']
invite.priority = args['priority']
updated = True
else:
invite = Invitation(date = args['when'].date(),
time = args['when'].time(),
owner = args['owner'],
location = args['where'],
notes = args['notes'],
priority = args['priority'],
parent = Invitation.dummy(),
)
updated = False
ndb.transaction(lambda: invite.put())
return updated, invite
def make_gamenight(self, overwrite=False):
"""Create an unsaved gamenight object from an invitation.
Args:
overwrite - if an existing GN is already scheduled, replace it. If
false, return it unchanged.
"""
gamenight = Gamenight.query(Gamenight.date==self.date).get()
if gamenight and not overwrite:
if gamenight.status == 'Yes':
return gamenight
if not gamenight:
gamenight = Gamenight(date=self.date)
gamenight.invitation = self.key
gamenight.status = 'Yes'
gamenight.lastupdate = Utils.now()
gamenight.time = self.time
gamenight.owner = self.owner
gamenight.location = self.location
gamenight.notes = self.notes
return gamenight
class User(ndb.Model):
"""Accounts of people who host."""
location = ndb.StringProperty('l', indexed=False)
color = ndb.StringProperty('c', indexed=False)
superuser = ndb.BooleanProperty('s')
nag = ndb.BooleanProperty('e')
name = ndb.StringProperty('n')
@classmethod
def lookup(cls, key):
return ndb.Key(cls, key).get()
@classmethod
def get(cls, userobj):
user = ndb.Key(cls, userobj.email()).get()
if user is None:
user = cls(id=userobj.email())
user.name = userobj.nickname()
if user.name in [None, 'None', ''] or user.name.find("http://") > -1:
animals = [ 'bear', 'emu', 'zebu', 'snake', 'bird', 'awk',
'quahog', 'rutabaga', 'rabbit', 'dragon', 'boar',
'horse', 'crab', 'fish', 'libra', 'alicorn',
'moose', 'geoduck', 'Nudibranch' ]
user.name = 'Some %s' % random.choice(animals).title()
user.put()
return user
class Config(ndb.Model):
"""Store application configuration values."""
name = ndb.StringProperty('n')
value = ndb.StringProperty('v')
@classmethod
def update(cls, key, val):
c = cls.query(cls.name==key).get()
if not c:
logging.warning('Trying to update an unknown key %s: %s', key, val)
return None
if c.value == val:
return False
c.value = val
c.put()
return True
class Auth(ndb.Model):
"""Store approximately one record, the oauth token for calendar access."""
credentials = CredentialsNDBProperty('c')
@classmethod
def get(cls):
creds = cls.query().get()
if creds:
return creds.credentials
else:
return None
# vim: set ts=4 sts=4 sw=4 et: