Skip to content

Commit d58c4b3

Browse files
author
Benjamin Golub
committed
pagination with cursors, 80 column cleanup
1 parent c08795c commit d58c4b3

3 files changed

Lines changed: 29 additions & 7 deletions

File tree

‎blog.py‎

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,14 @@ def head(self, *args):
157157

158158
class HomeHandler(BaseHandler):
159159
def get(self):
160-
entries = db.Query(Entry).filter("hidden =", False).order("-published").fetch(limit=5)
161-
self.recent_entries = entries
162-
self.render("home.html", entries=entries)
160+
q = db.Query(Entry).filter("hidden =", False).order("-published")
161+
cursor = self.get_argument("cursor", None)
162+
if cursor:
163+
q.with_cursor(cursor)
164+
entries = q.fetch(limit=5)
165+
if not cursor:
166+
self.recent_entries = entries
167+
self.render("home.html", entries=entries, cursor=q.cursor())
163168

164169

165170
class AboutHandler(BaseHandler):
@@ -278,8 +283,9 @@ def head(self, slug):
278283
class TagHandler(BaseHandler):
279284
@tornado.web.removeslash
280285
def get(self, tag):
281-
entries = db.Query(Entry).filter("hidden =", False).filter("tags =", tag).order("-published")
282-
self.render("tag.html", entries=entries, tag=tag)
286+
q = db.Query(Entry).filter("hidden =", False).filter("tags =", tag)
287+
q.order("-published")
288+
self.render("tag.html", entries=q, tag=tag)
283289

284290

285291
class CatchAllHandler(BaseHandler):
@@ -348,11 +354,22 @@ def render(self, entry, show_date=False):
348354

349355
class RecentEntriesModule(tornado.web.UIModule):
350356
def render(self):
351-
entries = getattr(self.handler, "recent_entries",
352-
db.Query(Entry).filter("hidden =", False).order("-published").fetch(limit=5))
357+
entries = getattr(self.handler, "recent_entries", None)
358+
if not entries:
359+
q = db.Query(Entry).filter("hidden =", False).order("-published")
360+
entries = q.fetch(limit=5)
353361
return self.render_string("modules/recententries.html", entries=entries)
354362

355363

364+
class NavigationModule(tornado.web.UIModule):
365+
def render(self, cursor):
366+
kwargs = {
367+
"cursor": cursor,
368+
}
369+
previous = self.request.path + "?" + urllib.urlencode(kwargs)
370+
return self.render_string("modules/navigation.html", previous=previous)
371+
372+
356373
settings = {
357374
"blog_author": "Benjamin Golub",
358375
"blog_title": "Benjamin Golub",
@@ -364,6 +381,7 @@ def render(self):
364381
"EntrySmall": EntrySmallModule,
365382
"MediaRSS": MediaRSSModule,
366383
"RecentEntries": RecentEntriesModule,
384+
"Navigation": NavigationModule,
367385
},
368386
"xsrf_cookies": True,
369387
}

‎templates/home.html‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@
44
{% for entry in entries %}
55
{{ modules.Entry(entry, show_share=True) }}
66
{% end %}
7+
{% if entries and cursor %}
8+
{{ modules.Navigation(cursor) }}
9+
{% end %}
710
{% end %}

‎templates/modules/navigation.html‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<a href="{{ previous }}">{{ _("Previous") }}</a>

0 commit comments

Comments
 (0)