-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
executable file
·301 lines (266 loc) · 10.9 KB
/
views.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
#!/usr/bin/python2.5
#
# Copyright 2012 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Snipper is a Google snippet management tool."""
__author__ = 'erichiggins@gmail.com (Eric Higgins)'
import calendar
import datetime
import logging
import os
import urllib
import json # pylint: disable-msg=C6204
os.environ['DJANGO_SETTINGS_MODULE'] = 'appengine_config'
from google.appengine import dist # pylint: disable-msg=C6204
dist.use_library('django', '1.1')
from django.utils import simplejson # pylint: disable-msg=C6204
from google.appengine.api import memcache
from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext import ereporter
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp import util as webapp_util
import models
import pytz
import util
ereporter.register_logger()
class PreferencesHandler(webapp.RequestHandler):
"""Handle the user preferences page."""
@webapp_util.login_required
# pylint: disable-msg=C6409
def get(self):
"""Render the preferences page."""
user = users.get_current_user()
snipper_user = models.GetSnippetUser(user)
errors = self.request.get('errors', '')
logging.info(errors)
errors = filter(None, errors.split(','))
logging.info(errors)
template_values = {
'gaia': util.GetGaiaData(user),
'user': user,
'errors': errors,
'snipper_user': snipper_user,
'reset_days': calendar.day_name,
'reset_hours': ([(0, '12 am')] +
[(x, '%s am' % x) for x in xrange(1, 12)] +
[(12, '12 pm')] +
[(x, '%s pm' % (x - 12)) for x in xrange(13, 24)]),
}
path = os.path.join(os.path.dirname(__file__), 'templates/preferences.html')
rendered_page = template.render(path, template_values)
self.response.headers['Expires'] = util.GetExpiryHeader(minutes=0)
self.response.headers['Cache-Control'] = 'private, max-age=0'
self.response.out.write(rendered_page)
# pylint: disable-msg=C6409
def post(self):
"""Save the user's preferences."""
user = users.get_current_user()
snipper_user = models.GetSnippetUser(user)
logging.debug('Saving settings for %s', user)
errors = []
date_format = str(self.request.get('date_format', snipper_user.date_format))
snippet_format = self.request.get('snippet_format',
snipper_user.snippet_format)
snipper_user.mail_snippets = bool(self.request.get('mail_snippets', False))
snipper_user.send_confirm = bool(self.request.get('send_confirm', False))
snipper_user.reset_day = int(self.request.get('reset_day',
snipper_user.reset_day))
snipper_user.reset_hour = int(self.request.get('reset_hour',
snipper_user.reset_hour))
timezone = self.request.get('timezone')
try:
assert pytz.timezone(timezone)
except pytz.UnknownTimeZoneError:
logging.exception('Invalid timezone: %s', timezone)
errors.append('Invalid timezone: %s.' % timezone)
else:
snipper_user.timezone = timezone
# Convert to UTC for storage.
utc_reset = util.ResetDatetimeToUtc(snipper_user.reset_day,
snipper_user.reset_hour,
snipper_user.timezone)
snipper_user.utc_reset_day = utc_reset.weekday()
snipper_user.utc_reset_hour = utc_reset.hour
try:
assert datetime.datetime.now().strftime(date_format)
except (ValueError, TypeError):
errors.append('Invalid date format "%s".' % date_format)
logging.exception('date_format "%s" failed validation.', date_format)
else:
snipper_user.date_format = date_format
try:
assert snippet_format % 'test snippet'
except (ValueError, TypeError):
errors.append('Invalid snippet format "%s".' % snippet_format)
logging.exception('snippet_format "%s" is invalid.', snippet_format)
else:
snipper_user.snippet_format = snippet_format
logging.debug('date:%s, snip:%s, mail:%s, conf:%s, day:%s, hour:%s, tz:%s'
'utc_day:%s, utc_hour:%s',
snipper_user.date_format,
snipper_user.snippet_format,
snipper_user.mail_snippets,
snipper_user.send_confirm,
snipper_user.reset_day,
snipper_user.reset_hour,
snipper_user.timezone,
snipper_user.utc_reset_day,
snipper_user.utc_reset_hour)
try:
db.put(snipper_user)
except (db.Timeout, db.InternalError):
logging.exception('Could not save settings.')
errors.append('Could not save settings.')
else:
memcache_key = 'SnippetUser-' + str(user)
memcache.set(memcache_key, snipper_user)
if errors:
errors = urllib.quote_plus(','.join(errors))
return self.redirect('/settings?errors=' + errors)
# Drop the last two weeks from memcache.
memcache.delete_multi(['snippets_%s_%d' % (str(user), x) for x in (0, 1)])
self.redirect('/?msg=Settings+saved.')
class AddSnippet(webapp.RequestHandler):
"""Add new snippets to datastore."""
def post(self): # pylint: disable-msg=C6409
"""Add a snippet to the datastore for a user. Print 1 on success."""
self.response.headers['Content-Type'] = 'application/json'
user = users.get_current_user()
snip = self.request.get('s', '')
version = self.request.get('v', '')
if not user:
logging.debug('Could not get user information, bailing out. '
'Version: %s Snippet: %s', version, snip)
self.response.out.write('0')
return
# Make sure the user has been added.
assert models.GetSnippetUser(user)
if not snip:
logging.debug('Empty snippet.')
self.response.out.write('0')
return
result = models.SaveSnippet(user, version, snip)
self.response.out.write(int(result[0]))
class ViewSnippets(webapp.RequestHandler):
"""Return a users snippets."""
@webapp_util.login_required
def get(self): # pylint: disable-msg=C6409
"""Print a users snippets."""
try:
offset = int(self.request.get('offset', default_value=0))
except ValueError:
offset = 0
results = models.FetchSnippets(offset=offset)
self.response.headers['Content-Type'] = 'application/json'
snip = ''.join(s.Snippet for s in results)
self.response.out.write(snip + '\n')
class JsonHandler(webapp.RequestHandler):
"""Handle JSON requests for the web front end."""
@webapp_util.login_required
def get(self): # pylint: disable-msg=C6409
"""Return a users snippets for the requested timeframe in JSON format."""
snipper_user = models.GetSnippetUser(users.get_current_user())
tz = pytz.timezone(snipper_user.timezone)
try:
offset = int(self.request.get('offset', default_value=0))
except ValueError:
offset = 0
results = models.FetchSnippets(offset=offset)
self.response.headers['Content-Type'] = 'application/json'
start_date = util.GetLastWeekDay(snipper_user.reset_day,
offset,
snipper_user.reset_hour,
tz)
end_date = util.GetNextWeekDay(snipper_user.reset_day,
offset,
snipper_user.reset_hour,
tz)
ret = {
'snippets': [{'key': str(s.key()), 'text': s.Snippet} for s in results],
'dates': {
'from': start_date.strftime('%b %d'),
'to': end_date.strftime('%b %d')
}
}
self.response.out.write(simplejson.dumps(ret))
class StaticHandler(webapp.RequestHandler):
"""Render Django templates as static HTML.."""
def get(self, filename): # pylint: disable-msg=C6409
user = users.get_current_user()
template_values = {'gaia': util.GetGaiaData(user)}
path = os.path.join(os.path.dirname(__file__), 'templates/', filename)
rendered_page = template.render(path, template_values)
self.response.headers['Expires'] = util.GetExpiryHeader()
self.response.headers['Cache-Control'] = 'public, max-age=600'
self.response.out.write(rendered_page)
class ErrorHandler(webapp.RequestHandler):
"""Error handler."""
def get(self): # pylint: disable-msg=C6409
logging.error(self.request)
return self.error(404)
class MainHandler(webapp.RequestHandler):
"""Main handler for Snipper."""
@webapp_util.login_required
def get(self): # pylint: disable-msg=C6409
"""Render the Snipper page."""
user = users.get_current_user()
snipper_user = models.GetSnippetUser(user)
tz = pytz.timezone(snipper_user.timezone)
try:
offset = int(self.request.get('offset', default_value=0))
except ValueError:
offset = 0
results = models.FetchSnippets(offset=offset)
snippets = [s.Snippet for s in results]
template_values = {
'gaia': util.GetGaiaData(user),
'user': user,
'reset_day': calendar.day_name[snipper_user.reset_day],
'start': util.GetLastWeekDay(snipper_user.reset_day,
hour=snipper_user.reset_hour,
tz=tz),
'end': util.GetNextWeekDay(snipper_user.reset_day,
hour=snipper_user.reset_hour,
tz=tz),
'snippets': snippets,
'offset': offset,
'older': offset + 1,
'msg': self.request.get('msg'),
}
if offset:
template_values['newer'] = offset - 1
else:
template_values['newer'] = 0
path = os.path.join(os.path.dirname(__file__), 'templates/index.html')
rendered_page = template.render(path, template_values)
self.response.headers['Expires'] = util.GetExpiryHeader(minutes=1)
self.response.headers['Cache-Control'] = 'private, max-age=60'
self.response.out.write(rendered_page)
application = webapp.WSGIApplication(
[('/', MainHandler),
('/index.html', MainHandler),
('/view', ViewSnippets),
('/json', JsonHandler),
('/add', AddSnippet),
(r'^/([a-zA-Z\d][\w\-]+\.html)$', StaticHandler),
('/_wave/.*', ErrorHandler),
('/settings', PreferencesHandler),
],
debug=True)
def main():
webapp_util.run_wsgi_app(application)
if __name__ == '__main__':
main()