Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
pagination with cursors, 80 column cleanup
  • Loading branch information
Benjamin Golub committed Aug 24, 2010
1 parent c08795c commit d58c4b3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
32 changes: 25 additions & 7 deletions blog.py
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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",
Expand All @@ -364,6 +381,7 @@ def render(self):
"EntrySmall": EntrySmallModule,
"MediaRSS": MediaRSSModule,
"RecentEntries": RecentEntriesModule,
"Navigation": NavigationModule,
},
"xsrf_cookies": True,
}
Expand Down
3 changes: 3 additions & 0 deletions templates/home.html
Expand Up @@ -4,4 +4,7 @@
{% for entry in entries %}
{{ modules.Entry(entry, show_share=True) }}
{% end %}
{% if entries and cursor %}
{{ modules.Navigation(cursor) }}
{% end %}
{% end %}
1 change: 1 addition & 0 deletions templates/modules/navigation.html
@@ -0,0 +1 @@
<a href="{{ previous }}">{{ _("Previous") }}</a>

0 comments on commit d58c4b3

Please sign in to comment.