Skip to content

Commit

Permalink
Added the chronological navigation to the post page.
Browse files Browse the repository at this point in the history
While following Nick's post series I felt the need for easy navigation from
current post to previous and next post in the series, thus avoiding going
back to the index page again and again.

Also it is helpful for somebody coming in from a search engine to read the
posts in order, or for archive pages.

This branch can be safely merged with the delete_handler branch and is kept
separate just as a choice to the user. The delete takes care of adjusting the
previous and next links automatically.
  • Loading branch information
Jai Sharma authored and Arachnid committed Jan 27, 2010
1 parent f04e473 commit a676e11
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
53 changes: 53 additions & 0 deletions generators.py
Expand Up @@ -73,6 +73,28 @@ def get_resource_list(cls, post):
def get_etag(cls, post):
return post.hash

@classmethod
def get_prev_next(cls, post):
"""Retrieves the chronologically previous and next post for this post"""
import models

q = models.BlogPost.all().order('-published')
q.filter('published <', post.published)
prev = q.fetch(1)

q = models.BlogPost.all().order('published')
q.filter('published >', post.published)
next = q.fetch(1)
if len(prev) > 0:
prev = prev[0]
else:
prev = None
if len(next) > 0:
next = next[0]
else:
next = None
return prev,next

@classmethod
def generate_resource(cls, post, resource):
import models
Expand All @@ -83,10 +105,41 @@ def generate_resource(cls, post, resource):
template_vals = {
'post': post,
}
prev, next = cls.get_prev_next(post)
if prev is not None:
template_vals['prev']=prev
if next is not None:
template_vals['next']=next
rendered = utils.render_template("post.html", template_vals)
static.set(post.path, rendered, config.html_mime_type)
generator_list.append(PostContentGenerator)

class PostPrevNextContentGenerator(PostContentGenerator):
"""ContentGenerator for the blog posts chronologically before and after the blog post."""

@classmethod
def get_resource_list(cls, post):
prev, next = cls.get_prev_next(post)
resource_list = [res.key().id() for res in (prev,next) if res is not None]
return resource_list

@classmethod
def generate_resource(cls, post, resource):
import models
post = models.BlogPost.get_by_id(resource)
if post is None:
return
template_vals = {
'post': post,
}
prev, next = cls.get_prev_next(post)
if prev is not None:
template_vals['prev']=prev
if next is not None:
template_vals['next']=next
rendered = utils.render_template("post.html", template_vals)
static.set(post.path, rendered, config.html_mime_type)
generator_list.append(PostPrevNextContentGenerator)

class ListingContentGenerator(ContentGenerator):
path = None
Expand Down
6 changes: 5 additions & 1 deletion models.py
Expand Up @@ -58,6 +58,7 @@ def summary_hash(self):
return hashlib.sha1(str(val)).hexdigest()

def publish(self):
regenerate = False
if not self.path:
num = 0
content = None
Expand All @@ -67,9 +68,12 @@ def publish(self):
num += 1
self.path = path
self.put()
# Force regenerate on new publish. Also helps with generation of
# chronologically previous and next page.
regenerate = True
if not self.deps:
self.deps = {}
for generator_class, deps in self.get_deps():
for generator_class, deps in self.get_deps(regenerate=regenerate):
for dep in deps:
if generator_class.can_defer:
deferred.defer(generator_class.generate_resource, None, dep)
Expand Down
6 changes: 6 additions & 0 deletions themes/default/post.html
Expand Up @@ -15,6 +15,12 @@ <h2>{{post.title|escape}}</h2>
<p class="postmeta">
<span class="date">{{post.published|date:"d F, Y"}}</span>
</p>
{% if prev %}
<a id="prev" href="{{prev.path}}">Previous Post</a>
{% endif %}
{% if next %}
<a id="next" href="{{next.path}}">Next Post</a>
{% endif %}

{% if config.disqus_forum %}
<h3 id="comments">Comments</h3>
Expand Down

0 comments on commit a676e11

Please sign in to comment.