Add recents generation to index

This commit is contained in:
Grégory Soutadé 2012-08-01 22:04:41 +02:00
parent 7a4b0892d3
commit 8bb10fc1e1
5 changed files with 49 additions and 14 deletions

View File

@ -69,7 +69,10 @@ class Archive(Index):
article_nodes = dom.getElementsByTagNameNS(self.URI, "articles")
if not article_nodes is None:
self.articles_per_page = int(article_nodes[0].getAttribute("limit"))
if article_nodes[0].hasAttribute("limit"):
self.articles_per_page = int(article_nodes[0].getAttribute("limit"))
else:
self.articles_per_page = 5
else:
self.addError('No tag dyn:articles found')
@ -81,15 +84,15 @@ class Archive(Index):
my_articles = []
now = datetime.now()
for article in articles:
# if self.cur_year == now.year:
# break
if self.cur_year == now.year:
break
if article.creation_date.year != self.cur_year:
self.createArchives(src, output, dom, hooks, my_articles)
self.cur_year = article.creation_date.year
#print 'New year ' + str(self.cur_year)
# if self.cur_year == now.year:
# continue
if self.cur_year == now.year:
continue
my_articles = []
else:
my_articles.append(article)

View File

@ -11,8 +11,10 @@ class Article(DynastieGenerator):
values = {}
values['title'] = article.title
values['author'] = article.author.first_name
values['date'] = article.creation_date.strftime("%d/%m/%Y")
if article.creation_date != None:
values['date'] = article.creation_date.strftime("%d/%m/%Y")
values['content'] = ''
print article.title
self.simpleTransform(values, dom, article_elem, root)

View File

@ -41,7 +41,10 @@ class Category(Index):
article_nodes = dom.getElementsByTagNameNS(self.URI, "articles")
if not article_nodes is None:
self.articles_per_page = int(article_nodes[0].getAttribute("limit"))
if article_nodes[0].hasAttribute("limit"):
self.articles_per_page = int(article_nodes[0].getAttribute("limit"))
else:
self.articles_per_page = 5
else:
self.addError('No tag dyn:articles found')

View File

@ -71,11 +71,32 @@ class Index(DynastieGenerator):
break
root.replaceChild(articles_elem, node)
def createRecents(self, articles, dom, root, node):
if node.hasAttribute("limit"):
nb_recents = int(node.getAttribute("limit"))
else:
nb_recents = 5
list_elem = dom.createElement('ul')
for i in range(0, nb_recents):
article_elem = dom.createElement('li')
if self.cur_article+i < len(articles):
link_elem = dom.createElement('a')
link_elem.setAttribute('href', articles[self.cur_article+i].getPath())
text_elem = dom.createTextNode(articles[self.cur_article+i].title)
link_elem.appendChild(text_elem)
article_elem.appendChild(link_elem)
else:
break
list_elem.appendChild(article_elem)
root.replaceChild(list_elem, node)
def generate(self, blog, src, output):
from dynastie.models import Article, Blog
hooks = {'articles' : self.createArticles,
'navigation' : self.createNavigation}
'navigation' : self.createNavigation,
'recents' : self.createRecents}
if not os.path.exists(src + '/_index.html'):
self.addError('No _index.html found, exiting')
@ -90,7 +111,10 @@ class Index(DynastieGenerator):
article_nodes = dom.getElementsByTagNameNS(self.URI, "articles")
if not article_nodes is None:
self.articles_per_page = int(article_nodes[0].getAttribute("limit"))
if article_nodes[0].hasAttribute("limit"):
self.articles_per_page = int(article_nodes[0].getAttribute("limit"))
else:
self.articles_per_page = 5
else:
self.addError('No tag dyn:articles found')

View File

@ -181,7 +181,7 @@ class Article(models.Model):
title_slug = models.CharField(max_length=255)
category = models.ForeignKey(Category, blank=True, null=True, on_delete=models.SET_NULL)
published = models.BooleanField()
creation_date = models.DateField()
creation_date = models.DateTimeField()
front_page = models.BooleanField()
author = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)
description = models.TextField(max_length=255, blank=True)
@ -189,6 +189,12 @@ class Article(models.Model):
tags = models.ManyToManyField(Tag, blank=True, null=True)
blog = models.ForeignKey(Blog)
def getPath(self):
filename = '/article/'
filename = filename + self.creation_date.strftime("%Y") + '/' + self.creation_date.strftime("%m") + '/'
filename = filename + self.title_slug + '.html'
return filename
def slugify(self):
name = normalize('NFKD', self.title).encode('ascii', 'ignore').replace(' ', '-').lower()
#remove `other` characters
@ -224,10 +230,7 @@ class Article(models.Model):
if os.path.exists(filename):
os.unlink(filename)
output = b.output_path
filename = output + '/article/'
filename = filename + self.creation_date.strftime("%Y") + '/' + self.creation_date.strftime("%m") + '/'
filename = filename + self.title_slug + '.html'
output = b.output_path + self.getPath()
if os.path.exists(filename):
os.unlink(filename)
filename = filename + '.gz'