From 8bb10fc1e1e4e0851a66aa471d378f849350270a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Soutad=C3=A9?= Date: Wed, 1 Aug 2012 22:04:41 +0200 Subject: [PATCH] Add recents generation to index --- generators/archive.py | 13 ++++++++----- generators/article.py | 4 +++- generators/category.py | 5 ++++- generators/index.py | 28 ++++++++++++++++++++++++++-- models.py | 13 ++++++++----- 5 files changed, 49 insertions(+), 14 deletions(-) diff --git a/generators/archive.py b/generators/archive.py index 7376e83..e429649 100644 --- a/generators/archive.py +++ b/generators/archive.py @@ -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) diff --git a/generators/article.py b/generators/article.py index c9900a5..4d9e6de 100644 --- a/generators/article.py +++ b/generators/article.py @@ -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) diff --git a/generators/category.py b/generators/category.py index a15c50c..53677ed 100644 --- a/generators/category.py +++ b/generators/category.py @@ -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') diff --git a/generators/index.py b/generators/index.py index 482d931..9821ed7 100644 --- a/generators/index.py +++ b/generators/index.py @@ -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') diff --git a/models.py b/models.py index 66f7aaa..58173d1 100644 --- a/models.py +++ b/models.py @@ -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'