Finalize comments

This commit is contained in:
Grégory Soutadé 2012-10-20 19:05:29 +02:00
parent 3c3070cf31
commit b87a78df59
5 changed files with 115 additions and 57 deletions

View File

@ -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)

View File

@ -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):

View File

@ -15,8 +15,7 @@
<input type="submit" name="edit" value="Edit" /><input type="button" name="preview" value="Preview" onClick="previewPost();"/><input type="submit" name="cancel" value="Cancel" />
</form>
<div class="comments">
{% for comment_list in comments %}
{% for comment in comment_list %}
{% for comment in comments %}
<div class="comment">
<div class="infos">
<a href="/comment/edit/{{ comment.id }}">#{{ comment.id }}</a> <span class="author">{{ comment.author }}</span> <span class="mail">{{ comment.email|default:"no mail" }}</span> <span class="date">{{ comment.date|date:"D d M Y" }}<span> <a href="/comment/delete/{{ comment.id }}">delete</a>
@ -25,9 +24,5 @@
{{ comment.the_comment }}
</div>
{% endfor %}
{% for comment in comment_list %}
</div>
{% endfor %}
</div>
{% endfor %}
{% endblock %}

21
tree.py Normal file
View File

@ -0,0 +1,21 @@
class TreeNode():
def __init__(self, key, value):
self.childs = []
self.key = key
self.value = value
def addChildNode(self, node):
self.childs.append(node)
def hasChilds(self):
return (len(self.childs) > 0)
def find(self, key):
if key == self.key: return self
for child in self.childs:
r = child.find(key)
if r != None: return r
return None

View File

@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
import os
from datetime import datetime, date, time
from django.shortcuts import render
@ -6,6 +7,9 @@ from django.http import HttpResponseRedirect, HttpResponse, Http404
from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import csrf_exempt
from django.forms.models import inlineformset_factory
from django.core import mail
from django.core.mail import EmailMultiAlternatives
from dynastie.models import *
from dynastie.forms import *
@ -62,7 +66,6 @@ def index(request):
login_failed = True
else:
login(request, user)
print request.GET
if 'next' in request.GET:
return HttpResponseRedirect(request.GET['next'])
else:
@ -367,24 +370,13 @@ def edit_post(request, post_id):
content = 'Empty post'
comments = Comment.objects.filter(post=post).order_by('date')
comment_list_list = []
comment_list = []
for comment in comments:
print comment.date
try:
if comment.parent.id is None:
comment_list_list.append([comment])
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])
comment_list.append(comment)
return render(request, 'edit_post.html', {
'form': form, 'post_id' : post_id, 'content' : content,
'blog_id' : blog_id, 'comments' : comment_list_list
'blog_id' : blog_id, 'comments' : comment_list
})
@login_required
@ -437,7 +429,7 @@ def preview(request, blog_id):
engine = globals()['post']
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") \
and obj.__module__.endswith("post"):
e = obj()
content = e.preview(b.src_path, values)
@ -542,7 +534,7 @@ def add_comment(request, post_id, parent_id):
return HttpResponseRedirect(ref)
comment = Comment(post=post, parent=parentComment, date=datetime.now(), author=request.POST['author'],\
email=request.POST['email'], the_comment=request.POST['the_comment'])
email=request.POST['email'], the_comment=request.POST['the_comment'], ip=request.META['REMOTE_ADDR'])
comment.save()
engine = globals()['post']
@ -550,15 +542,58 @@ def add_comment(request, post_id, parent_id):
post_list = [post]
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") \
and obj.__module__.endswith("post"):
e = obj()
content = e._generate(blog, blog.src_path, blog.output_path, post_list)
break
# Send emails
emails = {}
comments = Comment.objects.filter(post=post).order_by('date')
comment_index = str(len(comments))
for comment in comments:
email = comment.email
if email != '' and not email in emails:
emails[email] = comment.author
return HttpResponseRedirect(ref)
if len(emails) > 0:
connection = mail.get_connection(fail_silently=True)
connection.open()
messages = []
subject = '[%s] Nouveau commentaire pour l\'article "%s"' % (blog.name, post.title)
for email,author in emails.items():
text_body = u'Bonjour %s,\n\nUn nouveau commentaire a été posté pour l\'article "%s".\n\n' % (author, post.title)
text_body += u'Pour le consulter, rendez vous sur http://%s%s/#comment_%s\n\n----------------\n\n' % (blog.name, post.getPath(), comment_index)
text_body += comment.the_comment
text_body += '\n'
html_body = u'<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8" /></head><body>'
html_body += u'Bonjour %s,<br/><br/>Un nouveau commentaire a été posté pour l\'article "%s".<br/><br/>' % (author, post.title)
html_body = html_body + u'Pour le consulter, rendez vous sur <a href="http://%s%s#comment_%s">http://%s/%s#comment_%s</a><br/><br/>----------------<br/><pre>' % (blog.name, post.getPath(), comment_index, blog.name, post.getPath(), comment_index)
c = comment.the_comment
# Avoid script injection
c = c.replace('<pre>', '&lt;pre&gt;')
c = c.replace('</pre>', '&lt;/pre&gt;')
html_body += c + '</pre>'
html_body += '</body></html>'
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