-
Notifications
You must be signed in to change notification settings - Fork 1
/
gamenight.py
495 lines (399 loc) · 16.5 KB
/
gamenight.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), 'lib'))
from collections import defaultdict
from datetime import datetime, timedelta, time
from dateutil import parser
import jinja2
import logging
import os
import urllib
import webapp2
from google.appengine.api import mail, users
from oauth2client.appengine import OAuth2Decorator
from pprint import pformat
from schema import Gamenight, Invitation, User, Config, Auth
from utils import Utils
JINJA_ENVIRONMNT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'])
config = {x.name: x.value for x in Config.query().fetch()}
decorator = OAuth2Decorator(
client_id=config.get('client_id'),
client_secret=config.get('client_secret'),
scope='https://www.googleapis.com/auth/calendar')
def admin_only(func):
def dec(self, **kwargs):
sys_user = users.get_current_user()
if not sys_user:
self.redirect(users.create_login_url(self.request.uri))
return
user = User.lookup(sys_user.email())
if not user or not user.superuser:
self.redirect("/invite")
return
return func(self, **kwargs)
return dec
def logged_in(func):
def dec(self, **kwargs):
sys_user = users.get_current_user()
if not sys_user:
self.redirect(users.create_login_url(self.request.uri))
return
return func(self, **kwargs)
return dec
class ApiAuth(webapp2.RequestHandler):
@admin_only
@decorator.oauth_aware
def get(self):
if not decorator.has_credentials():
logging.info("no creds, redirecting to auth")
self.redirect(decorator.authorize_url())
return
logging.info("has creds, updating auth")
auth = Auth.get_or_insert('auth')
auth.credentials = decorator.get_credentials()
auth.put()
self.redirect('/config')
class ConfigPage(webapp2.RequestHandler):
@admin_only
def get(self, error=None, msg=None, flags={}):
user = User.get(users.get_current_user())
if not user.superuser:
self.redirect('/invite',
error="Must be an admin to edit configuration.")
return
template_values = {
'config': config,
'flags': flags,
'logout': users.create_logout_url('/'),
'user': user,
'error': error,
'msg': msg,
}
template = JINJA_ENVIRONMNT.get_template('config.html')
self.response.write(template.render(template_values))
@admin_only
def post(self):
user = User.get(users.get_current_user())
if not user.superuser:
self.redirect('/invite',
error="Must be an admin to edit configuration.")
return
err = []
msg = []
flags = {}
for name in config.keys():
v = self.request.get('config_%s' % name)
if v != "":
logging.info("Updating %s: %s" % (name, v))
flags[name] = Config.update(name, v)
if flags[name] == True:
config[name] = v
elif flags[name] is None:
err.append("Failed to update %s." % name)
else:
logging.info("Deleting key '%s'..." % name)
Config.query(Config.name==name).get().key.delete()
del(config[name])
msg.append("'%s' deleted." % name)
logging.info("Deleted key '%s'." % name)
new_name = self.request.get('new_name')
if new_name:
value = self.request.get('new_value')
logging.info("New config key %s: %s" % (new_name, value))
Config(name=new_name, value=value).put()
flags[new_name] = True
config[new_name] = value
self.get(error=", ".join(err), msg=", ".join(msg), flags=flags)
class InvitePage(webapp2.RequestHandler):
@logged_in
def get(self, template_values={}, msg=None, error=None):
user = User.get(users.get_current_user())
if user.superuser:
invitations = Invitation.query(ancestor=Invitation.dummy())
else:
invitations = Invitation.query(Invitation.owner==user.key,
ancestor=Invitation.dummy())
invitations = invitations\
.filter(Invitation.date >= Utils.now())\
.order(Invitation.date).iter()
template_values.update({
'user': user,
'msg': msg,
'error': error,
'invitations': invitations,
'logout': users.create_logout_url('/'),
})
if not template_values.has_key('where') and user.location:
template_values['where'] = user.location
template = JINJA_ENVIRONMNT.get_template('invite.html')
self.response.write(template.render(template_values))
@logged_in
def post(self, template_values={}):
user = User.get(users.get_current_user())
if self.request.get('withdraw'):
invite = Invitation.get(self.request.get('withdraw'))
if not invite:
self.get(error="Can't find this invitation.")
return
if invite.owner != user.key and not user.superuser:
self.get(error='Not your invitation.')
return
msg = ''
gn = Gamenight.query(Gamenight.invitation==invite.key).get()
logging.info('Invite id: %s, gn: %s', invite.key, gn)
invite.key.delete()
msg = 'Invitation withdrawn. '
if gn:
gn.key.delete()
msg += 'Rescheduling gamenight. '
Gamenight.schedule()
self.get(msg=msg)
return
args = {}
for k in ['when', 'where', 'priority', 'notes']:
args[k] = self.request.get(k)
error = None
warning = None
msg = ''
if args['when']:
try:
orig = args['when']
args['when'] = parser.parse(args['when'].replace('today', ''))
logging.info('Parsed "%s" as "%s"', orig, args['when'])
except ValueError:
error = 'Not sure what you mean by "%s"' % args['when']
logging.error('Failed to parse when: %s', args['when'])
else:
checks = []
if not time(18, 0, 0) <= args['when'].time() <= time(21, 0, 0):
checks.append(args['when'].time().strftime('%I:%M %p'))
if args['when'].date().weekday() != 5:
checks.append(args['when'].date().strftime('%A'))
if args['when'].date() < Utils.now().date():
checks.append(args['when'].date().strftime('%x'))
if checks:
warning = 'Just checking, did you really mean %s?' % ', '.join(checks)
else:
error = 'When do you want to host?'
if error:
self.get(template_values=args, error=error)
return
if not args['where']:
error = 'Where do you want to host?'
self.get(template_values=args, error=error)
return
if not args['priority']:
error = '''Gotta have a priority. Also, don't mess with me.'''
self.get(template_values=args, error=error)
return
args['owner'] = user.key
updated, invite = Invitation.create(args)
if updated:
gn = Gamenight.query(Gamenight.invitation==invite.key).get()
if gn:
gn.update()
msg += 'Invitation and gamenight updated! '
else:
msg += 'Invitation updated! '
self.get(msg=msg, error=warning)
else:
msg += 'Invitation created! '
self.get(msg=msg, error=warning)
class ProfilePage(webapp2.RequestHandler):
@logged_in
def get(self, template_values={}, msg=None, error=None, profile=None):
user = User.get(users.get_current_user())
template_values.update({
'user': user,
'msg': msg,
'error': error,
'logout': users.create_logout_url('/'),
})
template_values['profile'] = user
if user.superuser:
template_values['users'] = User.query().fetch()
if profile:
template_values['profile'] = User.lookup(profile)
if not profile:
template_values['profile'] = user
template_values['error'] = "Couldn't find user %s" % profile
template = JINJA_ENVIRONMNT.get_template('profile.html')
self.response.write(template.render(template_values))
@logged_in
def post(self):
user = User.get(users.get_current_user())
if user.superuser:
edit = self.request.get('edit', False)
if edit:
self.get(profile=edit, msg='Editing %s' % edit)
return
profile = User.lookup(self.request.get('pid'))
profile.superuser = self.request.get('admin')=='on'
else:
profile = user
profile.location = self.request.get('location')
profile.name = self.request.get('name')
profile.nag = self.request.get('nag')=='on'
profile.put()
self.get(msg='Profile updated!', profile=profile.key.id())
class SchedulePage(webapp2.RequestHandler):
def get(self):
days = defaultdict(dict)
for gn in Gamenight.future(10):
days[gn.date]['date'] = gn.date
days[gn.date]['scheduled'] = True
days[gn.date]['status'] = gn.status
days[gn.date]['owner'] = gn.owner
days[gn.date]['time'] = gn.time
days[gn.date]['where'] = gn.location
invitations = Invitation.query(Invitation.date >= Utils.now()).\
order(Invitation.date).iter()
for invite in invitations:
if not days[invite.date].get('invitations'):
days[invite.date]['date'] = invite.date
days[invite.date]['invitations'] = []
days[invite.date]['invitations'].append(invite)
day_sorter = lambda x: x.get('date')
template_values = { 'days': sorted(days.values(), key=day_sorter) }
current_user = users.get_current_user()
if current_user:
user = User.get(users.get_current_user())
template_values.update({
'logout': users.create_logout_url('/'),
'user': user,
})
template = JINJA_ENVIRONMNT.get_template('schedule.html')
self.response.write(template.render(template_values))
class MainPage(webapp2.RequestHandler):
def get(self):
futurenights = Gamenight.future(10)
if futurenights and futurenights[0].this_week():
# we have a gamenight scheduled for this week
gamenight = futurenights[0]
if len(futurenights) > 1:
futurenights = futurenights[1:]
else:
futurenights = []
else:
gamenight = Gamenight(status='Probably',
date=Utils.saturday(),
lastupdate=Utils.now())
futurenights = []
updated = gamenight.lastupdate.strftime('%A, %B %d, %I:%M %p')
invites = Invitation.summary()
upcoming = {g.date: { 'type': 'gamenight',
'date': g.date,
'location': g.location }
for g in futurenights}
for week in range(0,5):
day = (Utils.saturday() + timedelta(week*7)).date()
# skip days we already have a confirmed GN
if upcoming.get(day, False):
continue
summary = invites.get(day, False)
if summary:
upcoming[day] = { 'type': 'invites',
'date': day,
'invitations': summary }
continue
upcoming[day] = { 'date': day }
template_values = {
'future': sorted(upcoming.values(), key=lambda x: x['date']),
'status': gamenight.status,
'updated': updated,
'calendar_url': urllib.quote_plus(config.get('calendar_id')),
}
if gamenight.date and gamenight.time:
template_values['when'] = datetime.combine(gamenight.date, gamenight.time).strftime('%A, %I:%M %p')
if gamenight.location:
template_values['where'] = gamenight.location
if gamenight.notes:
template_values['notes'] = gamenight.notes
if 'peeron' in config.get('calendar_id'):
tmpl_file = 'index-old.html'
else:
tmpl_file = 'index.html'
template = JINJA_ENVIRONMNT.get_template(tmpl_file)
# Write the submission form and the footer of the page
self.response.write(template.render(template_values))
# tasks
class NagTask(webapp2.RequestHandler):
def get(self):
# don't bother starting to nag before Tuesday 10am
today = datetime.today()
email = self.request.get('email', False)
priority = None
# sun-tue only check high priority
if today.weekday() in (6, 0, 1) and not email:
logging.debug('Only checking high priority invites.')
priority = 'Insist'
# saturday afternoon just give up and say no
if today.weekday() == 5 and today.hour > 16:
logging.debug('Giving up on scheduling this week.')
gn = Gamenight.schedule(status='No', date=today.date())
self.redirect('/')
return
status = self.request.get('status', None)
gn = Gamenight.schedule(status=status, priority=priority)
if gn and gn.status == 'Yes' or not email:
logging.debug('No need to nag.')
self.redirect('/')
return
logging.debug('Sending out email template: %s', email)
subjects = { 'first': 'Want to host gamenight?',
'second': 'Still looking to find a host for gamenight this week!' }
bodies = {'first': "Seems that no one has offered to host gamenight this week. " +
"Want to host? Go to http://%(url)s/invite!" ,
'second': "We still haven't had anyone volunteer to host gamenight this week. " +
"For now, the site will show '%(status)s', but if you'd like to host, " +
"please go to http://%(url)s/invite to have people come over. ",
}
footer = ("\nThanks!\n\n(You asked to get these emails if no one is hosting gamenight. " +
"If you want to stop getting these, go to http://%(url)s/profile and uncheck " +
"the 'nag emails' option.)")
message = mail.EmailMessage()
message.sender = 'Gamenight <%s>' % config.get('sender')
message.to = message.sender
message.subject = subjects[email]
message.body = (bodies[email] + footer) % { 'url': config.get('url', "TBD"), 'status': status }
message.bcc = [u.key.id() for u in User.query(User.nag==True).fetch()]
logging.info('Sending nag email to %r', message.to)
message.send()
self.redirect('/')
class ResetTask(webapp2.RequestHandler):
def get(self):
Gamenight.reset()
self.redirect('/')
class ScheduleTask(webapp2.RequestHandler):
def get(self):
Gamenight.schedule()
self.redirect('/')
debug = False
if 'peeron' in config.get('calendar_id'):
application = webapp2.WSGIApplication([
('/', MainPage),
('/apiauth', MainPage),
('/config', MainPage),
('/invite', MainPage),
('/profile', MainPage),
('/schedule', MainPage),
(decorator.callback_path, decorator.callback_handler()),
], debug=debug)
else:
application = webapp2.WSGIApplication([
('/', MainPage),
('/apiauth', ApiAuth),
('/config', ConfigPage),
('/invite', InvitePage),
('/profile', ProfilePage),
('/schedule', SchedulePage),
(decorator.callback_path, decorator.callback_handler()),
], debug=debug)
cron = webapp2.WSGIApplication([
('/tasks/nag', NagTask),
('/tasks/reset', ResetTask),
('/tasks/schedule', ScheduleTask),
], debug=debug)
# vim: set ts=4 sts=4 sw=4 et: