Commits

Anonymous committed df724ed

pagination for blog

Comments (0)

Files changed (6)

 from django.template import RequestContext
 from django.http import HttpResponseRedirect, Http404
 
-from google.appengine.ext.webapp.util import login_required
+from django.conf import settings
 
 from google.appengine.api import memcache
 
+from gaesessions import get_current_session
 
 def listPost(request):
 	posts = models.Post.all()
 
 
 def stream(request):
+	PAGESIZE = int(settings.PAGESIZE)
+
 	# get post list
-	posts = models.Post.all().order('-pub_date')
+	if request.GET.get('page'):
+		#get the corrent page
+		page = int(request.GET.get('page'))
+		offset = settings.PAGESIZE *(page - 1)
+		posts = models.Post.all().order('-pub_date').fetch(settings.PAGESIZE, offset)
+	else:
+		posts = models.Post.all().order('-pub_date').fetch(settings.PAGESIZE)
+		page = 1
+
+
+	# check if there is next page
+	offset = settings.PAGESIZE *(page)
+	next_page = models.Post.all().order('-pub_date').fetch(settings.PAGESIZE, offset)
+	if next_page:
+		p_next = page + 1
+	else:
+		p_next = None
+
+	paging = {'prev': page - 1, 'next':p_next}
+	# get tag and categories
+	cat_tag = get_tag_cat_list()
+
+	return render_to_response('front/stream.html', {'posts': posts,
+												    'paging':paging,
+													  'categories': cat_tag['cat_list'],
+													  'tags': cat_tag['tag_list']},
+		                           						context_instance=RequestContext(request))
+
+
+
+def listPostByCategory(request, cat):
+	posts = models.Post.all().filter('category =', cat.replace('-',' '))
 
 	# get tag and categories
 	cat_tag = get_tag_cat_list()
 
 
 
-def listPostByCategory(request, cat):
-	posts = models.Post.all().filter('category =', cat.replace('-',' '))
-
-	# get tag and categories
-	cat_tag = get_tag_cat_list()
-
-	return render_to_response('front/stream.html', {'posts': posts,
-											  'categories': cat_tag['cat_list'],
-											  'tags': cat_tag['tag_list']},
-                           						context_instance=RequestContext(request))
-
-
-
 def listPostByTag(request, tag):
 	posts = models.Post.all().filter('tags =', tag)
 

posts/views.pyc

Binary file modified.
 #    'django.contrib.sessions',
 #    'django.contrib.sites',
 )
+
+
+# APP SETTINGS
+PAGESIZE = 5

settings.pyc

Binary file modified.

templates/front/stream.html

 	<hr style="color:white">
 	<div class="clear"></div>
 {% endfor %}
+
+{% if paging.prev %}<a href="/?page={{ paging.prev }}">Prev Posts</a> &nbsp;&nbsp;{% endif %}
+{% if paging.next %}<a href="/?page={{ paging.next }}">More Post</a>{% endif %}
+
 {% endblock %}
 
 
Binary file modified.