-
Notifications
You must be signed in to change notification settings - Fork 0
/
MasterHandler.py
283 lines (207 loc) · 9.21 KB
/
MasterHandler.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
# -*- coding: utf-8 -*-
import os
from datetime import datetime
import dateutil.relativedelta
import jinja2
import logging
import webapp2
from google.appengine.api import users
from django.utils import simplejson as json
from DataModels import UserSettings, UserProfile, Psinque, Invitation
#-----------------------------------------------------------------------------
jinja_environment = jinja2.Environment(
loader = jinja2.FileSystemLoader(os.path.dirname(__file__))
)
# Custom filter for jinja2 to display date in a human-readable form
def humanizeDateTime(value):
timeDifference = dateutil.relativedelta.relativedelta(datetime.now(), value)
attrs = [u'years', u'months', u'days', u'hours', u'minutes', u'seconds']
human_readable = lambda delta: ['%d %s' % (getattr(delta, attr), getattr(delta, attr) > 1 and attr or attr[:-1]) for attr in attrs if getattr(delta, attr)]
readableDifference = human_readable(timeDifference)
if len(readableDifference) == 0:
return u"Just now"
readableDifference = readableDifference[0] + u" ago"
if u'seconds' in readableDifference:
return u"Less than a minute ago"
if u'minutes' in readableDifference and timeDifference.minutes < 10:
return u"Few minutes ago"
return readableDifference
jinja_environment.filters['humanizeddatetime'] = humanizeDateTime
#-----------------------------------------------------------------------------
class MenuEntry:
url = ""
title = ""
entryclass = ""
def __init__(self, url, title, entryclass = ""):
self.url = url
self.title = title
self.entryclass = entryclass
#-----------------------------------------------------------------------------
class AjaxError(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
#-----------------------------------------------------------------------------
class MasterHandler(webapp2.RequestHandler):
'''
The base class for all Psinque request handlers.
'''
def get(self, actionName):
self.user = users.get_current_user()
if (not "view" in actionName) and (not self.getUserProfile()):
#self.sendJsonError("User profile not found")
return
try:
actionFunction = getattr(self, actionName)
except AttributeError as e:
logging.error("Action method not found:")
logging.error(e)
self.error404()
return
try:
actionFunction()
except AjaxError as e:
logging.error("AjaxError:")
logging.error(e)
self.sendJsonError(e.value)
def getUserProfile(self):
'''
Retrieves the UserProfile for the current user from the Datastore.
If the profile does not exist, the user is redirected to the profile
edit page.
'''
try:
getattr(self, "userProfile")
return True
except AttributeError:
self.userProfile = UserProfile.all(keys_only = True).filter("user =", self.user).get()
if not self.userProfile:
self.redirect("/profile/view")
return False
self.userProfile = UserProfile.get(self.userProfile) # retrieve actual data from datastore
return True
def getRequiredParameter(self, parameterName):
'''
Retrieves a text parameter from the HTTP request and raises an
error if the parameter is not found.
'''
val = self.request.get(parameterName)
if val == "":
raise AjaxError("Parameter " + parameterName + " should not be empty.")
return val
def getRequiredBoolParameter(self, name):
'''
Retrieves a Boolean parameter from the HTTP request and raises an
error if the parameter is not found.
'''
try:
val = self.getRequiredParameter(name)
val = {"true": True, "false": False}[val]
return val
except KeyError:
raise AjaxError("Unknown Boolean value: " + val);
def sendJsonOK(self, additionalValues = {}):
self.response.headers['Content-Type'] = "application/json"
self.response.out.write(json.dumps(dict({"status": 0}.items() +
additionalValues.items())))
def sendJsonError(self, msg):
self.response.headers['Content-Type'] = "application/json"
self.response.out.write(json.dumps({"status": 1,
"message": msg}))
def sendContent(self, templateName,
activeEntry = "",
templateVariables = None):
if self.user:
try:
getattr(self, "userProfile")
except AttributeError:
if not self.getUserProfile():
return
if not self.userProfile.active:
email = self.userProfile.emails.get().itemValue
invitation = Invitation.all().filter("email =", email).get()
if not invitation is None:
invitation.status = "used"
invitation.put()
self.userProfile.active = True
self.userProfile.put()
else:
self.restrictedAccess()
return
notificationCount = Psinque.all(keys_only = True). \
filter("fromUser =", self.userProfile). \
filter("status =", "pending"). \
count()
menuentries = [
MenuEntry("profile/view", "Profile"),
MenuEntry("personas/view", "Personas"),
MenuEntry("psinques/view", "Psinques"),
MenuEntry("settings/view", "Settings"),
]
if activeEntry != "":
for entry in menuentries:
if entry.title == activeEntry:
entry.entryclass = "active" # mark menu item as active
if templateVariables:
allTemplateVariables = dict(templateVariables.items() +
self.getUserVariables().items() + {
'menuentries': menuentries,
'notificationCount': notificationCount
}.items())
else:
allTemplateVariables = dict(self.getUserVariables().items() +
{'menuentries': menuentries}.items())
else:
allTemplateVariables = templateVariables
template = jinja_environment.get_template(templateName)
self.response.out.write(template.render(allTemplateVariables))
def displayMessage(self, templateName, templateVariables = None):
template = jinja_environment.get_template(templateName)
self.response.out.write(template.render(templateVariables))
def restrictedAccess(self):
template = jinja_environment.get_template('templates/Message_restricted.html')
self.response.out.write(template.render())
def getUserVariables(self):
user = users.get_current_user()
if user:
return {
'username': user.nickname(),
'logouturl': users.create_logout_url(self.request.uri),
'settings': True
}
else:
raise Exception("User not logged in.") # this should never happen, because sendTopTemplate() redirects to /login earlier
#def getLanguage(self):
#query = UserSettings.all()
#userProfile = query.filter("user =", self.user).get()
#if not userProfile: # no user profile registered yet
#self.LANGUAGE_CODE = "en"
#else:
#self.LANGUAGE_CODE = userProfile.preferredLanguage
#logging.error("Changed language to " + settings.LANGUAGE_CODE)
def error404(self):
self.error(404)
template = jinja_environment.get_template('templates/notFound.html')
self.response.out.write(template.render(requestName = self.request.uri))
#-----------------------------------------------------------------------------
class StaticHandler(webapp2.RequestHandler):
'''
The base class for all static Psinque request handlers.
'''
def get(self, actionName):
try:
actionFunction = getattr(self, actionName)
except AttributeError as e:
logging.error("Action method not found:")
logging.error(e)
self.error404()
return
actionFunction()
def sendContent(self, templateName, templateVariables = {}):
template = jinja_environment.get_template(templateName)
self.response.out.write(template.render(templateVariables))
def error404(self):
self.error(404)
template = jinja_environment.get_template('templates/notFound.html')
self.response.out.write(template.render(requestName = self.request.uri))