-
Notifications
You must be signed in to change notification settings - Fork 2
/
handlers.py
118 lines (96 loc) · 2.97 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
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
import logging
# Google App Engine imports
from google.appengine.ext import ndb
from models import Permission
# 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.debug(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):
flashes = self.session.get_flashes()
if flashes:
return flashes[0]
@property
def flashes(self):
return self.session.get_flashes()
@flash.setter
def flash(self, value):
self.session.add_flash(value)
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"""
djkey = dj.key
permissions = {
'djs': Permission.DJ_EDIT,
'programs': Permission.PROGRAM_EDIT,
'albums': Permission.ALBUM_EDIT,
'permissions': Permission.PERMISSION_EDIT,
'genres': Permission.GENRE_EDIT,
'blogs': Permission.BLOG_EDIT,
'events': Permission.EVENT_EDIT,}
permissions_dict = dict(('manage_%s'%key,
Permission.get_by_title(perm).has_dj(djkey)) for
(key, perm) in permissions.iteritems())
if not reduce(lambda x,y: x or y, permissions.values()):
permissions = None
self.session['dj'] = {
'key' : dj.key.urlsafe(),
'fullname' : dj.fullname,
'lowername' : dj.lowername,
'email' : dj.email,
'permissions' : permissions,
}
@property
def user(self):
return self.dj_key
@user.setter
def user(self, dj):
self.set_session_user(dj)
def set_session_program(self, pgm):
"""Takes a Program model, and stores values to the session"""
self.session['program'] = {
'key' : pgm.key.urlsafe(),
'slug' : pgm.slug,
'title' : pgm.title,
}
@property
def program(self):
return self.program_key
@program.setter
def program(self, pgm):
self.set_session_program(pgm)
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 ndb.Key(urlsafe=key)
return None
@property
def program_key(self):
key = self.session.get('program').get('key')
if key is not None:
return ndb.Key(urlsafe=key)
return None