Add category generator
Add name_slug to category Do a more generic parse function
This commit is contained in:
parent
765c3ad19b
commit
47d68daec0
2
forms.py
2
forms.py
|
@ -17,7 +17,7 @@ class ArticleForm(ModelForm):
|
|||
class CategoryForm(ModelForm):
|
||||
class Meta:
|
||||
model = Category
|
||||
exclude = ('parent')
|
||||
exclude = ('parent', 'name_slug')
|
||||
|
||||
class UserForm(ModelForm):
|
||||
class Meta:
|
||||
|
|
|
@ -1 +1 @@
|
|||
__all__ = ["generator", "index", "article"]
|
||||
__all__ = ["generator", "index", "article", "category"]
|
||||
|
|
|
@ -43,8 +43,8 @@ class Article(DynastieGenerator):
|
|||
self.addError('Error parsing _article.html : ' + e)
|
||||
return self.report
|
||||
|
||||
if not os.path.exists(output + '/articles'):
|
||||
os.mkdir(output + '/articles')
|
||||
if not os.path.exists(output + '/article'):
|
||||
os.mkdir(output + '/article')
|
||||
|
||||
articles = Article.objects.all()
|
||||
|
||||
|
@ -52,7 +52,7 @@ class Article(DynastieGenerator):
|
|||
#print 'Generate ' + filename
|
||||
nodes = dom.getElementsByTagName("*")
|
||||
self.parse(article, dom, nodes[0])
|
||||
filename = output + '/articles/'
|
||||
filename = output + '/article/'
|
||||
filename = filename + article.creation_date.strftime("%Y") + '/' + article.creation_date.strftime("%m") + '/'
|
||||
if not os.path.exists(filename):
|
||||
os.makedirs(filename)
|
||||
|
|
85
generators/category.py
Normal file
85
generators/category.py
Normal file
|
@ -0,0 +1,85 @@
|
|||
import os
|
||||
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 Category(Index):
|
||||
|
||||
cur_page = 0
|
||||
nb_pages = 0
|
||||
cur_article = 0
|
||||
articles_per_page = 0
|
||||
filename = 'category'
|
||||
cur_category = None
|
||||
|
||||
def createCategory(self, articles, dom, root, node):
|
||||
if node.hasAttribute('name'):
|
||||
self.replaceByText(dom, root, node, self.cur_category.name)
|
||||
if node.hasAttribute('description'):
|
||||
self.replaceByText(dom, root, node, self.cur_category.description)
|
||||
|
||||
def generate(self, blog, src, output):
|
||||
from dynastie.models import Article, Blog, Category
|
||||
|
||||
hooks = {'articles' : self.createArticles,
|
||||
'navigation' : self.createNavigation,
|
||||
'category' : self.createCategory}
|
||||
|
||||
if not os.path.exists(src + '/_category.html'):
|
||||
self.addError('No _category.html found, exiting')
|
||||
return self.report
|
||||
|
||||
try:
|
||||
dom = parse(src + '/_category.html')
|
||||
except xml.dom.DOMException as e:
|
||||
self.addError('Error parsing _category.html : ' + e)
|
||||
return self.report
|
||||
|
||||
if not os.path.exists(output + '/category'):
|
||||
os.mkdir(output + '/category')
|
||||
|
||||
article_nodes = dom.getElementsByTagNameNS(self.URI, "articles")
|
||||
if not article_nodes is None:
|
||||
self.articles_per_page = int(article_nodes[0].getAttribute("limit"))
|
||||
else:
|
||||
self.addError('No tag dyn:articles found')
|
||||
|
||||
categories = Category.objects.all()
|
||||
|
||||
for category in categories:
|
||||
self.cur_category = category
|
||||
self.filename = category.name_slug
|
||||
filename = self.filename + '.html'
|
||||
articles = Article.objects.filter(category__exact=category)
|
||||
|
||||
self.nb_pages = 0
|
||||
self.cur_page = 0
|
||||
if articles.count() > self.articles_per_page:
|
||||
self.nb_pages = articles.count() / self.articles_per_page
|
||||
|
||||
while self.cur_page <= self.nb_pages:
|
||||
#print 'Generate ' + filename
|
||||
nodes = dom.getElementsByTagName("*")
|
||||
self.parse(hooks, articles, dom, nodes[0])
|
||||
self.writeIfNotTheSame(output + '/category/' + filename, nodes[0].toxml('utf8'))
|
||||
self.cur_page = self.cur_page + 1
|
||||
filename = self.filename + str(self.cur_page) + '.html'
|
||||
dom = parse(src + '/_category.html')
|
||||
|
||||
while os.path.exists(filename):
|
||||
self.addReport('Removing unused ' + filename)
|
||||
os.unlink(filename)
|
||||
filename = filename + '.gz'
|
||||
if os.path.exists(filename):
|
||||
self.addReport('Removing unused ' + filename)
|
||||
os.unlink(filename)
|
||||
self.cur_page = self.cur_page + 1
|
||||
filename = output + '/category/' + self.filename + str(self.cur_page) + '.html'
|
||||
|
||||
if not self.somethingWrote:
|
||||
self.addReport('Nothing changed')
|
||||
|
||||
return self.report
|
||||
|
|
@ -82,6 +82,17 @@ class DynastieGenerator:
|
|||
new_elem = node.cloneNode(False)
|
||||
self.simpleTransform(values, dom, new_elem, node)
|
||||
elem.appendChild(new_elem)
|
||||
|
||||
def replaceByText(self, dom, root, node, content):
|
||||
new_node = dom.createTextNode(content)
|
||||
root.replaceChild(new_node, node)
|
||||
|
||||
|
||||
|
||||
def parse(self, hooks, articles, dom, root):
|
||||
for node in root.childNodes:
|
||||
if node.prefix == 'dyn':
|
||||
if node.localName in hooks:
|
||||
hooks[node.localName](articles, dom, root, node)
|
||||
|
||||
if node.hasChildNodes():
|
||||
self.parse(hooks, articles, dom, node)
|
||||
return
|
||||
|
|
|
@ -4,26 +4,25 @@ from xml.dom.minidom import parse, parseString
|
|||
from dynastie.generators.generator import DynastieGenerator
|
||||
from django.db import models
|
||||
|
||||
# TODO : content
|
||||
|
||||
class Index(DynastieGenerator):
|
||||
|
||||
cur_page = 0
|
||||
nb_pages = 0
|
||||
cur_article = 0
|
||||
articles_per_page = 0
|
||||
filename = 'index'
|
||||
|
||||
def createNavigation(self):
|
||||
def createNavigation(self, articles, dom, root, node):
|
||||
if self.nb_pages == 0 or self.nb_pages == 1:
|
||||
return ''
|
||||
|
||||
nav = ''
|
||||
if self.cur_page != 0:
|
||||
nav = nav + '<a href="/index.html"><<</a> '
|
||||
nav = nav + '<a href="/' + self.filename + '.html"><<</a> '
|
||||
if self.cur_page == 1:
|
||||
nav = nav + '<a href="/index.html"><</a> '
|
||||
nav = nav + '<a href="/' + self.filename + '.html"><</a> '
|
||||
else:
|
||||
nav = nav + '<a href="/index' + str(self.cur_page) + '.html"><</a> '
|
||||
nav = nav + '<a href="/' + self.filename + str(self.cur_page) + '.html"><</a> '
|
||||
|
||||
start = (self.cur_page/5)*5
|
||||
end = start + 10
|
||||
|
@ -36,15 +35,17 @@ class Index(DynastieGenerator):
|
|||
nav = nav + str(i+1) + ' '
|
||||
else:
|
||||
if i == 0:
|
||||
nav = nav + '<a href="/index.html">1</a> '
|
||||
nav = nav + '<a href="/' + self.filename + '.html">1</a> '
|
||||
else:
|
||||
nav = nav + '<a href="/index' + str(i) + '.html">' + str(i+1) + '</a> '
|
||||
nav = nav + '<a href="/' + self.filename + str(i) + '.html">' + str(i+1) + '</a> '
|
||||
|
||||
if self.cur_page != self.nb_pages:
|
||||
nav = nav + '<a href="/index' + str(self.cur_page+1) + '.html">></a> '
|
||||
nav = nav + '<a href="/index' + str(self.nb_pages) + '.html">>></a>'
|
||||
nav = nav + '<a href="/' + self.filename + str(self.cur_page+1) + '.html">></a> '
|
||||
nav = nav + '<a href="/' + self.filename + str(self.nb_pages) + '.html">>></a>'
|
||||
|
||||
return nav
|
||||
new_dom = parseString('<div class="navigation">' + nav + '</div>')
|
||||
new_node = new_dom.getElementsByTagName('div')[0]
|
||||
root.replaceChild(new_node.cloneNode(True), node)
|
||||
|
||||
def createArticle(self, article, dom, article_elem, root):
|
||||
values = {}
|
||||
|
@ -55,37 +56,27 @@ class Index(DynastieGenerator):
|
|||
|
||||
self.simpleTransform(values, dom, article_elem, root)
|
||||
|
||||
def parse(self, articles, dom, root):
|
||||
for node in root.childNodes:
|
||||
if node.prefix == 'dyn':
|
||||
if node.localName == 'articles':
|
||||
articles_elem = self.createElement(dom, 'articles')
|
||||
for i in range(0, self.articles_per_page):
|
||||
article_elem = self.createElement(dom, 'article')
|
||||
if articles.count() > self.cur_article:
|
||||
self.createArticle(articles[self.cur_article], dom, article_elem, node)
|
||||
else:
|
||||
article_elem = self.createElement(dom, '', '<b>No articles yet</b>')
|
||||
articles_elem.appendChild(article_elem)
|
||||
def createArticles(self, articles, dom, root, node):
|
||||
articles_elem = self.createElement(dom, 'articles')
|
||||
for i in range(0, self.articles_per_page):
|
||||
article_elem = self.createElement(dom, 'article')
|
||||
if articles.count() > self.cur_article:
|
||||
self.createArticle(articles[self.cur_article], dom, article_elem, node)
|
||||
else:
|
||||
article_elem = self.createElement(dom, '', '<b>No articles yet</b>')
|
||||
articles_elem.appendChild(article_elem)
|
||||
|
||||
self.cur_article = self.cur_article + 1
|
||||
if self.cur_article == articles.count():
|
||||
break
|
||||
root.replaceChild(articles_elem, node)
|
||||
|
||||
if node.localName == 'navigation':
|
||||
new_dom = parseString('<div class="navigation">' + self.createNavigation() + '</div>')
|
||||
new_node = new_dom.getElementsByTagName('div')[0]
|
||||
root.replaceChild(new_node.cloneNode(True), node)
|
||||
|
||||
continue
|
||||
if node.hasChildNodes():
|
||||
self.parse(articles, dom, node)
|
||||
return
|
||||
self.cur_article = self.cur_article + 1
|
||||
if self.cur_article == articles.count():
|
||||
break
|
||||
root.replaceChild(articles_elem, node)
|
||||
|
||||
def generate(self, blog, src, output):
|
||||
from dynastie.models import Article, Blog
|
||||
|
||||
hooks = {'articles' : self.createArticles,
|
||||
'navigation' : self.createNavigation}
|
||||
|
||||
if not os.path.exists(src + '/_index.html'):
|
||||
self.addError('No _index.html found, exiting')
|
||||
return self.report
|
||||
|
@ -117,7 +108,7 @@ class Index(DynastieGenerator):
|
|||
while self.cur_page <= self.nb_pages:
|
||||
#print 'Generate ' + filename
|
||||
nodes = dom.getElementsByTagName("*")
|
||||
self.parse(articles, dom, nodes[0])
|
||||
self.parse(hooks, articles, dom, nodes[0])
|
||||
self.writeIfNotTheSame(output + '/' + filename, nodes[0].toxml('utf8'))
|
||||
self.cur_page = self.cur_page + 1
|
||||
filename = 'index' + str(self.cur_page) + '.html'
|
||||
|
|
13
init.sql
13
init.sql
|
@ -1,13 +0,0 @@
|
|||
BEGIN TRANSACTION;
|
||||
CREATE TABLE user (id INTEGER PRIMARY KEY, name VARCHAR(255), login VARCHAR(255), password VARCHAR(255), email VARCHAR(255), administrator CHAR(1), editor REFERENCES editor(id));
|
||||
CREATE TABLE editor (name VARCHAR(255));
|
||||
CREATE TABLE article (id INTEGER PRIMARY KEY, title VARCHAR(255), category REFERENCES category(id) ON DELETE SET NULL, published CHAR(1), creation_date DATE, front_page CHAR(1), author REFERENCES user(id) ON DELETE SET NULL, description TEXT, keywords TEXT);
|
||||
CREATE TABLE category (name VARCHAR(255) PRIMARY KEY, parent REFERENCES category(id) ON DELETE SET NULL);
|
||||
CREATE TABLE tag (name VARCHAR(255));
|
||||
CREATE TABLE tags (article REFERENCES article(id), tag REFERENCES tag(id));
|
||||
CREATE TABLE comment(id INTEGER PRIMARY KEY, article REFERENCES article(id), parent REFERENCES comment(id), comment_date DATE, author VARCHAR(255), email VARCHAR(255), comment TEXT);
|
||||
CREATE TABLE blog (id INTEGER PRIMARY KEY, name VARCHAR(255), title VARCHAR(255), description TEXT, keywords TEXT);
|
||||
CREATE TABLE writers (blog REFERENCES blog(id), user REFERENCES user(id));
|
||||
-- Administrator with empty password
|
||||
INSERT INTO user ("name", "login", "password", "administrator") VALUES ("Administrator", "admin", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "1");
|
||||
COMMIT TRANSACTION;
|
20
models.py
20
models.py
|
@ -121,9 +121,22 @@ class Editor(models.Model):
|
|||
|
||||
class Category(models.Model):
|
||||
name = models.CharField(max_length=255, unique=True)
|
||||
name_slug = models.CharField(max_length=255)
|
||||
parent = models.ForeignKey('self', blank=True, null=True)
|
||||
description = models.TextField(max_length=255, blank=True)
|
||||
|
||||
def slugify(self):
|
||||
name = normalize('NFKD', self.name).encode('ascii', 'ignore').replace(' ', '-').lower()
|
||||
#remove `other` characters
|
||||
name = sub('[^a-zA-Z0-9_-]', '', name)
|
||||
#nomalize dashes
|
||||
name = sub('-+', '-', name)
|
||||
self.name_slug = name
|
||||
|
||||
def save(self):
|
||||
self.slugify()
|
||||
super(Category, self).save()
|
||||
|
||||
class Tag(models.Model):
|
||||
name = models.CharField(max_length=255, unique=True)
|
||||
|
||||
|
@ -140,7 +153,6 @@ class Article(models.Model):
|
|||
tags = models.ManyToManyField(Tag, blank=True, null=True)
|
||||
blog = models.ForeignKey(Blog)
|
||||
|
||||
|
||||
def slugify(self):
|
||||
name = normalize('NFKD', self.title).encode('ascii', 'ignore').replace(' ', '-').lower()
|
||||
#remove `other` characters
|
||||
|
@ -177,7 +189,7 @@ class Article(models.Model):
|
|||
os.unlink(filename)
|
||||
|
||||
output = b.output_path
|
||||
filename = output + '/articles/'
|
||||
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):
|
||||
|
@ -186,12 +198,12 @@ class Article(models.Model):
|
|||
if os.path.exists(filename):
|
||||
os.unlink(filename)
|
||||
|
||||
filename = output + '/articles/'
|
||||
filename = output + '/article/'
|
||||
filename = filename + self.creation_date.strftime("%Y") + '/' + self.creation_date.strftime("%m") + '/'
|
||||
if len(os.listdir(filename)) == 0:
|
||||
os.rmdir(filename)
|
||||
|
||||
filename = output + '/articles/'
|
||||
filename = output + '/article/'
|
||||
filename = filename + self.creation_date.strftime("%Y") + '/'
|
||||
if len(os.listdir(filename)) == 0:
|
||||
os.rmdir(filename)
|
||||
|
|
Loading…
Reference in New Issue
Block a user