Skip to content
Browse files

Fixing paging Bug

  • Loading branch information...
1 parent 7e4bea4 commit 5b5b1bcae5cf81f6faa33bdfd8e94dba52219770 @varunpant committed Feb 3, 2013
Showing with 27 additions and 35 deletions.
  1. +21 −29 controllers/main_controller.py
  2. +6 −6 services/post_service.py
View
50 controllers/main_controller.py
@@ -90,13 +90,11 @@ def GET(self, page):
page = safe_number(page)
if page < 1:
return notfound("The requested resource was not found on this server.Pages start from 1")
- count = post_service.count_published()
- if count < 0:
- return notfound("No archived posts were found.")
- offset = start_index(page, blog_settings.items_per_page)
+
limit = blog_settings.items_per_page
+ offset = start_index(page, limit)
posts = post_service.get_published(offset, limit)
- page_count = total_page(count, limit)
+ page_count = total_page(len(posts), limit)
nextLink = previousLink = None
if page < page_count:
nextLink = "/archives/" + str(page + 1)
@@ -113,18 +111,15 @@ def GET(self, page, year):
year = safe_number(year)
if year <= 1990:
return notfound("The requested resources for year: ' " + str(year) + " ' were not found")
- count = post_service.count_published()
- if count < 0:
- return notfound("No archived posts were found.")
- offset = start_index(page, blog_settings.items_per_page)
- limit = blog_settings.items_per_page
+ limit = blog_settings.items_per_page
+ offset = start_index(page, limit)
posts = post_service.find_published_by_year(year, offset, limit)
- page_count = total_page(count, limit)
+ page_count = total_page(len(posts), limit)
nextLink = previousLink = None
if page < page_count:
- nextLink = "/archives/" + str(page + 1)
+ nextLink = "/archives/" + str(page + 1) + "/" + year
if page > 1 :
- previousLink = "/archives/" + str(page - 1)
+ previousLink = "/archives/" + str(page - 1) + "/" + year
return render.index(posts, nextLink, previousLink)
class ArchivePageYearMonth:
@@ -138,18 +133,17 @@ def GET(self, page, year, month):
safe_month = safe_number(month)
if safe_month < 1 or safe_month > 12:
return notfound("Incorrect month ' " + str(safe_month) + " '")
- count = post_service.count_published()
- if count < 0:
- return notfound("No archived posts were found.")
- offset = start_index(page, blog_settings.items_per_page)
- limit = blog_settings.items_per_page
+
+ limit = blog_settings.items_per_page
+ offset = start_index(page, limit)
posts = post_service.find_published_by_year_and_month(safe_year, safe_month, offset, limit)
- page_count = total_page(count, limit)
+ page_count = total_page(len(posts), limit)
+
nextLink = previousLink = None
if page < page_count:
- nextLink = "/archives/" + str(page + 1)
+ nextLink = "/archives/" + str(page + 1) + "/" + year + "/" + month
if page > 1 :
- previousLink = "/archives/" + str(page - 1)
+ previousLink = "/archives/" + str(page - 1) + "/" + year + "/" + month
return render.index(posts, nextLink, previousLink)
class ArchivePageYearMonthDay:
@@ -166,18 +160,16 @@ def GET(self, page, year, month, day):
safe_day = safe_number(day)
if safe_day < 1 or safe_day > calendar.monthrange(int(year), int(month))[1]:
return notfound("Incorrect day: ' " + str(day) + " '")
- count = post_service.count_published()
- if count < 0:
- return notfound("No archived posts were found.")
- offset = start_index(page, blog_settings.items_per_page)
- limit = blog_settings.items_per_page
+
+ limit = blog_settings.items_per_page
+ offset = start_index(page, limit)
posts = post_service.find_published_by_year_month_and_day(safe_year, safe_month, safe_day, offset, limit)
- page_count = total_page(count, limit)
+ page_count = total_page(len(posts), limit)
nextLink = previousLink = None
if page < page_count:
- nextLink = "/archives/" + str(page + 1)
+ nextLink = "/archives/" + str(page + 1) + "/" + year + "/" + month + "/" + day
if page > 1 :
- previousLink = "/archives/" + str(page - 1)
+ previousLink = "/archives/" + str(page - 1) + "/" + year + "/" + month + "/" + day
return render.index(posts, nextLink, previousLink)
class Contact:
View
12 services/post_service.py
@@ -44,21 +44,21 @@ def find_published_by_date_range(start_date, end_date):
session = getSession()
return session.query(Post).filter(Post.published_at.between(start_date, end_date)).order_by(Post.published_at.desc()).all()
-def find_published_by_year(year, offset, items_per_page):
+def find_published_by_year(year, offset, limit):
session = getSession()
- return session.query(Post).filter(Post.published_at.between(str(year) + "-01-1 00:00:00", str(year) + "-12-31 00:00:00")).order_by(Post.published_at.desc()).all()
+ return session.query(Post).filter(Post.published_at.between(str(year) + "-01-1 00:00:00", str(year) + "-12-31 00:00:00")).order_by(Post.published_at.desc()).limit(limit).all()
-def find_published_by_year_and_month(year, month, offset, items_per_page):
+def find_published_by_year_and_month(year, month, offset, limit):
session = getSession()
startDate = date(year, month, 1)
endDate = date(year, month, calendar.monthrange(year, month)[1])
- return session.query(Post).filter(Post.published_at.between(startDate, endDate)).order_by(Post.published_at.desc()).all()
+ return session.query(Post).filter(Post.published_at.between(startDate, endDate)).order_by(Post.published_at.desc()).limit(limit).offset(offset).all()
-def find_published_by_year_month_and_day(year, month, day, offset, items_per_page):
+def find_published_by_year_month_and_day(year, month, day, offset, limit):
session = getSession()
startDate = datetime(year, month, day, 0, 0, 0)
endDate = datetime(year, month, day, 23, 59, 59)
- return session.query(Post).filter(Post.published_at.between(startDate, endDate)).order_by(Post.published_at.desc()).all()
+ return session.query(Post).filter(Post.published_at.between(startDate, endDate)).order_by(Post.published_at.desc()).limit(limit).offset(offset).all()
def count_all():
session = getSession()

0 comments on commit 5b5b1bc

Please sign in to comment.
Something went wrong with that request. Please try again.