Dynastie/generators/archive.py

73 lines
2.3 KiB
Python
Raw Normal View History

2012-07-22 20:49:11 +02:00
import os
from datetime import datetime
from xml.dom.minidom import parse, parseString
from dynastie.generators.generator import DynastieGenerator
from dynastie.generators.index import Index
from django.db import models
class Archive(Index):
cur_page = 0
nb_pages = 0
2012-08-28 09:09:14 +02:00
cur_post = 0
posts_per_page = 0
2012-07-22 20:49:11 +02:00
filename = 'index'
2012-08-14 21:26:48 +02:00
dirname = ''
2012-07-22 20:49:11 +02:00
cur_year = 0
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
2012-08-28 09:09:14 +02:00
posts = Post.objects.filter(published=True, front_page=True).order_by('creation_date')
2012-07-22 20:49:11 +02:00
2012-08-28 09:09:14 +02:00
if posts.count() != 0:
self.cur_year = int(posts[0].creation_date.year)
2012-07-22 20:49:11 +02:00
2012-08-28 09:09:14 +02:00
my_post = []
2012-07-22 20:49:11 +02:00
now = datetime.now()
2012-08-28 09:09:14 +02:00
nb_post = len(posts)
for i in range(0, nb_post):
2012-08-01 22:04:41 +02:00
if self.cur_year == now.year:
break
2012-07-22 20:49:11 +02:00
2012-08-28 09:09:14 +02:00
if i < nb_post-1:
if posts[i].creation_date.year != posts[i+1].creation_date.year:
2012-08-04 20:46:32 +02:00
dom = parse(src + '/_archive.html')
2012-08-28 09:09:14 +02:00
my_post.reverse()
self.nb_pages = 0
self.cur_page = 0
self.cur_post = 0
self.dirname = '/archive/' + str(self.cur_year)
self.generatePages(dom, my_post, src, output, 'archive', 'archive')
2012-08-28 09:09:14 +02:00
self.cur_year = int(posts[i+1].creation_date.year)
#print 'New year ' + str(self.cur_year)
2012-08-28 09:09:14 +02:00
my_post = []
if self.cur_year == int(now.year):
break
else:
2012-08-28 09:09:14 +02:00
my_post.append(posts[i])
2012-07-22 20:49:11 +02:00
else:
2012-08-28 09:09:14 +02:00
# Last post
my_post.append(posts[i])
2012-08-28 09:09:14 +02:00
if nb_post != 1 and posts[i].creation_date.year != posts[i-1].creation_date.year:
self.cur_year = int(posts[i].creation_date.year)
2012-07-22 20:49:11 +02:00
if not self.somethingWrote:
self.addReport('Nothing changed')
return self.report