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.close()
|
||||
|
||||
def createElement(self, dom, name, content):
|
||||
def createElement(self, dom, name='', content=''):
|
||||
div = dom.createElement('div')
|
||||
div.setAttribute('class', name)
|
||||
div.appendChild(dom.createTextNode(content))
|
||||
if name != '':
|
||||
div.setAttribute('class', name)
|
||||
if content != '':
|
||||
div.appendChild(dom.createTextNode(content))
|
||||
|
||||
return div
|
||||
|
||||
|
@ -75,6 +77,9 @@ class DynastieGenerator:
|
|||
new_elem = self.createElement(dom, node.localName, values[node.localName])
|
||||
elem.appendChild(new_elem)
|
||||
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
|
||||
from xml.dom.minidom import parse
|
||||
from xml.dom.minidom import parse, parseString
|
||||
from dynastie.generators.generator import DynastieGenerator
|
||||
from django.db import models
|
||||
|
||||
# TODO : links between indexes
|
||||
# TODO : gzip
|
||||
# TODO : content
|
||||
|
||||
class Index(DynastieGenerator):
|
||||
|
||||
cur_page = 0
|
||||
nb_pages = 0
|
||||
cur_article = 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):
|
||||
values = {}
|
||||
values['title'] = article.title
|
||||
|
@ -25,23 +59,32 @@ class Index(DynastieGenerator):
|
|||
for node in root.childNodes:
|
||||
if node.prefix == 'dyn':
|
||||
if node.localName == 'articles':
|
||||
articles_elem = dom.createElement("articles")
|
||||
articles_elem = self.createElement(dom, 'articles')
|
||||
for i in range(0, self.articles_per_page):
|
||||
article_elem = dom.createElementNS(self.URI, "article")
|
||||
self.createArticle(articles[self.cur_article], dom, article_elem, node)
|
||||
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
|
||||
|
||||
def generate(self, blog, src, output):
|
||||
from dynastie.models import *
|
||||
from dynastie.models import Article, Blog
|
||||
|
||||
if not os.path.exists(src + '/_index.html'):
|
||||
self.addError('No _index.html found, exiting')
|
||||
|
@ -67,13 +110,20 @@ class Index(DynastieGenerator):
|
|||
if articles.count() < 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'
|
||||
while self.cur_article < articles.count():
|
||||
while self.cur_page <= self.nb_pages:
|
||||
#print 'Generate ' + filename
|
||||
nodes = dom.getElementsByTagName("*")
|
||||
self.parse(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'
|
||||
dom = parse(src + '/_index.html')
|
||||
|
||||
filename = output + '/' + filename
|
||||
|
||||
while os.path.exists(filename):
|
||||
self.addReport('Removing unused ' + filename)
|
||||
|
@ -83,7 +133,7 @@ class Index(DynastieGenerator):
|
|||
self.addReport('Removing unused ' + filename)
|
||||
os.unlink(filename)
|
||||
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
|
||||
|
||||
|
|
10
models.py
10
models.py
|
@ -2,6 +2,7 @@ import os
|
|||
import shutil
|
||||
import hashlib
|
||||
import inspect
|
||||
from datetime import datetime
|
||||
from django.db import models
|
||||
from django.contrib.auth.models import User
|
||||
from django.db.models.signals import post_init, post_delete, post_save
|
||||
|
@ -19,6 +20,7 @@ class Blog(models.Model):
|
|||
|
||||
src_path = ''
|
||||
output_path = ''
|
||||
report = ''
|
||||
|
||||
def create_paths(self):
|
||||
self.src_path = 'sites/' + self.name
|
||||
|
@ -80,8 +82,10 @@ class Blog(models.Model):
|
|||
f.close()
|
||||
|
||||
if src_md5.digest() != dst_md5.digest():
|
||||
self.report = self.report + 'Update ' + dstname + '<br/>\n'
|
||||
shutil.copy2(srcname, dstname)
|
||||
else:
|
||||
self.report = self.report + 'Add ' + dstname + '<br/>\n'
|
||||
shutil.copy2(srcname, dstname)
|
||||
|
||||
# XXX What about devices, sockets etc.?
|
||||
|
@ -95,17 +99,17 @@ class Blog(models.Model):
|
|||
raise Exception(errors)
|
||||
|
||||
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.copytree(self.src_path, self.output_path)
|
||||
report = ''
|
||||
for engine in self.engines:
|
||||
for name, obj in inspect.getmembers(engine):
|
||||
if inspect.isclass(obj):
|
||||
e = obj()
|
||||
r = e.generate(self, self.src_path, self.output_path)
|
||||
if not r is None:
|
||||
report = report + '<br/>\n' + r
|
||||
return report
|
||||
self.report = self.report + '<br/>\n' + r
|
||||
return self.report
|
||||
|
||||
class Editor(models.Model):
|
||||
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
|
||||
def generate(request, blog_id):
|
||||
if not request.user.is_superuser:
|
||||
#b = Blog.objects.filter(id=blog_id).filter(writers=request.user.id)
|
||||
b = Blog.objects.get(pk=blog_id)
|
||||
b = Blog.objects.filter(id=blog_id).filter(writers=request.user.id)[0]
|
||||
else:
|
||||
b = Blog.objects.get(pk=blog_id)
|
||||
|
||||
|
@ -327,4 +326,10 @@ def generate(request, blog_id):
|
|||
b.create_paths()
|
||||
report = b.generate()
|
||||
|
||||
return render(request, 'generate.html', {'report': report})
|
||||
articles = Article.objects.filter(blog=b)
|
||||
b = Blog.objects.get(pk=blog_id)
|
||||
form = BlogForm(instance=b)
|
||||
|
||||
c = {'blog' : b, 'articles' : articles, 'form' : form, 'report': report}
|
||||
|
||||
return render(request, 'generate.html', c)
|
||||
|
|
Loading…
Reference in New Issue
Block a user