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))
|
|
|
|
|
2012-10-16 22:06:33 +02:00
|
|
|
return None
|
|
|
|
|
2012-08-28 09:09:14 +02:00
|
|
|
def createArchives(self, src, output, dom, hooks, posts):
|
2012-07-22 20:49:11 +02:00
|
|
|
filename = self.filename + '.html'
|
|
|
|
self.nb_pages = 0
|
|
|
|
self.cur_page = 0
|
2012-08-28 09:09:14 +02:00
|
|
|
self.cur_post = 0
|
2012-08-04 20:46:32 +02:00
|
|
|
|
2012-08-28 09:09:14 +02:00
|
|
|
if len(posts) > self.posts_per_page:
|
|
|
|
self.nb_pages = self.computeNbPages(len(posts), self.posts_per_page)
|
2012-08-14 21:26:48 +02:00
|
|
|
|
|
|
|
self.dirname = '/archive/' + str(self.cur_year)
|
2012-07-22 20:49:11 +02:00
|
|
|
|
2012-08-14 21:26:48 +02:00
|
|
|
if not os.path.exists(output + self.dirname):
|
|
|
|
os.mkdir(output + self.dirname)
|
2012-07-22 20:49:11 +02:00
|
|
|
|
|
|
|
while self.cur_page <= self.nb_pages:
|
|
|
|
#print 'Generate ' + filename
|
|
|
|
nodes = dom.getElementsByTagName("*")
|
2012-08-28 09:09:14 +02:00
|
|
|
nodes[0] = self.parse(src, hooks, posts, dom, nodes[0])
|
2012-08-15 18:11:55 +02:00
|
|
|
self.writeIfNotTheSame(output + self.dirname + '/' + filename, nodes[0])
|
2012-07-22 20:49:11 +02:00
|
|
|
self.cur_page = self.cur_page + 1
|
|
|
|
filename = self.filename + str(self.cur_page) + '.html'
|
|
|
|
dom = parse(src + '/_archive.html')
|
|
|
|
|
2012-08-03 21:58:04 +02:00
|
|
|
while os.path.exists(filename):
|
|
|
|
self.addReport('Removing unused ' + filename)
|
|
|
|
os.unlink(filename)
|
|
|
|
filename = filename + '.gz'
|
|
|
|
if os.path.exists(filename):
|
2012-07-22 20:49:11 +02:00
|
|
|
self.addReport('Removing unused ' + filename)
|
|
|
|
os.unlink(filename)
|
2012-08-03 21:58:04 +02:00
|
|
|
self.cur_page = self.cur_page + 1
|
2012-08-14 21:26:48 +02:00
|
|
|
filename = output + self.dirname + '/' + self.filename + str(self.cur_page) + '.html'
|
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
|
|
|
|
2012-08-28 09:09:14 +02:00
|
|
|
hooks = {'posts' : self.createPosts,
|
2012-07-22 20:49:11 +02:00
|
|
|
'navigation' : self.createNavigation,
|
|
|
|
'archive' : self.createArchive}
|
|
|
|
|
|
|
|
if not os.path.exists(src + '/_archive.html'):
|
|
|
|
self.addError('No _archive.html found, exiting')
|
|
|
|
return self.report
|
|
|
|
|
|
|
|
try:
|
|
|
|
dom = parse(src + '/_archive.html')
|
|
|
|
except xml.dom.DOMException as e:
|
|
|
|
self.addError('Error parsing _archive.html : ' + e)
|
|
|
|
return self.report
|
|
|
|
|
|
|
|
if not os.path.exists(output + '/archive'):
|
|
|
|
os.mkdir(output + '/archive')
|
|
|
|
|
2012-08-28 09:09:14 +02:00
|
|
|
post_nodes = dom.getElementsByTagNameNS(self.URI, "posts")
|
|
|
|
if not post_nodes is None:
|
|
|
|
if post_nodes[0].hasAttribute("limit"):
|
|
|
|
self.posts_per_page = int(post_nodes[0].getAttribute("limit"))
|
2012-08-01 22:04:41 +02:00
|
|
|
else:
|
2012-08-28 09:09:14 +02:00
|
|
|
self.posts_per_page = 5
|
2012-07-22 20:49:11 +02:00
|
|
|
else:
|
2012-08-28 09:09:14 +02:00
|
|
|
self.addError('No tag dyn:posts found')
|
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.createArchives(src, output, dom, hooks, my_post)
|
|
|
|
self.cur_year = int(posts[i+1].creation_date.year)
|
2012-08-03 21:58:04 +02:00
|
|
|
#print 'New year ' + str(self.cur_year)
|
2012-08-28 09:09:14 +02:00
|
|
|
my_post = []
|
2012-08-03 21:58:04 +02:00
|
|
|
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(post)
|
|
|
|
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
|
|
|
|
2012-08-28 09:09:14 +02:00
|
|
|
if len(my_post) != 0:
|
|
|
|
self.createArchives(src, output, dom, hooks, my_post)
|
2012-07-22 20:49:11 +02:00
|
|
|
|
|
|
|
if not self.somethingWrote:
|
|
|
|
self.addReport('Nothing changed')
|
|
|
|
|
|
|
|
return self.report
|
|
|
|
|