Skip to content

Instantly share code, notes, and snippets.

@vishnevskiy
Created April 11, 2010 06:54
Show Gist options
  • Save vishnevskiy/362559 to your computer and use it in GitHub Desktop.
Save vishnevskiy/362559 to your computer and use it in GitHub Desktop.
@route('/forum/p/(\w+)/', 'forum_post')
class PostHandler(BaseHandler):
def get(self, id):
post = Post.documents.find_one({'_id': ObjectId(id), 'guild_id': self.guild_id}, fields=['thread_id', 'n'])
assert post, 'Post does not exist.'
thread = Thread.documents.get(post.thread_id, fields=['slug'])
if post.n == None:
post.n = Post.documents.eval("""function() {
var n = -1;
db[collection].find(query).forEach(function(doc) {
n++;
if (doc._id.toString() == post_id) {
db[collection].update({_id: doc._id}, {$set: {n: n}});
return false;
}
});
return n;
}""", query={'thread_id': thread.id}, scope={'post_id': post.id})
posts_per_page = self.current_user.get_setting('posts_per_page')
return self.redirect(' % s?page = % d#%s' % (thread.get_url(), int(math.ceil(float(post.n + 1) / posts_per_page)), id))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment