Dynastie/dynastie/generators/all_posts.py

178 lines
5.9 KiB
Python
Raw Normal View History

2013-10-26 09:26:47 +02:00
# -*- coding: utf-8 -*-
"""
2014-01-04 13:55:30 +01:00
Copyright 2012-2014 Grégory Soutadé
2013-10-26 09:26:47 +02:00
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/>.
"""
from dynastie.generators.index import Index
from django.db import models
class AllPosts(Index):
def createPosts(self, posts, dom, root, node):
posts_elem = self.createElement(dom, 'posts')
self.cur_post = 0
for p in posts:
self.cur_post_obj = p
post_elem = self.createElement(dom, 'post', '', subtree=node)
self._parse(self.hooks, posts, dom, post_elem)
posts_elem.appendChild(post_elem)
self.cur_post = self.cur_post + 1
root.replaceChild(posts_elem, node)
def createMonth(self, posts, dom, root, node):
date_format = node.getAttribute('format')
2014-09-24 20:27:27 +02:00
if not date_format:
2013-10-26 09:26:47 +02:00
date_format = '%B'
self.cur_month = posts[0].creation_date.month
root.removeChild(node)
cur_posts = []
month_elem = None
prev_month = None
for p in posts:
if p.creation_date.month == self.cur_month:
2014-09-24 20:27:27 +02:00
cur_posts.insert(0, p)
2013-10-26 09:26:47 +02:00
continue
month_elem = self.createElement(dom, 'month')
month_def = dom.createElement('month')
month_def.appendChild(dom.createTextNode(cur_posts[0].creation_date.strftime(date_format)))
month_elem.appendChild(month_def)
self.cloneSubtree(month_elem, node)
# Parse inner HTML
self._parse(self.hooks, cur_posts, dom, month_elem)
if not root.hasChildNodes():
root.appendChild(month_elem)
else:
root.insertBefore(month_elem, prev_month)
prev_month = month_elem
cur_posts = []
self.cur_month = p.creation_date.month
cur_posts.append(p)
# Last month
2014-09-24 20:27:27 +02:00
if cur_posts:
cur_posts.reverse()
2013-10-26 09:26:47 +02:00
month_elem = self.createElement(dom, 'month')
month_def = dom.createElement('month')
month_def.appendChild(dom.createTextNode(cur_posts[0].creation_date.strftime(date_format)))
month_elem.appendChild(month_def)
self.cloneSubtree(month_elem, node)
# Parse inner HTML
self._parse(self.hooks, cur_posts, dom, month_elem)
if not root.hasChildNodes():
root.appendChild(month_elem)
else:
root.insertBefore(month_elem, prev_month)
return None
def createYear(self, posts, dom, root, node):
date_format = node.getAttribute('format')
if date_format == '':
date_format = '%Y'
self.cur_year = posts[0].creation_date.year
root.removeChild(node)
cur_posts = []
year_elem = None
for p in posts:
if p.creation_date.year == self.cur_year:
cur_posts.append(p)
continue
year_elem = self.createElement(dom, 'year')
year_def = dom.createElement('year')
year_def.appendChild(dom.createTextNode(cur_posts[0].creation_date.strftime(date_format)))
year_elem.appendChild(year_def)
self.cloneSubtree(year_elem, node)
# Parse inner HTML
self._parse(self.hooks, cur_posts, dom, year_elem)
root.appendChild(year_elem)
cur_posts = []
self.cur_year = p.creation_date.year
cur_posts.append(p)
# Last year
if len(cur_posts) != 0:
2013-10-26 09:26:47 +02:00
year_elem = self.createElement(dom, 'year')
year_def = dom.createElement('year')
year_def.appendChild(dom.createTextNode(cur_posts[0].creation_date.strftime(date_format)))
year_elem.appendChild(year_def)
self.cloneSubtree(year_elem, node)
# Parse inner HTML
self._parse(self.hooks, cur_posts, dom, year_elem)
root.appendChild(year_elem)
return None
def createAllPosts(self, posts, dom, root, node):
posts_elem = self.createElement(dom, 'all_posts')
if len(posts) == 0:
post_elem = self.createElement(dom, '', '<b>No posts yet</b>')
posts_elem.appendChild(post_elem)
else:
for cnode in node.childNodes:
new_node = cnode.cloneNode(True)
posts_elem.appendChild(new_node)
self._parse(self.hooks, posts, dom, posts_elem)
root.replaceChild(posts_elem, node)
return posts_elem
def generate(self, blog, src, output):
from dynastie.models import Post, Blog, Category
2014-09-24 20:27:27 +02:00
self.filename = 'all_posts'
2013-10-26 09:26:47 +02:00
self.hooks['year'] = self.createYear
self.hooks['month'] = self.createMonth
self.hooks['posts'] = self.createPosts
self.hooks['all_posts'] = self.createAllPosts
dom = self.parseTemplate(blog, src, output, 'all_posts')
if dom is None: return self.report
posts = Post.objects.filter(published=True).order_by('-creation_date')
nodes = dom.getElementsByTagName("*")
nodes[0] = self.parse(src, self.hooks, posts, dom, nodes[0])
self.writeIfNotTheSame(output + "/" + self.filename + '.html', nodes[0])
if not self.somethingWrote:
self.addReport('Nothing changed')
return self.report