Dynastie/dynastie/generators/archive.py

70 lines
2.2 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
"""
2014-01-04 13:55:30 +01:00
Copyright 2012-2014 Grégory Soutadé
This file is part of Dynastie.
Dynastie is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Dynastie is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Dynastie. If not, see <http://www.gnu.org/licenses/>.
"""
2012-07-22 20:49:11 +02:00
from datetime import datetime
from dynastie.generators.index import Index
from django.db import models
class Archive(Index):
2012-08-28 09:09:14 +02:00
def createArchive(self, posts, dom, root, node):
2012-07-22 20:49:11 +02:00
if node.hasAttribute('year'):
self.replaceByText(dom, root, node, str(self.cur_year))
return None
2012-07-22 20:49:11 +02:00
def generate(self, blog, src, output):
2012-08-28 09:09:14 +02:00
from dynastie.models import Post, Blog
2012-07-22 20:49:11 +02:00
self.hooks['archive'] = self.createArchive
2012-12-10 19:30:25 +01:00
dom = self.parseTemplate(blog, src, output, 'archive', 'archive')
if dom is None: return self.report
2012-07-22 20:49:11 +02:00
posts = Post.objects.filter(published=True, front_page=True).order_by('creation_date')
if len(posts) == 0:
return self.report
first_post = posts[0]
2014-09-24 20:27:27 +02:00
last_post = posts[len(posts)-1]
2012-07-22 20:49:11 +02:00
2013-02-17 08:57:37 +01:00
start_year = first_post.creation_date.year
end_year = last_post.creation_date.year
2012-07-22 20:49:11 +02:00
now = datetime.now()
2013-02-17 08:57:37 +01:00
for i in range(start_year, end_year):
if i == now.year: continue
2012-07-22 20:49:11 +02:00
2013-02-17 08:57:37 +01:00
self.cur_year = i
2014-09-24 20:27:27 +02:00
posts = Post.objects.filter(published=True, creation_date__gt=datetime(i, 1, 1), creation_date__lt=datetime(i+1, 1, 1)).order_by('-creation_date')
2014-09-24 20:27:27 +02:00
self.resetCounters()
2013-02-17 08:57:37 +01:00
self.dirname = '/archive/' + str(i)
2013-02-17 08:57:37 +01:00
self.generatePages(dom, posts, src, output, 'archive')
2012-07-22 20:49:11 +02:00
if not self.somethingWrote:
self.addReport('Nothing changed')
return self.report