Add recents generation to index
This commit is contained in:
parent
7a4b0892d3
commit
8bb10fc1e1
|
@ -69,7 +69,10 @@ class Archive(Index):
|
||||||
|
|
||||||
article_nodes = dom.getElementsByTagNameNS(self.URI, "articles")
|
article_nodes = dom.getElementsByTagNameNS(self.URI, "articles")
|
||||||
if not article_nodes is None:
|
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:
|
else:
|
||||||
self.addError('No tag dyn:articles found')
|
self.addError('No tag dyn:articles found')
|
||||||
|
|
||||||
|
@ -81,15 +84,15 @@ class Archive(Index):
|
||||||
my_articles = []
|
my_articles = []
|
||||||
now = datetime.now()
|
now = datetime.now()
|
||||||
for article in articles:
|
for article in articles:
|
||||||
# if self.cur_year == now.year:
|
if self.cur_year == now.year:
|
||||||
# break
|
break
|
||||||
|
|
||||||
if article.creation_date.year != self.cur_year:
|
if article.creation_date.year != self.cur_year:
|
||||||
self.createArchives(src, output, dom, hooks, my_articles)
|
self.createArchives(src, output, dom, hooks, my_articles)
|
||||||
self.cur_year = article.creation_date.year
|
self.cur_year = article.creation_date.year
|
||||||
#print 'New year ' + str(self.cur_year)
|
#print 'New year ' + str(self.cur_year)
|
||||||
# if self.cur_year == now.year:
|
if self.cur_year == now.year:
|
||||||
# continue
|
continue
|
||||||
my_articles = []
|
my_articles = []
|
||||||
else:
|
else:
|
||||||
my_articles.append(article)
|
my_articles.append(article)
|
||||||
|
|
|
@ -11,8 +11,10 @@ class Article(DynastieGenerator):
|
||||||
values = {}
|
values = {}
|
||||||
values['title'] = article.title
|
values['title'] = article.title
|
||||||
values['author'] = article.author.first_name
|
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'] = ''
|
values['content'] = ''
|
||||||
|
print article.title
|
||||||
|
|
||||||
self.simpleTransform(values, dom, article_elem, root)
|
self.simpleTransform(values, dom, article_elem, root)
|
||||||
|
|
||||||
|
|
|
@ -41,7 +41,10 @@ class Category(Index):
|
||||||
|
|
||||||
article_nodes = dom.getElementsByTagNameNS(self.URI, "articles")
|
article_nodes = dom.getElementsByTagNameNS(self.URI, "articles")
|
||||||
if not article_nodes is None:
|
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:
|
else:
|
||||||
self.addError('No tag dyn:articles found')
|
self.addError('No tag dyn:articles found')
|
||||||
|
|
||||||
|
|
|
@ -71,11 +71,32 @@ class Index(DynastieGenerator):
|
||||||
break
|
break
|
||||||
root.replaceChild(articles_elem, node)
|
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):
|
def generate(self, blog, src, output):
|
||||||
from dynastie.models import Article, Blog
|
from dynastie.models import Article, Blog
|
||||||
|
|
||||||
hooks = {'articles' : self.createArticles,
|
hooks = {'articles' : self.createArticles,
|
||||||
'navigation' : self.createNavigation}
|
'navigation' : self.createNavigation,
|
||||||
|
'recents' : self.createRecents}
|
||||||
|
|
||||||
if not os.path.exists(src + '/_index.html'):
|
if not os.path.exists(src + '/_index.html'):
|
||||||
self.addError('No _index.html found, exiting')
|
self.addError('No _index.html found, exiting')
|
||||||
|
@ -90,7 +111,10 @@ class Index(DynastieGenerator):
|
||||||
article_nodes = dom.getElementsByTagNameNS(self.URI, "articles")
|
article_nodes = dom.getElementsByTagNameNS(self.URI, "articles")
|
||||||
|
|
||||||
if not article_nodes is None:
|
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:
|
else:
|
||||||
self.addError('No tag dyn:articles found')
|
self.addError('No tag dyn:articles found')
|
||||||
|
|
||||||
|
|
13
models.py
13
models.py
|
@ -181,7 +181,7 @@ class Article(models.Model):
|
||||||
title_slug = models.CharField(max_length=255)
|
title_slug = models.CharField(max_length=255)
|
||||||
category = models.ForeignKey(Category, blank=True, null=True, on_delete=models.SET_NULL)
|
category = models.ForeignKey(Category, blank=True, null=True, on_delete=models.SET_NULL)
|
||||||
published = models.BooleanField()
|
published = models.BooleanField()
|
||||||
creation_date = models.DateField()
|
creation_date = models.DateTimeField()
|
||||||
front_page = models.BooleanField()
|
front_page = models.BooleanField()
|
||||||
author = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)
|
author = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)
|
||||||
description = models.TextField(max_length=255, blank=True)
|
description = models.TextField(max_length=255, blank=True)
|
||||||
|
@ -189,6 +189,12 @@ class Article(models.Model):
|
||||||
tags = models.ManyToManyField(Tag, blank=True, null=True)
|
tags = models.ManyToManyField(Tag, blank=True, null=True)
|
||||||
blog = models.ForeignKey(Blog)
|
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):
|
def slugify(self):
|
||||||
name = normalize('NFKD', self.title).encode('ascii', 'ignore').replace(' ', '-').lower()
|
name = normalize('NFKD', self.title).encode('ascii', 'ignore').replace(' ', '-').lower()
|
||||||
#remove `other` characters
|
#remove `other` characters
|
||||||
|
@ -224,10 +230,7 @@ class Article(models.Model):
|
||||||
if os.path.exists(filename):
|
if os.path.exists(filename):
|
||||||
os.unlink(filename)
|
os.unlink(filename)
|
||||||
|
|
||||||
output = b.output_path
|
output = b.output_path + self.getPath()
|
||||||
filename = output + '/article/'
|
|
||||||
filename = filename + self.creation_date.strftime("%Y") + '/' + self.creation_date.strftime("%m") + '/'
|
|
||||||
filename = filename + self.title_slug + '.html'
|
|
||||||
if os.path.exists(filename):
|
if os.path.exists(filename):
|
||||||
os.unlink(filename)
|
os.unlink(filename)
|
||||||
filename = filename + '.gz'
|
filename = filename + '.gz'
|
||||||
|
|
Loading…
Reference in New Issue
Block a user