Skip to content

Instantly share code, notes, and snippets.

@mikeywaites
Created December 15, 2011 11:27
Show Gist options
  • Save mikeywaites/1480779 to your computer and use it in GitHub Desktop.
Save mikeywaites/1480779 to your computer and use it in GitHub Desktop.
def get_archives(self):
""" get all post objects grouped by year/month """
archives = {}
posts = self.active(Post.ACTIVE).published().order_by('-publish_date')
for p in posts:
year = p.publish_date.year
month = p.publish_date.month
try:
archives[year][month-1][1]=True
except KeyError:
# catch the KeyError, and set up list for that year
archives[year]=[[datetime.date(year,m,1),False, 0] for m in xrange(1,13)]
archives[year][month-1][1]=True
if archives[year][month-1][1] == True:
archives[year][month-1][2] +=1
for year in archives:
archives[year].sort(reverse=True)
archives = sorted(archives.items(),reverse=True)
return archives
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment