Finalize comments
This commit is contained in:
parent
3c3070cf31
commit
b87a78df59
|
@ -4,6 +4,7 @@ from xml.dom.minidom import parse, parseString
|
||||||
from dynastie.generators.generator import DynastieGenerator, StrictUTF8Writer
|
from dynastie.generators.generator import DynastieGenerator, StrictUTF8Writer
|
||||||
from dynastie.generators.index import Index
|
from dynastie.generators.index import Index
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from dynastie.tree import TreeNode
|
||||||
|
|
||||||
class Post(Index):
|
class Post(Index):
|
||||||
|
|
||||||
|
@ -21,14 +22,14 @@ class Post(Index):
|
||||||
for key,value in replace_elem.attributes.items():
|
for key,value in replace_elem.attributes.items():
|
||||||
if key == 'div_name': continue
|
if key == 'div_name': continue
|
||||||
value = value.replace('dyn:post_id', str(post.id))
|
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:
|
if self.cur_comment is None:
|
||||||
value = value.replace('dyn:comment_id', '0')
|
value = value.replace('dyn:comment_id', '0')
|
||||||
else:
|
else:
|
||||||
value = value.replace('dyn:comment_id', str(self.comment_index[self.cur_comment.id]))
|
value = value.replace('dyn:comment_id', str(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)
|
div_element.setAttribute(key, value)
|
||||||
|
|
||||||
|
@ -37,13 +38,24 @@ class Post(Index):
|
||||||
|
|
||||||
def createComment(self, comment, dom, comment_elem, root):
|
def createComment(self, comment, dom, comment_elem, root):
|
||||||
values = {}
|
values = {}
|
||||||
values['comment_id'] = str(comment.id)
|
values['comment_index'] = str(self.comment_index[comment.id])
|
||||||
values['comment_author'] = comment.author
|
values['comment_author'] = comment.author
|
||||||
values['comment_date'] = comment.date.strftime('%d %B %Y %H:%m')
|
values['comment_date'] = comment.date.strftime('%d %B %Y %H:%m')
|
||||||
values['comment_content'] = comment.the_comment
|
values['comment_content'] = comment.the_comment
|
||||||
|
|
||||||
self.simpleTransform(values, dom, comment_elem, root)
|
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):
|
def createComments(self, post, dom, post_elem, root):
|
||||||
from dynastie.models import Post, Blog, Comment
|
from dynastie.models import Post, Blog, Comment
|
||||||
|
|
||||||
|
@ -51,35 +63,29 @@ class Post(Index):
|
||||||
|
|
||||||
cur_comment = None
|
cur_comment = None
|
||||||
comment_index = {}
|
comment_index = {}
|
||||||
|
|
||||||
comment_list_list = []
|
|
||||||
index = 1
|
index = 1
|
||||||
|
|
||||||
|
rootNode = TreeNode('', '')
|
||||||
for comment in comments:
|
for comment in comments:
|
||||||
self.comment_index[comment.id] = index
|
self.comment_index[comment.id] = index
|
||||||
index = index + 1
|
index = index + 1
|
||||||
try:
|
tnode = TreeNode(comment.id, comment)
|
||||||
if comment.parent is None:
|
if comment.parent is None:
|
||||||
comment_list_list.append([comment])
|
rootNode.addChildNode(tnode)
|
||||||
|
else:
|
||||||
|
temp = rootNode.find(comment.parent.id)
|
||||||
|
if temp is None:
|
||||||
|
self.addWarning('Error with comments chain')
|
||||||
|
rootNode.addChildNode(tnode)
|
||||||
else:
|
else:
|
||||||
for comment_list in comment_list_list:
|
temp.addChildNode(tnode)
|
||||||
if comment_list[0].id == comment.parent.id:
|
|
||||||
comment_list.append(comment)
|
|
||||||
break
|
|
||||||
except Comment.DoesNotExist:
|
|
||||||
comment_list_list.append([comment])
|
|
||||||
|
|
||||||
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 tnode in rootNode.childs:
|
||||||
for comment in comment_list:
|
self._createComments(tnode, post, dom, root_comment, root)
|
||||||
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
|
|
||||||
|
|
||||||
# Empty tag seems to crap rendering
|
# Empty tag seems to crap rendering
|
||||||
if len(comment_list_list) == 0:
|
if len(rootNode.childs) == 0:
|
||||||
post_elem.removeChild(root)
|
post_elem.removeChild(root)
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
|
@ -131,7 +137,7 @@ class Post(Index):
|
||||||
|
|
||||||
def _generate(self, blog, src, output, posts):
|
def _generate(self, blog, src, output, posts):
|
||||||
import xml
|
import xml
|
||||||
hooks = {'post' : self._createPost,
|
self.hooks = {'post' : self._createPost,
|
||||||
'meta' : self.createMetas,
|
'meta' : self.createMetas,
|
||||||
'comments' : self.createComments,
|
'comments' : self.createComments,
|
||||||
'replace' : self.createReplace}
|
'replace' : self.createReplace}
|
||||||
|
@ -152,7 +158,7 @@ class Post(Index):
|
||||||
for post in posts:
|
for post in posts:
|
||||||
#print 'Generate ' + filename
|
#print 'Generate ' + filename
|
||||||
nodes = dom.getElementsByTagName("*")
|
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 = output + '/post/'
|
||||||
filename = filename + post.creation_date.strftime("%Y") + '/' + post.creation_date.strftime("%m") + '/'
|
filename = filename + post.creation_date.strftime("%Y") + '/' + post.creation_date.strftime("%m") + '/'
|
||||||
|
@ -204,7 +210,7 @@ class Post(Index):
|
||||||
def preview(self, src, values):
|
def preview(self, src, values):
|
||||||
from dynastie.models import Blog
|
from dynastie.models import Blog
|
||||||
|
|
||||||
hooks = {'post' : self.createPreview}
|
self.hooks = {'post' : self.createPreview}
|
||||||
|
|
||||||
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')
|
||||||
|
@ -223,7 +229,7 @@ class Post(Index):
|
||||||
return self.report
|
return self.report
|
||||||
|
|
||||||
nodes = dom.getElementsByTagName("*")
|
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()
|
writer = StrictUTF8Writer()
|
||||||
nodes[0].writexml(writer)
|
nodes[0].writexml(writer)
|
||||||
|
|
||||||
|
|
|
@ -141,7 +141,7 @@ class Blog(models.Model):
|
||||||
if not inspect.ismodule(engine):
|
if not inspect.ismodule(engine):
|
||||||
continue
|
continue
|
||||||
for name, obj in inspect.getmembers(engine):
|
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
|
if obj.__module__ in generated: continue
|
||||||
e = obj()
|
e = obj()
|
||||||
r = e.generate(self, self.src_path, self.output_path)
|
r = e.generate(self, self.src_path, self.output_path)
|
||||||
|
@ -264,6 +264,7 @@ class Comment(models.Model):
|
||||||
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)
|
||||||
the_comment = models.TextField(max_length=255)
|
the_comment = models.TextField(max_length=255)
|
||||||
|
ip = models.GenericIPAddressField()
|
||||||
|
|
||||||
@receiver(post_init, sender=Blog)
|
@receiver(post_init, sender=Blog)
|
||||||
def delete_blog_signal(sender, **kwargs):
|
def delete_blog_signal(sender, **kwargs):
|
||||||
|
|
|
@ -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" />
|
<input type="submit" name="edit" value="Edit" /><input type="button" name="preview" value="Preview" onClick="previewPost();"/><input type="submit" name="cancel" value="Cancel" />
|
||||||
</form>
|
</form>
|
||||||
<div class="comments">
|
<div class="comments">
|
||||||
{% for comment_list in comments %}
|
{% for comment in comments %}
|
||||||
{% for comment in comment_list %}
|
|
||||||
<div class="comment">
|
<div class="comment">
|
||||||
<div class="infos">
|
<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>
|
<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 }}
|
{{ comment.the_comment }}
|
||||||
</div>
|
</div>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
{% for comment in comment_list %}
|
|
||||||
</div>
|
|
||||||
{% endfor %}
|
|
||||||
</div>
|
</div>
|
||||||
{% endfor %}
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
21
tree.py
Normal file
21
tree.py
Normal 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
|
73
views.py
73
views.py
|
@ -1,3 +1,4 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
import os
|
import os
|
||||||
from datetime import datetime, date, time
|
from datetime import datetime, date, time
|
||||||
from django.shortcuts import render
|
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.contrib.auth.decorators import login_required
|
||||||
from django.views.decorators.csrf import csrf_exempt
|
from django.views.decorators.csrf import csrf_exempt
|
||||||
from django.forms.models import inlineformset_factory
|
from django.forms.models import inlineformset_factory
|
||||||
|
from django.core import mail
|
||||||
|
from django.core.mail import EmailMultiAlternatives
|
||||||
|
|
||||||
from dynastie.models import *
|
from dynastie.models import *
|
||||||
from dynastie.forms import *
|
from dynastie.forms import *
|
||||||
|
|
||||||
|
@ -62,7 +66,6 @@ def index(request):
|
||||||
login_failed = True
|
login_failed = True
|
||||||
else:
|
else:
|
||||||
login(request, user)
|
login(request, user)
|
||||||
print request.GET
|
|
||||||
if 'next' in request.GET:
|
if 'next' in request.GET:
|
||||||
return HttpResponseRedirect(request.GET['next'])
|
return HttpResponseRedirect(request.GET['next'])
|
||||||
else:
|
else:
|
||||||
|
@ -367,24 +370,13 @@ def edit_post(request, post_id):
|
||||||
content = 'Empty post'
|
content = 'Empty post'
|
||||||
|
|
||||||
comments = Comment.objects.filter(post=post).order_by('date')
|
comments = Comment.objects.filter(post=post).order_by('date')
|
||||||
|
comment_list = []
|
||||||
comment_list_list = []
|
|
||||||
for comment in comments:
|
for comment in comments:
|
||||||
print comment.date
|
comment_list.append(comment)
|
||||||
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])
|
|
||||||
|
|
||||||
return render(request, 'edit_post.html', {
|
return render(request, 'edit_post.html', {
|
||||||
'form': form, 'post_id' : post_id, 'content' : content,
|
'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
|
@login_required
|
||||||
|
@ -437,7 +429,7 @@ def preview(request, blog_id):
|
||||||
engine = globals()['post']
|
engine = globals()['post']
|
||||||
|
|
||||||
for name, obj in inspect.getmembers(engine):
|
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"):
|
and obj.__module__.endswith("post"):
|
||||||
e = obj()
|
e = obj()
|
||||||
content = e.preview(b.src_path, values)
|
content = e.preview(b.src_path, values)
|
||||||
|
@ -542,7 +534,7 @@ def add_comment(request, post_id, parent_id):
|
||||||
return HttpResponseRedirect(ref)
|
return HttpResponseRedirect(ref)
|
||||||
|
|
||||||
comment = Comment(post=post, parent=parentComment, date=datetime.now(), author=request.POST['author'],\
|
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()
|
comment.save()
|
||||||
|
|
||||||
engine = globals()['post']
|
engine = globals()['post']
|
||||||
|
@ -550,15 +542,58 @@ def add_comment(request, post_id, parent_id):
|
||||||
|
|
||||||
post_list = [post]
|
post_list = [post]
|
||||||
for name, obj in inspect.getmembers(engine):
|
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"):
|
and obj.__module__.endswith("post"):
|
||||||
e = obj()
|
e = obj()
|
||||||
content = e._generate(blog, blog.src_path, blog.output_path, post_list)
|
content = e._generate(blog, blog.src_path, blog.output_path, post_list)
|
||||||
break
|
break
|
||||||
|
|
||||||
# Send emails
|
# 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>', '<pre>')
|
||||||
|
c = c.replace('</pre>', '</pre>')
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user