Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

pagination with cursors, 80 column cleanup

commit d58c4b3fc1686c28732e04bd153c2859ffdff39f 1 parent c08795c
Benjamin Golub authored
View
32 blog.py
@@ -157,9 +157,14 @@ def head(self, *args):
class HomeHandler(BaseHandler):
def get(self):
- entries = db.Query(Entry).filter("hidden =", False).order("-published").fetch(limit=5)
- self.recent_entries = entries
- self.render("home.html", entries=entries)
+ q = db.Query(Entry).filter("hidden =", False).order("-published")
+ cursor = self.get_argument("cursor", None)
+ if cursor:
+ q.with_cursor(cursor)
+ entries = q.fetch(limit=5)
+ if not cursor:
+ self.recent_entries = entries
+ self.render("home.html", entries=entries, cursor=q.cursor())
class AboutHandler(BaseHandler):
@@ -278,8 +283,9 @@ def head(self, slug):
class TagHandler(BaseHandler):
@tornado.web.removeslash
def get(self, tag):
- entries = db.Query(Entry).filter("hidden =", False).filter("tags =", tag).order("-published")
- self.render("tag.html", entries=entries, tag=tag)
+ q = db.Query(Entry).filter("hidden =", False).filter("tags =", tag)
+ q.order("-published")
+ self.render("tag.html", entries=q, tag=tag)
class CatchAllHandler(BaseHandler):
@@ -348,11 +354,22 @@ def render(self, entry, show_date=False):
class RecentEntriesModule(tornado.web.UIModule):
def render(self):
- entries = getattr(self.handler, "recent_entries",
- db.Query(Entry).filter("hidden =", False).order("-published").fetch(limit=5))
+ entries = getattr(self.handler, "recent_entries", None)
+ if not entries:
+ q = db.Query(Entry).filter("hidden =", False).order("-published")
+ entries = q.fetch(limit=5)
return self.render_string("modules/recententries.html", entries=entries)
+class NavigationModule(tornado.web.UIModule):
+ def render(self, cursor):
+ kwargs = {
+ "cursor": cursor,
+ }
+ previous = self.request.path + "?" + urllib.urlencode(kwargs)
+ return self.render_string("modules/navigation.html", previous=previous)
+
+
settings = {
"blog_author": "Benjamin Golub",
"blog_title": "Benjamin Golub",
@@ -364,6 +381,7 @@ def render(self):
"EntrySmall": EntrySmallModule,
"MediaRSS": MediaRSSModule,
"RecentEntries": RecentEntriesModule,
+ "Navigation": NavigationModule,
},
"xsrf_cookies": True,
}
View
3  templates/home.html
@@ -4,4 +4,7 @@
{% for entry in entries %}
{{ modules.Entry(entry, show_share=True) }}
{% end %}
+ {% if entries and cursor %}
+ {{ modules.Navigation(cursor) }}
+ {% end %}
{% end %}
View
1  templates/modules/navigation.html
@@ -0,0 +1 @@
+<a href="{{ previous }}">{{ _("Previous") }}</a>
Please sign in to comment.
Something went wrong with that request. Please try again.