diff --git a/generators/post.py b/generators/post.py index 03010ac..5d39d79 100644 --- a/generators/post.py +++ b/generators/post.py @@ -4,6 +4,7 @@ from xml.dom.minidom import parse, parseString from dynastie.generators.generator import DynastieGenerator, StrictUTF8Writer from dynastie.generators.index import Index from django.db import models +from dynastie.tree import TreeNode class Post(Index): @@ -21,14 +22,14 @@ class Post(Index): 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_index', '0') + else: + value = value.replace('dyn:comment_index', str(self.comment_index[self.cur_comment.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)) + value = value.replace('dyn:comment_id', str(self.cur_comment.id)) div_element.setAttribute(key, value) @@ -37,13 +38,24 @@ class Post(Index): def createComment(self, comment, dom, comment_elem, root): values = {} - values['comment_id'] = str(comment.id) + values['comment_index'] = str(self.comment_index[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, rootNode, post, dom, root_comment, root): + self.cur_comment = rootNode.value + comment_element = self.createElement(dom, 'comment') + self.createComment(self.cur_comment, dom, comment_element, root) + root_comment.appendChild(comment_element) + # Parse inner HTML + self._parse(self.hooks, post, dom, comment_element) + + for commentNode in rootNode.childs: + self._createComments(commentNode, post, dom, comment_element, root) + def createComments(self, post, dom, post_elem, root): from dynastie.models import Post, Blog, Comment @@ -51,35 +63,29 @@ class Post(Index): cur_comment = None comment_index = {} - - comment_list_list = [] index = 1 + + rootNode = TreeNode('', '') for comment in comments: self.comment_index[comment.id] = index index = index + 1 - try: - if comment.parent is None: - comment_list_list.append([comment]) + tnode = TreeNode(comment.id, comment) + if comment.parent is None: + rootNode.addChildNode(tnode) + else: + temp = rootNode.find(comment.parent.id) + if temp is None: + self.addWarning('Error with comments chain') + rootNode.addChildNode(tnode) else: - for comment_list in comment_list_list: - if comment_list[0].id == comment.parent.id: - comment_list.append(comment) - break - except Comment.DoesNotExist: - comment_list_list.append([comment]) - + temp.addChildNode(tnode) + initial_root_comment = root_comment = self.createElement(dom, 'comments') - for comment_list in comment_list_list: - for comment in comment_list: - self.cur_comment = comment - comment_element = self.createElement(dom, 'comment') - self.createComment(comment, dom, comment_element, root) - root_comment.appendChild(comment_element) - root_comment = comment_element - root_comment = initial_root_comment + for tnode in rootNode.childs: + self._createComments(tnode, post, dom, root_comment, root) # Empty tag seems to crap rendering - if len(comment_list_list) == 0: + if len(rootNode.childs) == 0: post_elem.removeChild(root) return None else: @@ -131,7 +137,7 @@ class Post(Index): def _generate(self, blog, src, output, posts): import xml - hooks = {'post' : self._createPost, + self.hooks = {'post' : self._createPost, 'meta' : self.createMetas, 'comments' : self.createComments, 'replace' : self.createReplace} @@ -152,7 +158,7 @@ class Post(Index): for post in posts: #print 'Generate ' + filename nodes = dom.getElementsByTagName("*") - nodes[0] = self.parse(src, hooks, post, dom, nodes[0]) + nodes[0] = self.parse(src, self.hooks, post, dom, nodes[0]) filename = output + '/post/' filename = filename + post.creation_date.strftime("%Y") + '/' + post.creation_date.strftime("%m") + '/' @@ -204,7 +210,7 @@ class Post(Index): def preview(self, src, values): from dynastie.models import Blog - hooks = {'post' : self.createPreview} + self.hooks = {'post' : self.createPreview} if not os.path.exists(src + '/_post.html'): self.addError('No _post.html found, exiting') @@ -223,7 +229,7 @@ class Post(Index): return self.report nodes = dom.getElementsByTagName("*") - nodes[0] = self.parse(src, hooks, values, dom, nodes[0]) + nodes[0] = self.parse(src, self.hooks, values, dom, nodes[0]) writer = StrictUTF8Writer() nodes[0].writexml(writer) diff --git a/models.py b/models.py index 9f610d1..6e067c1 100644 --- a/models.py +++ b/models.py @@ -141,7 +141,7 @@ class Blog(models.Model): if not inspect.ismodule(engine): continue for name, obj in inspect.getmembers(engine): - if inspect.isclass(obj) and obj.__module__.startswith("dynastie"): + if inspect.isclass(obj) and obj.__module__.startswith("dynastie.generators"): if obj.__module__ in generated: continue e = obj() r = e.generate(self, self.src_path, self.output_path) @@ -264,6 +264,7 @@ class Comment(models.Model): author = models.CharField(max_length=255) email = models.EmailField(max_length=255, blank=True) the_comment = models.TextField(max_length=255) + ip = models.GenericIPAddressField() @receiver(post_init, sender=Blog) def delete_blog_signal(sender, **kwargs): diff --git a/templates/edit_post.html b/templates/edit_post.html index f6f5037..7fe89ee 100644 --- a/templates/edit_post.html +++ b/templates/edit_post.html @@ -15,8 +15,7 @@
' % (blog.name, post.getPath(), comment_index, blog.name, post.getPath(), comment_index) + c = comment.the_comment + # Avoid script injection + c = c.replace('' + html_body += '' + + msg = EmailMultiAlternatives(subject, text_body, 'no-reply@%s' % blog.name , [email]) + msg.attach_alternative(html_body, "text/html") + messages.append(msg) + + connection.send_messages(messages) + connection.close() + + response = HttpResponseRedirect(ref) + + response.set_cookie('author', request.POST['author'], domain=blog.name, secure=True, httponly=False); + if request.POST['email'] != '': + response.set_cookie('email', request.POST['email'], domain=blog.name, secure=True, httponly=False); + + return response', '<pre>') + c = c.replace('', '</pre>') + html_body += c + '