Permalink
Browse files

pagination with cursors, 80 column cleanup

  • Loading branch information...
Benjamin Golub
Benjamin Golub committed Aug 24, 2010
1 parent c08795c commit d58c4b3fc1686c28732e04bd153c2859ffdff39f
Showing with 29 additions and 7 deletions.
  1. +25 −7 blog.py
  2. +3 −0 templates/home.html
  3. +1 −0 templates/modules/navigation.html
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
@@ -4,4 +4,7 @@
{% for entry in entries %}
{{ modules.Entry(entry, show_share=True) }}
{% end %}
{% if entries and cursor %}
{{ modules.Navigation(cursor) }}
{% end %}
{% end %}
@@ -0,0 +1 @@
<a href="{{ previous }}">{{ _("Previous") }}</a>

0 comments on commit d58c4b3

Please sign in to comment.