/
handlersold.py
334 lines (286 loc) · 10.2 KB
/
handlersold.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
import logging
import random
import sys
import datetime
import os
import urllib2
import re
from google.appengine.ext import webapp
from google.appengine.ext import db
import modelold
import libs.gettheme.gettheme as themecontroller
import utils
import libs.pb64.converter as pb64
import libs.safehtml.parser as safehtml
class GetRandomCounterPage(webapp.RequestHandler):
"""
Called when clicking on the icon, to get a random counter page
"""
def get(self):
# Get a random entry in the URLs store
total = utils.getNbOfTotalUrls() - 1
nb = random.randint(1, total)
self.redirect("/" + pb64.encodeB64Padless(int(nb)))
class AccessViaShortUrl(webapp.RequestHandler):
"""
Legacy short URL ... just forwarding to the corresponding base64padless
"""
def get(self, nb):
logging.info(">>> " + self.request.url + " >>>AccessViaShortUrl")
self.redirect("/" + pb64.encodeB64Padless(int(nb)))
class NewAccessViaB64ShortUrl(webapp.RequestHandler):
def get(self, nb):
logging.info(">>> " + self.request.url + " >>>NewAccessViaB64ShortUrl")
nb = pb64.decodeB64Padless(nb)
if nb:
# Get the full url corresponding to count
q = db.GqlQuery("SELECT * FROM URLs WHERE counter = :1", nb)
urls = q.fetch(1)
if len(urls) == 1:
matches = re.findall(utils.regexp["parseFullUrlWithSeconds"], urls[0].url)
if len(matches) == 0:
matches = re.findall(utils.regexp["parseFullUrl"], urls[0].url)
try:
target = matches[0][0]
options = matches[0][1] or ""
title = matches[0][2] or ""
subHandler = CountDownDMYHMS()
subHandler.request = self.request
subHandler.response = self.response
subHandler.get(target, options, title)
except:
#print sys.exc_info()
# TODO: need error management for the user here
self.redirect("/")
else:
#print sys.exc_info()
# TODO: need error management for the user here
self.redirect("/")
else:
# TODO: need error management for the user here
self.redirect("/")
class AccessViaB64ShortUrl(webapp.RequestHandler):
"""
New short URL access. URLs are like http://<domain>/<base64padless>
Will call the CountDownDMYHMS handler which is the true main entry point
"""
def get(self, nb):
logging.info(">>> " + self.request.url + " >>>AccessViaB64ShortUrl")
nb = pb64.decodeB64Padless(nb)
if nb:
# Get the full url corresponding to count
q = db.GqlQuery("SELECT * FROM URLs WHERE counter = :1", nb)
urls = q.fetch(1)
if len(urls) == 1:
matches = re.findall(utils.regexp["parseFullUrlWithSeconds"], urls[0].url)
if len(matches) == 0:
matches = re.findall(utils.regexp["parseFullUrl"], urls[0].url)
try:
target = matches[0][0]
options = matches[0][1] or ""
title = matches[0][2] or ""
subHandler = CountDownDMYHMS()
subHandler.request = self.request
subHandler.response = self.response
subHandler.get(target, options, title)
except:
#print sys.exc_info()
# TODO: need error management for the user here
self.redirect("/")
else:
#print sys.exc_info()
# TODO: need error management for the user here
self.redirect("/")
else:
# TODO: need error management for the user here
self.redirect("/")
class CountDownDMYHM(webapp.RequestHandler):
"""
Legacy full URL entry point, with no seconds
Just adds 00 at the end of the URL and forwards to the CountDownDMYHMS handler
"""
def get(self, target, options, title):
logging.info(">>> " + self.request.url + " >>>CountDownDMYHM")
if options == None:
options = ""
if title == None:
title = ""
else:
title = "/" + title
self.redirect("/" + target + "00" + options + title)
class AccessViaRelativeUrl(webapp.RequestHandler):
"Relative counter entry point, starts a counter from now, for a number of seconds"
def get(self, numberOfSeconds, optionsStr, title):
logging.info(">>> " + self.request.url + " >>>AccessViaRelativeUrl")
options = themecontroller.getThemeOptions(optionsStr)
if title:
title = title.strip()
if title != '':
title = urllib2.unquote(title.strip())
else:
title = ""
urlToShare = self.request.url
data = {
"dateObject" : {
"relativeDuration": numberOfSeconds
},
"title": title,
"options": options,
"form": {},
"relative": True,
"shortUrl": urlToShare,
"listOfThemes": themecontroller.getListOfThemes()
}
data["latestUrls"] = utils.getLatestUrls()
data["popularUrls"] = utils.getMostPopulars()
formData = utils.prepareFormData()
data["form"].update(formData)
pageContent = utils.getTemplateContent(os.path.join(os.path.dirname(__file__), "templates/main.html"), data)
self.response.out.write(pageContent)
class CountDownDMYHMS(webapp.RequestHandler):
"""
Real entry point to the site, takes in a full URL like http://<domain>/<ddmmyyyyhhmmss><options>/<title>
Responsibility is to display the corresponding countdown and increment usage counter for URL
"""
def get(self, target, optionsStr, title):
logging.info(">>> " + self.request.url + " >>>CountDownDMYHMS")
# Expected format is DDMMYYYYHHMMSS (checked via main.py by a regexp anyway)
day = target[:2]
month = target[2:4]
year = target[4:8]
hour = target[8:10]
minute = target[10:12]
second = target[12:14]
if utils.isValidDate(day, month, year, hour, minute, second):
storedUrl = utils.storeUrl(self.request.url, target, optionsStr or "", title or "", None, None, None)
ctheme = None
isTemplatizedCTheme = False
options = {}
if storedUrl.customTheme:
ctheme = storedUrl.customTheme.html
isTemplatizedCTheme = storedUrl.customTheme.isTemplate
else:
options = themecontroller.getThemeOptions(optionsStr)
jsdate = month + "/" + day + "/" + year + " " + hour + ":" + minute + ":" + second
if title:
title = title.strip()
if title != '':
title = urllib2.unquote(title.strip())
else:
title = ""
urlToShare = utils.getContextRootFromUrl(self.request.url) + "/" + pb64.encodeB64Padless(storedUrl.counter)
data = {
"dateObject" : {
"day": day, "month": month, "year": year, "hour": hour, "minute": minute
},
"jsdate": jsdate,
"title": title,
"options": options,
"customTheme": ctheme,
"isTemplateTheme": isTemplatizedCTheme,
"form": {},
"up": storedUrl.up,
"relative": storedUrl.relative,
"shortUrl": urlToShare,
"listOfThemes": themecontroller.getListOfThemes()
}
data["latestUrls"] = utils.getLatestUrls()
data["popularUrls"] = utils.getMostPopulars()
formData = utils.prepareFormData()
data["form"].update(formData)
pageContent = utils.getTemplateContent(os.path.join(os.path.dirname(__file__), "templates/main.html"), data)
self.response.out.write(pageContent)
else:
# Error, the date has a valid format, but is not a real date
self.redirect("/")
class CreateCountdown(webapp.RequestHandler):
"""
Called when a new countdown URL is created
In fact simply needs to redirect to the encoded b64padless version
TODO: When managing custom themes, will also need to take them into account here
"""
def post(self):
self.doCreate()
def get(self):
self.doCreate()
def doCreate(self):
target = self.request.get("datestamp")
title = self.request.get("title")
up = self.request.get("up")
relative = self.request.get("relative")
ctheme = None
options = None
# Handling custom themes or normal themes
customTheme = self.request.get("customTheme")
if customTheme:
parser = safehtml.SafeHtmlParser()
safe = parser.getSafeHtml(customTheme)
ctheme = modelold.CustomTheme(html=safe, isTemplate=False)
ctheme.put()
else:
options = self.request.get("music") + self.request.get("theme")
if relative:
self.redirect("/" + target + options + "/" + title)
else:
storedUrl = utils.storeUrl(self.request.url, target, options, title, ctheme, up, relative)
self.redirect("/" + pb64.encodeB64Padless(storedUrl.counter))
class CreateCustomThemedCountdown(webapp.RequestHandler):
"""
Called when a user wants to start creating a countdown with an advanced custom theme
"""
def get(self):
pageContent = utils.getTemplateContent(os.path.join(os.path.dirname(__file__), "templates/customThemePlayGround.html"), {"expertMode": True})
self.response.out.write(pageContent)
class CreateSimpleCustomThemedCountdown(webapp.RequestHandler):
"""
Called when a user wants to start creating a countdown with simple custom theme
"""
def get(self):
pageContent = utils.getTemplateContent(os.path.join(os.path.dirname(__file__), "templates/customThemePlayGround.html"), {"simpleCustomMode": True})
self.response.out.write(pageContent)
class GetSafeHtml(webapp.RequestHandler):
"""
Called via XHR to tame the html during custom theme preview action
"""
def post(self):
parser = safehtml.SafeHtmlParser()
safe = parser.getSafeHtml(self.request.get("html"))
self.response.out.write(safe)
class Index(webapp.RequestHandler):
"""
Fallback handler in case no URL pattern matched.
Shows a fake timer to urge users to create their own
"""
def get(self):
delta = datetime.datetime.today() + datetime.timedelta(hours = 1)
day = str(delta.day)
if len(day) == 1: day = "0" + day
month = str(delta.month)
if len(month) == 1: month = "0" + month
year = str(delta.year)
hour = str(delta.hour)
if len(hour) == 1: hour = "0" + hour
minute = str(delta.minute)
if len(minute) == 1: minute = "0" + minute
second = str(delta.second)
if len(second) == 1: second = "0" + second
data = {
"dateObject" : {
"day": day, "month": month, "year": year, "hour": hour, "minute": minute
},
"jsdate": month + "/" + day + "/" + year + " " + hour + ":" + minute + ":" + second,
"title": "w.aitfor.it",
"options": themecontroller.getThemeOptions("waitforit"),
"form": {},
"up": True,
"relative": False,
"shortUrl": self.request.url,
"listOfThemes": themecontroller.getListOfThemes(),
"nbOfTotalUrls": utils.getNbOfTotalUrls()
}
data["latestUrls"] = utils.getLatestUrls()
data["popularUrls"] = utils.getMostPopulars()
formData = utils.prepareFormData()
data["form"].update(formData)
pageContent = utils.getTemplateContent(os.path.join(os.path.dirname(__file__), "templates/main.html"), data)
self.response.out.write(pageContent)