Need to return objects in generator hooks
Add dyn:comments and dyn:replace support
This commit is contained in:
parent
01685868e2
commit
3c3070cf31
|
@ -19,6 +19,8 @@ class Archive(Index):
|
||||||
if node.hasAttribute('year'):
|
if node.hasAttribute('year'):
|
||||||
self.replaceByText(dom, root, node, str(self.cur_year))
|
self.replaceByText(dom, root, node, str(self.cur_year))
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
def createArchives(self, src, output, dom, hooks, posts):
|
def createArchives(self, src, output, dom, hooks, posts):
|
||||||
filename = self.filename + '.html'
|
filename = self.filename + '.html'
|
||||||
self.nb_pages = 0
|
self.nb_pages = 0
|
||||||
|
|
|
@ -19,6 +19,7 @@ class Category(Index):
|
||||||
self.replaceByText(dom, root, node, self.cur_category.name)
|
self.replaceByText(dom, root, node, self.cur_category.name)
|
||||||
if node.hasAttribute('description'):
|
if node.hasAttribute('description'):
|
||||||
self.replaceByText(dom, root, node, self.cur_category.description)
|
self.replaceByText(dom, root, node, self.cur_category.description)
|
||||||
|
return None
|
||||||
|
|
||||||
def generate(self, blog, src, output):
|
def generate(self, blog, src, output):
|
||||||
from dynastie.models import Post, Blog, Category
|
from dynastie.models import Post, Blog, Category
|
||||||
|
|
|
@ -141,14 +141,13 @@ class DynastieGenerator:
|
||||||
else:
|
else:
|
||||||
new_elem = self.createElement(dom, node.localName)
|
new_elem = self.createElement(dom, node.localName)
|
||||||
new_elem.appendChild(content)
|
new_elem.appendChild(content)
|
||||||
elem.appendChild(new_elem)
|
|
||||||
else:
|
else:
|
||||||
new_elem = node.cloneNode(True)
|
new_elem = node.cloneNode(False)
|
||||||
elem.appendChild(new_elem)
|
self.simpleTransform(values, dom, new_elem, node)
|
||||||
else:
|
else:
|
||||||
new_elem = node.cloneNode(False)
|
new_elem = node.cloneNode(False)
|
||||||
self.simpleTransform(values, dom, new_elem, node)
|
self.simpleTransform(values, dom, new_elem, node)
|
||||||
elem.appendChild(new_elem)
|
elem.appendChild(new_elem)
|
||||||
|
|
||||||
def replaceByText(self, dom, root, node, content):
|
def replaceByText(self, dom, root, node, content):
|
||||||
new_node = dom.createTextNode(content)
|
new_node = dom.createTextNode(content)
|
||||||
|
@ -158,10 +157,9 @@ class DynastieGenerator:
|
||||||
for node in root.childNodes:
|
for node in root.childNodes:
|
||||||
if node.prefix == 'dyn':
|
if node.prefix == 'dyn':
|
||||||
if node.localName in hooks:
|
if node.localName in hooks:
|
||||||
hooks[node.localName](posts, dom, root, node)
|
node = hooks[node.localName](posts, dom, root, node)
|
||||||
if node.hasChildNodes():
|
if not node is None and node.hasChildNodes():
|
||||||
self._parse(hooks, posts, dom, node)
|
self._parse(hooks, posts, dom, node)
|
||||||
return
|
|
||||||
|
|
||||||
def parse(self, src, hooks, posts, dom, root):
|
def parse(self, src, hooks, posts, dom, root):
|
||||||
bases = dom.getElementsByTagNameNS(self.URI, 'base')
|
bases = dom.getElementsByTagNameNS(self.URI, 'base')
|
||||||
|
|
|
@ -17,7 +17,7 @@ class Index(DynastieGenerator):
|
||||||
|
|
||||||
def createNavigation(self, posts, dom, root, node):
|
def createNavigation(self, posts, dom, root, node):
|
||||||
if self.nb_pages == 0 or self.nb_pages == 1:
|
if self.nb_pages == 0 or self.nb_pages == 1:
|
||||||
return ''
|
return None
|
||||||
|
|
||||||
if self.dirname != '':
|
if self.dirname != '':
|
||||||
if self.dirname.startswith('/'):
|
if self.dirname.startswith('/'):
|
||||||
|
@ -58,7 +58,10 @@ class Index(DynastieGenerator):
|
||||||
|
|
||||||
new_dom = parseString('<div class="navigation">' + nav + '</div>')
|
new_dom = parseString('<div class="navigation">' + nav + '</div>')
|
||||||
new_node = new_dom.getElementsByTagName('div')[0]
|
new_node = new_dom.getElementsByTagName('div')[0]
|
||||||
root.replaceChild(new_node.cloneNode(True), node)
|
res = new_node.cloneNode(True)
|
||||||
|
root.replaceChild(res, node)
|
||||||
|
|
||||||
|
return res
|
||||||
|
|
||||||
def createPost(self, post, dom, post_elem, root):
|
def createPost(self, post, dom, post_elem, root):
|
||||||
values = {}
|
values = {}
|
||||||
|
@ -106,6 +109,7 @@ class Index(DynastieGenerator):
|
||||||
if self.cur_post == len(posts):
|
if self.cur_post == len(posts):
|
||||||
break
|
break
|
||||||
root.replaceChild(posts_elem, node)
|
root.replaceChild(posts_elem, node)
|
||||||
|
return posts_elem
|
||||||
|
|
||||||
def createRecents(self, posts, dom, root, node):
|
def createRecents(self, posts, dom, root, node):
|
||||||
if self.cur_post == len(posts):
|
if self.cur_post == len(posts):
|
||||||
|
@ -133,6 +137,8 @@ class Index(DynastieGenerator):
|
||||||
recents_elem.appendChild(list_elem)
|
recents_elem.appendChild(list_elem)
|
||||||
root.replaceChild(recents_elem, node)
|
root.replaceChild(recents_elem, node)
|
||||||
|
|
||||||
|
return recents_elem
|
||||||
|
|
||||||
def generate(self, blog, src, output):
|
def generate(self, blog, src, output):
|
||||||
from dynastie.models import Post, Blog
|
from dynastie.models import Post, Blog
|
||||||
|
|
||||||
|
|
|
@ -7,22 +7,62 @@ from django.db import models
|
||||||
|
|
||||||
class Post(Index):
|
class Post(Index):
|
||||||
|
|
||||||
|
cur_comment = None
|
||||||
|
comment_index = {}
|
||||||
|
|
||||||
|
def createReplace(self, post, dom, root, replace_elem):
|
||||||
|
if not replace_elem.hasAttribute('div_name'):
|
||||||
|
self.addError('No attribute div_name for a replace tag')
|
||||||
|
return
|
||||||
|
|
||||||
|
div_element = replace_elem.cloneNode(True)
|
||||||
|
div_element.tagName = replace_elem.getAttribute('div_name')
|
||||||
|
div_element.removeAttribute('div_name')
|
||||||
|
for key,value in replace_elem.attributes.items():
|
||||||
|
if key == 'div_name': continue
|
||||||
|
value = value.replace('dyn:post_id', str(post.id))
|
||||||
|
if self.cur_comment is None:
|
||||||
|
value = value.replace('dyn:comment_id', '0')
|
||||||
|
else:
|
||||||
|
value = value.replace('dyn:comment_id', str(self.comment_index[self.cur_comment.id]))
|
||||||
|
if self.cur_comment is None or self.cur_comment.parent is None:
|
||||||
|
value = value.replace('dyn:comment_parent_id', '0')
|
||||||
|
else:
|
||||||
|
value = value.replace('dyn:comment_parent_id', str(self.cur_comment.id))
|
||||||
|
|
||||||
|
div_element.setAttribute(key, value)
|
||||||
|
|
||||||
|
root.replaceChild(div_element, replace_elem)
|
||||||
|
return div_element
|
||||||
|
|
||||||
|
def createComment(self, comment, dom, comment_elem, root):
|
||||||
|
values = {}
|
||||||
|
values['comment_id'] = str(comment.id)
|
||||||
|
values['comment_author'] = comment.author
|
||||||
|
values['comment_date'] = comment.date.strftime('%d %B %Y %H:%m')
|
||||||
|
values['comment_content'] = comment.the_comment
|
||||||
|
|
||||||
|
self.simpleTransform(values, dom, comment_elem, root)
|
||||||
|
|
||||||
def createComments(self, post, dom, post_elem, root):
|
def createComments(self, post, dom, post_elem, root):
|
||||||
from dynastie.models import Post, Blog, Comment
|
from dynastie.models import Post, Blog, Comment
|
||||||
|
|
||||||
base_url = root.getAttribute('base_url')
|
comments = Comment.objects.filter(post=post).order_by('date')
|
||||||
add_comment = (root.getAttribute('add_comment') == '1')
|
|
||||||
|
|
||||||
comments = Comment.objects.filter(post=post)
|
cur_comment = None
|
||||||
|
comment_index = {}
|
||||||
|
|
||||||
comment_list_list = []
|
comment_list_list = []
|
||||||
|
index = 1
|
||||||
for comment in comments:
|
for comment in comments:
|
||||||
|
self.comment_index[comment.id] = index
|
||||||
|
index = index + 1
|
||||||
try:
|
try:
|
||||||
if comment.parent_id == 0:
|
if comment.parent is None:
|
||||||
comment_list_list.append([comment])
|
comment_list_list.append([comment])
|
||||||
else:
|
else:
|
||||||
for comment_list in comment_list_list:
|
for comment_list in comment_list_list:
|
||||||
if comment_list[0].id == comment.parent_id:
|
if comment_list[0].id == comment.parent.id:
|
||||||
comment_list.append(comment)
|
comment_list.append(comment)
|
||||||
break
|
break
|
||||||
except Comment.DoesNotExist:
|
except Comment.DoesNotExist:
|
||||||
|
@ -31,13 +71,20 @@ class Post(Index):
|
||||||
initial_root_comment = root_comment = self.createElement(dom, 'comments')
|
initial_root_comment = root_comment = self.createElement(dom, 'comments')
|
||||||
for comment_list in comment_list_list:
|
for comment_list in comment_list_list:
|
||||||
for comment in comment_list:
|
for comment in comment_list:
|
||||||
|
self.cur_comment = comment
|
||||||
comment_element = self.createElement(dom, 'comment')
|
comment_element = self.createElement(dom, 'comment')
|
||||||
comment_content = self.createElement(dom, 'comment_content', comment.the_comment)
|
self.createComment(comment, dom, comment_element, root)
|
||||||
comment_element.appendChild(comment_content)
|
|
||||||
root_comment.appendChild(comment_element)
|
root_comment.appendChild(comment_element)
|
||||||
root_comment = comment_element
|
root_comment = comment_element
|
||||||
root_comment = initial_root_comment
|
root_comment = initial_root_comment
|
||||||
post_elem.replaceChild(root_comment, root)
|
|
||||||
|
# Empty tag seems to crap rendering
|
||||||
|
if len(comment_list_list) == 0:
|
||||||
|
post_elem.removeChild(root)
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
post_elem.replaceChild(root_comment, root)
|
||||||
|
return root_comment
|
||||||
|
|
||||||
def createMetas(self, post, dom, meta_elem, root):
|
def createMetas(self, post, dom, meta_elem, root):
|
||||||
name = root.getAttribute('name')
|
name = root.getAttribute('name')
|
||||||
|
@ -57,14 +104,14 @@ class Post(Index):
|
||||||
|
|
||||||
if not new_elem is None:
|
if not new_elem is None:
|
||||||
root.parentNode.replaceChild(new_elem, root)
|
root.parentNode.replaceChild(new_elem, root)
|
||||||
|
return new_elem
|
||||||
else:
|
else:
|
||||||
self.addError('name attribute \'' + name + '\' unknown for dyn:meta' )
|
self.addError('name attribute \'' + name + '\' unknown for dyn:meta' )
|
||||||
|
return None
|
||||||
|
|
||||||
def _createPost(self, post, dom, post_elem, root):
|
def _createPost(self, post, dom, post_elem, root):
|
||||||
import sys, traceback
|
import sys, traceback
|
||||||
|
|
||||||
if post.id == 122:
|
|
||||||
print post_elem.toxml()
|
|
||||||
self.createPost(post, dom, post_elem, root)
|
self.createPost(post, dom, post_elem, root)
|
||||||
|
|
||||||
# Post are appended by index. Remove template
|
# Post are appended by index. Remove template
|
||||||
|
@ -80,11 +127,14 @@ class Post(Index):
|
||||||
node.removeChild(node.childNodes[0])
|
node.removeChild(node.childNodes[0])
|
||||||
node.appendChild(dom.createTextNode(post.title))
|
node.appendChild(dom.createTextNode(post.title))
|
||||||
|
|
||||||
|
return node
|
||||||
|
|
||||||
def _generate(self, blog, src, output, posts):
|
def _generate(self, blog, src, output, posts):
|
||||||
import xml
|
import xml
|
||||||
hooks = {'post' : self._createPost,
|
hooks = {'post' : self._createPost,
|
||||||
'meta' : self.createMetas,
|
'meta' : self.createMetas,
|
||||||
'comments' : self.createComments}
|
'comments' : self.createComments,
|
||||||
|
'replace' : self.createReplace}
|
||||||
|
|
||||||
if not os.path.exists(src + '/_post.html'):
|
if not os.path.exists(src + '/_post.html'):
|
||||||
self.addError('No _post.html found, exiting')
|
self.addError('No _post.html found, exiting')
|
||||||
|
@ -149,6 +199,8 @@ class Post(Index):
|
||||||
post_elem = post_nodes[0]
|
post_elem = post_nodes[0]
|
||||||
post_elem.parentNode.removeChild(post_elem)
|
post_elem.parentNode.removeChild(post_elem)
|
||||||
|
|
||||||
|
return post_elem
|
||||||
|
|
||||||
def preview(self, src, values):
|
def preview(self, src, values):
|
||||||
from dynastie.models import Blog
|
from dynastie.models import Blog
|
||||||
|
|
||||||
|
|
|
@ -259,7 +259,7 @@ class Post(models.Model):
|
||||||
|
|
||||||
class Comment(models.Model):
|
class Comment(models.Model):
|
||||||
post = models.ForeignKey(Post)
|
post = models.ForeignKey(Post)
|
||||||
parent = models.ForeignKey('self', null=True)
|
parent = models.ForeignKey('self', null=True, blank=True)
|
||||||
date = models.DateTimeField()
|
date = models.DateTimeField()
|
||||||
author = models.CharField(max_length=255)
|
author = models.CharField(max_length=255)
|
||||||
email = models.EmailField(max_length=255, blank=True)
|
email = models.EmailField(max_length=255, blank=True)
|
||||||
|
|
9
views.py
9
views.py
|
@ -599,10 +599,15 @@ def delete_comment(request, comment_id):
|
||||||
(b, post) = have_I_right(request, None, comment.post_id)
|
(b, post) = have_I_right(request, None, comment.post_id)
|
||||||
post_id = comment.post_id
|
post_id = comment.post_id
|
||||||
|
|
||||||
childs = Comment.objects.filter(parent=comment)
|
childs = Comment.objects.filter(parent=comment.id)
|
||||||
|
|
||||||
|
try:
|
||||||
|
parent = comment.parent
|
||||||
|
except:
|
||||||
|
parent = None
|
||||||
|
|
||||||
for child in childs:
|
for child in childs:
|
||||||
child.parent = comment.parent
|
child.parent = parent
|
||||||
child.save()
|
child.save()
|
||||||
|
|
||||||
comment.delete()
|
comment.delete()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user