Add navigation bar + fix some bugs
This commit is contained in:
parent
a0a4e84c2e
commit
fa07825546
|
@ -61,10 +61,12 @@ class DynastieGenerator:
|
||||||
f.write(content)
|
f.write(content)
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
def createElement(self, dom, name, content):
|
def createElement(self, dom, name='', content=''):
|
||||||
div = dom.createElement('div')
|
div = dom.createElement('div')
|
||||||
div.setAttribute('class', name)
|
if name != '':
|
||||||
div.appendChild(dom.createTextNode(content))
|
div.setAttribute('class', name)
|
||||||
|
if content != '':
|
||||||
|
div.appendChild(dom.createTextNode(content))
|
||||||
|
|
||||||
return div
|
return div
|
||||||
|
|
||||||
|
@ -75,6 +77,9 @@ class DynastieGenerator:
|
||||||
new_elem = self.createElement(dom, node.localName, values[node.localName])
|
new_elem = self.createElement(dom, node.localName, values[node.localName])
|
||||||
elem.appendChild(new_elem)
|
elem.appendChild(new_elem)
|
||||||
else:
|
else:
|
||||||
elem.appendChild(node.cloneNode(True))
|
new_elem = node.cloneNode(False)
|
||||||
|
self.simpleTransform(values, dom, new_elem, node)
|
||||||
|
elem.appendChild(new_elem)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,17 +1,51 @@
|
||||||
|
import os
|
||||||
import datetime
|
import datetime
|
||||||
from xml.dom.minidom import parse
|
from xml.dom.minidom import parse, parseString
|
||||||
from dynastie.generators.generator import DynastieGenerator
|
from dynastie.generators.generator import DynastieGenerator
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
# TODO : links between indexes
|
# TODO : content
|
||||||
# TODO : gzip
|
|
||||||
|
|
||||||
class Index(DynastieGenerator):
|
class Index(DynastieGenerator):
|
||||||
|
|
||||||
cur_page = 0
|
cur_page = 0
|
||||||
|
nb_pages = 0
|
||||||
cur_article = 0
|
cur_article = 0
|
||||||
articles_per_page = 0
|
articles_per_page = 0
|
||||||
|
|
||||||
|
def createNavigation(self):
|
||||||
|
if self.nb_pages == 0 or self.nb_pages == 1:
|
||||||
|
return ''
|
||||||
|
|
||||||
|
nav = ''
|
||||||
|
if self.cur_page != 0:
|
||||||
|
nav = nav + '<a href="/index.html"><<</a> '
|
||||||
|
if self.cur_page == 1:
|
||||||
|
nav = nav + '<a href="/index.html"><</a> '
|
||||||
|
else:
|
||||||
|
nav = nav + '<a href="/index' + str(self.cur_page) + '.html"><</a> '
|
||||||
|
|
||||||
|
start = (self.cur_page/5)*5
|
||||||
|
end = start + 10
|
||||||
|
|
||||||
|
if end > self.nb_pages+1:
|
||||||
|
end = self.nb_pages+1
|
||||||
|
|
||||||
|
for i in range(start, end):
|
||||||
|
if i == self.cur_page:
|
||||||
|
nav = nav + str(i+1) + ' '
|
||||||
|
else:
|
||||||
|
if i == 0:
|
||||||
|
nav = nav + '<a href="/index.html">1</a> '
|
||||||
|
else:
|
||||||
|
nav = nav + '<a href="/index' + 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>'
|
||||||
|
|
||||||
|
return nav
|
||||||
|
|
||||||
def createArticle(self, article, dom, article_elem, root):
|
def createArticle(self, article, dom, article_elem, root):
|
||||||
values = {}
|
values = {}
|
||||||
values['title'] = article.title
|
values['title'] = article.title
|
||||||
|
@ -25,23 +59,32 @@ class Index(DynastieGenerator):
|
||||||
for node in root.childNodes:
|
for node in root.childNodes:
|
||||||
if node.prefix == 'dyn':
|
if node.prefix == 'dyn':
|
||||||
if node.localName == 'articles':
|
if node.localName == 'articles':
|
||||||
articles_elem = dom.createElement("articles")
|
articles_elem = self.createElement(dom, 'articles')
|
||||||
for i in range(0, self.articles_per_page):
|
for i in range(0, self.articles_per_page):
|
||||||
article_elem = dom.createElementNS(self.URI, "article")
|
article_elem = self.createElement(dom, 'article')
|
||||||
self.createArticle(articles[self.cur_article], dom, article_elem, node)
|
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)
|
articles_elem.appendChild(article_elem)
|
||||||
|
|
||||||
self.cur_article = self.cur_article + 1
|
self.cur_article = self.cur_article + 1
|
||||||
if self.cur_article == articles.count():
|
if self.cur_article == articles.count():
|
||||||
break
|
break
|
||||||
root.replaceChild(articles_elem, node)
|
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
|
continue
|
||||||
if node.hasChildNodes():
|
if node.hasChildNodes():
|
||||||
self.parse(articles, dom, node)
|
self.parse(articles, dom, node)
|
||||||
return
|
return
|
||||||
|
|
||||||
def generate(self, blog, src, output):
|
def generate(self, blog, src, output):
|
||||||
from dynastie.models import *
|
from dynastie.models import Article, Blog
|
||||||
|
|
||||||
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')
|
||||||
|
@ -67,13 +110,20 @@ class Index(DynastieGenerator):
|
||||||
if articles.count() < self.articles_per_page:
|
if articles.count() < self.articles_per_page:
|
||||||
articles = Article.objects.all()[:self.articles_per_page]
|
articles = Article.objects.all()[:self.articles_per_page]
|
||||||
|
|
||||||
|
if articles.count() > self.articles_per_page:
|
||||||
|
self.nb_pages = articles.count() / self.articles_per_page
|
||||||
|
|
||||||
filename = 'index.html'
|
filename = 'index.html'
|
||||||
while self.cur_article < articles.count():
|
while self.cur_page <= self.nb_pages:
|
||||||
|
#print 'Generate ' + filename
|
||||||
nodes = dom.getElementsByTagName("*")
|
nodes = dom.getElementsByTagName("*")
|
||||||
self.parse(articles, dom, nodes[0])
|
self.parse(articles, dom, nodes[0])
|
||||||
self.writeIfNotTheSame(output + '/' + filename, nodes[0].toxml('utf8'))
|
self.writeIfNotTheSame(output + '/' + filename, nodes[0].toxml('utf8'))
|
||||||
self.cur_page = self.cur_page + 1
|
self.cur_page = self.cur_page + 1
|
||||||
filename = 'index' + str(self.cur_page) + '.html'
|
filename = 'index' + str(self.cur_page) + '.html'
|
||||||
|
dom = parse(src + '/_index.html')
|
||||||
|
|
||||||
|
filename = output + '/' + filename
|
||||||
|
|
||||||
while os.path.exists(filename):
|
while os.path.exists(filename):
|
||||||
self.addReport('Removing unused ' + filename)
|
self.addReport('Removing unused ' + filename)
|
||||||
|
@ -83,7 +133,7 @@ class Index(DynastieGenerator):
|
||||||
self.addReport('Removing unused ' + filename)
|
self.addReport('Removing unused ' + filename)
|
||||||
os.unlink(filename)
|
os.unlink(filename)
|
||||||
self.cur_page = self.cur_page + 1
|
self.cur_page = self.cur_page + 1
|
||||||
filename = 'index' + str(self.cur_page) + '.html'
|
filename = output + '/index' + str(self.cur_page) + '.html'
|
||||||
|
|
||||||
return self.report
|
return self.report
|
||||||
|
|
||||||
|
|
10
models.py
10
models.py
|
@ -2,6 +2,7 @@ import os
|
||||||
import shutil
|
import shutil
|
||||||
import hashlib
|
import hashlib
|
||||||
import inspect
|
import inspect
|
||||||
|
from datetime import datetime
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.db.models.signals import post_init, post_delete, post_save
|
from django.db.models.signals import post_init, post_delete, post_save
|
||||||
|
@ -19,6 +20,7 @@ class Blog(models.Model):
|
||||||
|
|
||||||
src_path = ''
|
src_path = ''
|
||||||
output_path = ''
|
output_path = ''
|
||||||
|
report = ''
|
||||||
|
|
||||||
def create_paths(self):
|
def create_paths(self):
|
||||||
self.src_path = 'sites/' + self.name
|
self.src_path = 'sites/' + self.name
|
||||||
|
@ -80,8 +82,10 @@ class Blog(models.Model):
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
if src_md5.digest() != dst_md5.digest():
|
if src_md5.digest() != dst_md5.digest():
|
||||||
|
self.report = self.report + 'Update ' + dstname + '<br/>\n'
|
||||||
shutil.copy2(srcname, dstname)
|
shutil.copy2(srcname, dstname)
|
||||||
else:
|
else:
|
||||||
|
self.report = self.report + 'Add ' + dstname + '<br/>\n'
|
||||||
shutil.copy2(srcname, dstname)
|
shutil.copy2(srcname, dstname)
|
||||||
|
|
||||||
# XXX What about devices, sockets etc.?
|
# XXX What about devices, sockets etc.?
|
||||||
|
@ -95,17 +99,17 @@ class Blog(models.Model):
|
||||||
raise Exception(errors)
|
raise Exception(errors)
|
||||||
|
|
||||||
def generate(self):
|
def generate(self):
|
||||||
|
self.report = '<br/><br/>Generation of ' + datetime.now().strftime("%d/%m/%Y at %H:%M:%S") + '<br/>\n'
|
||||||
self.load_generators()
|
self.load_generators()
|
||||||
self.copytree(self.src_path, self.output_path)
|
self.copytree(self.src_path, self.output_path)
|
||||||
report = ''
|
|
||||||
for engine in self.engines:
|
for engine in self.engines:
|
||||||
for name, obj in inspect.getmembers(engine):
|
for name, obj in inspect.getmembers(engine):
|
||||||
if inspect.isclass(obj):
|
if inspect.isclass(obj):
|
||||||
e = obj()
|
e = obj()
|
||||||
r = e.generate(self, self.src_path, self.output_path)
|
r = e.generate(self, self.src_path, self.output_path)
|
||||||
if not r is None:
|
if not r is None:
|
||||||
report = report + '<br/>\n' + r
|
self.report = self.report + '<br/>\n' + r
|
||||||
return report
|
return self.report
|
||||||
|
|
||||||
class Editor(models.Model):
|
class Editor(models.Model):
|
||||||
name = models.CharField(max_length=255, unique=True)
|
name = models.CharField(max_length=255, unique=True)
|
||||||
|
|
11
views.py
11
views.py
|
@ -316,8 +316,7 @@ def delete_article(request, article_id):
|
||||||
@login_required
|
@login_required
|
||||||
def generate(request, blog_id):
|
def generate(request, blog_id):
|
||||||
if not request.user.is_superuser:
|
if not request.user.is_superuser:
|
||||||
#b = Blog.objects.filter(id=blog_id).filter(writers=request.user.id)
|
b = Blog.objects.filter(id=blog_id).filter(writers=request.user.id)[0]
|
||||||
b = Blog.objects.get(pk=blog_id)
|
|
||||||
else:
|
else:
|
||||||
b = Blog.objects.get(pk=blog_id)
|
b = Blog.objects.get(pk=blog_id)
|
||||||
|
|
||||||
|
@ -326,5 +325,11 @@ def generate(request, blog_id):
|
||||||
|
|
||||||
b.create_paths()
|
b.create_paths()
|
||||||
report = b.generate()
|
report = b.generate()
|
||||||
|
|
||||||
|
articles = Article.objects.filter(blog=b)
|
||||||
|
b = Blog.objects.get(pk=blog_id)
|
||||||
|
form = BlogForm(instance=b)
|
||||||
|
|
||||||
return render(request, 'generate.html', {'report': report})
|
c = {'blog' : b, 'articles' : articles, 'form' : form, 'report': report}
|
||||||
|
|
||||||
|
return render(request, 'generate.html', c)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user