forked from hchapman/WBOR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.py
73 lines (60 loc) · 1.86 KB
/
handlers.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
import logging
# Google App Engine imports
from google.appengine.ext import db
# Webapp2 imports
import webapp2
from webapp2_extras import sessions
class BaseHandler(webapp2.RequestHandler):
"""Enables session management"""
def dispatch(self):
self.session_store = sessions.get_store(request = self.request)
try:
webapp2.RequestHandler.dispatch(self)
finally:
logging.info(self.session)
self.session_store.save_sessions(self.response)
@webapp2.cached_property
def session(self):
return self.session_store.get_session()
@property
def flash(self):
return self.session.get_flashes()
class UserHandler(BaseHandler):
"""Handler facilitating a currently logged in user (i.e. a DJ) and their programs, etc.
"""
def set_session_user(self, dj):
"""Takes a Dj model, and stores values into the session"""
self.session['dj'] = {
'key' : str(dj.key()),
'fullname' : dj.fullname,
'lowername' : dj.lowername,
'email' : dj.email,
}
def set_session_program(self, pgm):
"""Takes a Program model, and stores values to the session"""
self.session['program'] = {
'key' : str(pgm.key()),
'slug' : pgm.slug,
'title' : pgm.title,
}
def session_logout(self):
"""Logs the dj out, deleting program and dj keys in the session"""
for key in ('dj', 'program'):
if key in self.session:
del self.session[key]
def session_has_login(self):
return self.session.has_key('dj')
def session_has_program(self):
return self.session.has_key('program')
@property
def dj_key(self):
key = self.session.get('dj').get('key')
if key is not None:
return db.Key(key)
return None
@property
def program_key(self):
key = self.session.get('program').get('key')
if key is not None:
return db.Key(key)
return None