Permalink
Browse files

Fixed paging in archives

  • Loading branch information...
1 parent 5b5b1bc commit 5acc5f6ee4992faf3d60f803eb861537639d692b @varunpant committed Feb 3, 2013
Showing with 25 additions and 5 deletions.
  1. +8 −5 controllers/main_controller.py
  2. +17 −0 services/post_service.py
@@ -108,13 +108,14 @@ def GET(self, page, year):
page = safe_number(page)
if page < 1:
return notfound("The requested resource was not found on this server.Pages start from 1")
- year = safe_number(year)
+ safe_year = safe_number(year)
if year <= 1990:
return notfound("The requested resources for year: ' " + str(year) + " ' were not found")
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(len(posts), limit)
+ posts = post_service.find_published_by_year(safe_year, offset, limit)
+ count = post_service.count_published_by_year(safe_year)
+ page_count = total_page(count, limit)
nextLink = previousLink = None
if page < page_count:
nextLink = "/archives/" + str(page + 1) + "/" + year
@@ -137,7 +138,8 @@ def GET(self, page, year, month):
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(len(posts), limit)
+ count = post_service.count_published_by_year_month(safe_year, safe_month)
+ page_count = total_page(count, limit)
nextLink = previousLink = None
if page < page_count:
@@ -164,7 +166,8 @@ def GET(self, page, year, month, day):
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(len(posts), limit)
+ count = post_service.count_published_by_year_month_and_day(safe_year, safe_month,safe_day)
+ page_count = total_page(count, limit)
nextLink = previousLink = None
if page < page_count:
nextLink = "/archives/" + str(page + 1) + "/" + year + "/" + month + "/" + day
@@ -76,6 +76,23 @@ def count_published_by_tag(slug):
session = getSession()
return session.query(Post).filter(Post.published_at != None).filter(Post.tags.any(Tag.slug == slug)).count()
+def count_published_by_year(year):
+ 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")).count()
+
+def count_published_by_year_month(year, month):
+ 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)).count()
+
+def count_published_by_year_month_and_day(year, month, day):
+ 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)).count()
+
+
def get_archives():
session = getSession()
return session.query(Archives).filter(Archives.posts_count > 0).order_by(Archives.year.desc(),Archives.month.desc()).all()

0 comments on commit 5acc5f6

Please sign in to comment.