forked from bgolub/blog
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
418 lines (357 loc) · 14 KB
/
main.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import functools
import os
import uuid
import urllib
from django.conf import settings
settings._target = None
os.environ["DJANGO_SETTINGS_MODULE"] = "settings"
from django import newforms as forms
from django.template.defaultfilters import slugify
from django.utils import feedgenerator
from django.utils import simplejson
from google.appengine.api import memcache
from google.appengine.api import urlfetch
from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.db import djangoforms
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
DOPPLR_TOKEN = getattr(settings, "DOPPLR_TOKEN", None)
MAPS_API_KEY = getattr(settings, "MAPS_API_KEY", None)
SHOW_CURRENT_CITY = getattr(settings, "SHOW_CURRENT_CITY", False)
TITLE = getattr(settings, "TITLE", "Blog")
OLD_WORDPRESS_BLOG = getattr(settings, "OLD_WORDPRESS_BLOG", None)
webapp.template.register_template_library("filters")
def admin(method):
@functools.wraps(method)
def wrapper(self, *args, **kwargs):
user = users.get_current_user()
if not user:
if self.request.method == "GET":
return self.redirect(users.create_login_url(self.request.uri))
return self.error(403)
elif not users.is_current_user_admin():
return self.error(403)
else:
return method(self, *args, **kwargs)
return wrapper
class Entry(db.Model):
author = db.UserProperty()
title = db.StringProperty(required=True)
slug = db.StringProperty(required=True)
body = db.TextProperty(required=True)
published = db.DateTimeProperty(auto_now_add=True)
updated = db.DateTimeProperty(auto_now=True)
tags = db.ListProperty(db.Category)
#podcast? maybe?
mp3url = db.LinkProperty()
mp3length = db.IntegerProperty()
class EntryForm(djangoforms.ModelForm):
class Meta:
model = Entry
exclude = ["author", "slug", "published", "updated", "tags", "mp3url", "mp3length"]
class BaseRequestHandler(webapp.RequestHandler):
def head(self):
pass
def _content_length(self, url):
"""Feeds enclosures need media length,
so we retrieve it here using json-head
appengine service by Simon.
http://json-head.appspot.com/?url="""
if url:
response = urlfetch.fetch("http://json-head.appspot.com/?url=" + url)
if response.status_code == 200:
data = simplejson.loads(response.content)
length = data["headers"]["Content-Length"]
try:
length = int(length)
except ValueError:
length = 0
return length
def raise_error(self, code):
self.error(code)
self.render("%i.html" % code)
def get_current_city(self):
key = "current_city/now"
current_city = memcache.get(key)
if not current_city:
response = urlfetch.fetch("https://www.dopplr.com/api/traveller_info?format=js&token=" + DOPPLR_TOKEN)
if response.status_code == 200:
data = simplejson.loads(response.content)
current_city = data["traveller"]["current_city"]
current_city["maps_api_key"] = MAPS_API_KEY
memcache.set(key, current_city, 60*60*5)
else:
current_city = None
return current_city
def get_recent_entries(self):
key = "entries/recent"
entries = memcache.get(key)
if not entries:
entries = db.Query(Entry).order("-published").fetch(limit=5)
memcache.set(key, list(entries))
return entries
def get_main_page_entries(self):
key = "entries/main"
entries = memcache.get(key)
if not entries:
entries = db.Query(Entry).order("-published").fetch(limit=10)
memcache.set(key, list(entries))
return entries
def get_archive_entries(self):
key = "entries/archive"
entries = memcache.get(key)
if not entries:
entries = db.Query(Entry).order("-published")
memcache.set(key, list(entries))
return entries
def get_entry_from_slug(self, slug):
key = "entry/%s" % slug
entry = memcache.get(key)
if not entry:
entry = db.Query(Entry).filter("slug =", slug).get()
if entry:
memcache.set(key, entry)
return entry
def get_tagged_entries(self, tag):
key = "entries/tag/%s" % tag
entries = memcache.get(key)
if not entries:
entries = db.Query(Entry).filter("tags =", tag).order("-published")
memcache.set(key, list(entries))
return entries
def kill_entries_cache(self, slug=None, tags=[]):
memcache.delete("entries/recent")
memcache.delete("entries/main")
memcache.delete("entries/archive")
if slug:
memcache.delete("entry/%s" % slug)
for tag in tags:
memcache.delete("entries/tag/%s" % tag)
def get_integer_argument(self, name, default):
try:
return int(self.request.get(name, default))
except ValueError:
return default
def render_feed(self, entries):
f = feedgenerator.Atom1Feed(
title=TITLE,
link="http://" + self.request.host + "/",
description=TITLE,
language="en",
)
for entry in entries[:10]:
if entry.mp3url:
enclosure = feedgenerator.Enclosure(unicode(entry.mp3url), unicode(entry.mp3length), u'audio/mpeg')
else:
enclosure = None
f.add_item(
title=entry.title,
link=self.entry_link(entry),
description=entry.body,
author_name=entry.author.nickname(),
pubdate=entry.published,
categories=entry.tags,
enclosure=enclosure,
)
data = f.writeString("utf-8")
self.response.headers["Content-Type"] = "application/atom+xml"
self.response.out.write(data)
def render_json(self, entries):
json_entries = [{
"title": entry.title,
"slug": entry.slug,
"body": entry.body,
"author": entry.author.nickname(),
"published": entry.published.isoformat(),
"updated": entry.updated.isoformat(),
"tags": entry.tags,
"link": self.entry_link(entry, permalink=True),
} for entry in entries]
json = {"entries": json_entries}
self.response.headers["Content-Type"] = "text/javascript"
self.response.out.write(simplejson.dumps(json))
def render(self, template_file, extra_context={}):
if "entries" in extra_context:
format = self.request.get("format", None)
if format == "atom":
return self.render_feed(extra_context["entries"])
elif format == "json":
return self.render_json(extra_context["entries"])
extra_context["request"] = self.request
extra_context["admin"] = users.is_current_user_admin()
extra_context["recent_entries"] = self.get_recent_entries()
if SHOW_CURRENT_CITY:
extra_context["current_city"] = self.get_current_city()
extra_context.update(settings._target.__dict__)
template_file = "templates/%s" % template_file
path = os.path.join(os.path.dirname(__file__), template_file)
self.response.out.write(template.render(path, extra_context))
def ping(self, entry=None):
args = urllib.urlencode({
"name": TITLE,
"url": "http://" + self.request.host + "/",
"changesURL": "http://" + self.request.host + "/?format=atom",
})
response = urlfetch.fetch("http://blogsearch.google.com/ping?" + args)
return response.status_code
def is_valid_xhtml(self, entry):
args = urllib.urlencode({
"uri": self.entry_link(entry, permalink=True),
})
response = urlfetch.fetch("http://validator.w3.org/check?" + args,
method=urlfetch.HEAD)
return response.headers["X-W3C-Validator-Status"] == "Valid"
def entry_link(self, entry, query_args={}, permalink=False):
url = "/e/" + entry.slug
if permalink:
url = "http://" + self.request.host + url
if query_args:
url += "?" + urllib.urlencode(query_args)
return url
class ArchivePageHandler(BaseRequestHandler):
def get(self):
extra_context = {
"entries": self.get_archive_entries(),
}
self.render("archive.html", extra_context)
class DeleteEntryHandler(BaseRequestHandler):
@admin
def post(self):
key = self.request.get("key")
try:
entry = db.get(key)
entry.delete()
self.kill_entries_cache(slug=entry.slug, tags=entry.tags)
data = {"success": True}
except db.BadKeyError:
data = {"success": False}
json = simplejson.dumps(data)
self.response.out.write(json)
class EntryPageHandler(BaseRequestHandler):
def head(self, slug):
entry = self.get_entry_from_slug(slug=slug)
if not entry:
self.error(404)
def get(self, slug):
entry = self.get_entry_from_slug(slug=slug)
if not entry:
return self.raise_error(404)
extra_context = {
"entries": [entry], # So we can use the same template for everything
"entry": entry, # To easily pull out the title
"previous": db.Query(Entry).filter("published <", entry.published).order("-published").get(),
"next": db.Query(Entry).filter("published >", entry.published).order("published").get(),
"invalid": self.request.get("invalid", False),
}
self.render("entry.html", extra_context)
class FeedRedirectHandler(BaseRequestHandler):
def get(self):
self.redirect("/?format=atom", permanent=True)
class MainPageHandler(BaseRequestHandler):
def get(self):
offset = self.get_integer_argument("start", 0)
if not offset:
entries = self.get_main_page_entries()
else:
entries = db.Query(Entry).order("-published").fetch(limit=10, offset=offset)
if not entries and offset > 0:
return self.redirect("/")
extra_context = {
"entries": entries,
"next": max(offset - 10, 0),
"previous": offset + 10 if len(entries) == 10 else None,
"offset": offset,
}
self.render("main.html", extra_context)
class NewEntryHandler(BaseRequestHandler):
def get_tags_argument(self, name):
tags = [slugify(tag) for tag in self.request.get(name, "").split(",")]
tags = set([tag for tag in tags if tag])
return [db.Category(tag) for tag in tags]
@admin
def get(self, key=None):
extra_context = {}
form = EntryForm()
if key:
try:
entry = db.get(key)
extra_context["tags"] = ", ".join(entry.tags)
extra_context["mp3url"] = entry.mp3url
form = EntryForm(instance=entry)
except db.BadKeyError:
return self.redirect("/new")
extra_context["form"] = form
self.render("new.html", extra_context)
@admin
def post(self, key=None):
form = EntryForm(data=self.request.POST)
if form.is_valid():
if key:
try:
entry = db.get(key)
except db.BadKeyError:
return self.raise_error(404)
entry.title = self.request.get("title")
entry.body = self.request.get("body")
else:
slug = slugify(self.request.get("title"))
if self.get_entry_from_slug(slug=slug):
slug += "-" + uuid.uuid4().hex[:4]
entry = Entry(
author=users.get_current_user(),
body=self.request.get("body"),
title=self.request.get("title"),
slug=slug,
)
if self.request.get("mp3url"):
entry.mp3url = self.request.get("mp3url")
entry.mp3length = self._content_length(entry.mp3url)
entry.tags = self.get_tags_argument("tags")
entry.put()
self.kill_entries_cache(slug=entry.slug if key else None,
tags=entry.tags)
if not key:
self.ping(entry)
valid = self.is_valid_xhtml(entry)
return self.redirect(self.entry_link(entry,
query_args={"invalid": 1} if not valid else {}))
extra_context = {
"form": form,
}
self.render("new.html", extra_context)
class NotFoundHandler(BaseRequestHandler):
def head(self):
self.error(404)
def get(self):
self.raise_error(404)
class OldBlogRedirectHandler(BaseRequestHandler):
def get(self, year, month, day, slug):
if not OLD_WORDPRESS_BLOG:
return self.raise_error(404)
self.redirect("http://%s/%s/%s/%s/%s/" %
(OLD_WORDPRESS_BLOG, year, month, day, slug), permanent=True)
class TagPageHandler(BaseRequestHandler):
def get(self, tag):
extra_context = {
"entries": self.get_tagged_entries(tag),
"tag": tag,
}
self.render("tag.html", extra_context)
application = webapp.WSGIApplication([
("/", MainPageHandler),
("/archive/?", ArchivePageHandler),
("/delete/?", DeleteEntryHandler),
("/edit/([\w-]+)/?", NewEntryHandler),
("/e/([\w-]+)/?", EntryPageHandler),
("/new/?", NewEntryHandler),
("/t/([\w-]+)/?", TagPageHandler),
("/(\d+)/(\d+)/(\d+)/([\w-]+)/?", OldBlogRedirectHandler),
("/feed/?", FeedRedirectHandler),
("/.*", NotFoundHandler),
], debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()