Fix some bugs + add primary comment support
This commit is contained in:
parent
76cda68611
commit
01685868e2
|
@ -142,6 +142,9 @@ class DynastieGenerator:
|
||||||
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)
|
elem.appendChild(new_elem)
|
||||||
|
else:
|
||||||
|
new_elem = node.cloneNode(True)
|
||||||
|
elem.appendChild(new_elem)
|
||||||
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)
|
||||||
|
|
|
@ -64,7 +64,7 @@ class Index(DynastieGenerator):
|
||||||
values = {}
|
values = {}
|
||||||
values['title'] = self.createLinkElem(dom, post.getPath(), post.title)
|
values['title'] = self.createLinkElem(dom, post.getPath(), post.title)
|
||||||
values['author'] = post.author.first_name + ' ' + post.author.last_name
|
values['author'] = post.author.first_name + ' ' + post.author.last_name
|
||||||
values['date'] = post.creation_date.strftime("%A, %d %B %Y %H:%m")
|
values['date'] = post.creation_date.strftime('%A, %d %B %Y %H:%m')
|
||||||
values['post_content'] = ''
|
values['post_content'] = ''
|
||||||
|
|
||||||
blog = post.blog
|
blog = post.blog
|
||||||
|
@ -82,16 +82,16 @@ class Index(DynastieGenerator):
|
||||||
|
|
||||||
self.simpleTransform(values, dom, post_elem, root)
|
self.simpleTransform(values, dom, post_elem, root)
|
||||||
|
|
||||||
content_nodes = post_elem.getElementsByTagName("div")
|
content_nodes = post_elem.getElementsByTagName('div')
|
||||||
post_transform = ('post_content')
|
post_transform = ['post_content']
|
||||||
for content_node in content_nodes:
|
for content_node in content_nodes:
|
||||||
the_class = content_node.getAttribute('class')
|
the_class = content_node.getAttribute('class')
|
||||||
if not the_class in post_transform:
|
if not the_class in post_transform:
|
||||||
continue
|
continue
|
||||||
if the_class == 'post_content':
|
|
||||||
new_node = dom.createTextNode(post_content)
|
new_node = dom.createTextNode(post_content)
|
||||||
content_node.appendChild(new_node)
|
content_node.appendChild(new_node)
|
||||||
|
|
||||||
|
|
||||||
def createPosts(self, posts, dom, root, node):
|
def createPosts(self, posts, dom, root, node):
|
||||||
posts_elem = self.createElement(dom, 'posts')
|
posts_elem = self.createElement(dom, 'posts')
|
||||||
for i in range(0, self.posts_per_page):
|
for i in range(0, self.posts_per_page):
|
||||||
|
|
|
@ -7,6 +7,38 @@ from django.db import models
|
||||||
|
|
||||||
class Post(Index):
|
class Post(Index):
|
||||||
|
|
||||||
|
def createComments(self, post, dom, post_elem, root):
|
||||||
|
from dynastie.models import Post, Blog, Comment
|
||||||
|
|
||||||
|
base_url = root.getAttribute('base_url')
|
||||||
|
add_comment = (root.getAttribute('add_comment') == '1')
|
||||||
|
|
||||||
|
comments = Comment.objects.filter(post=post)
|
||||||
|
|
||||||
|
comment_list_list = []
|
||||||
|
for comment in comments:
|
||||||
|
try:
|
||||||
|
if comment.parent_id == 0:
|
||||||
|
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])
|
||||||
|
|
||||||
|
initial_root_comment = root_comment = self.createElement(dom, 'comments')
|
||||||
|
for comment_list in comment_list_list:
|
||||||
|
for comment in comment_list:
|
||||||
|
comment_element = self.createElement(dom, 'comment')
|
||||||
|
comment_content = self.createElement(dom, 'comment_content', comment.the_comment)
|
||||||
|
comment_element.appendChild(comment_content)
|
||||||
|
root_comment.appendChild(comment_element)
|
||||||
|
root_comment = comment_element
|
||||||
|
root_comment = initial_root_comment
|
||||||
|
post_elem.replaceChild(root_comment, root)
|
||||||
|
|
||||||
def createMetas(self, post, dom, meta_elem, root):
|
def createMetas(self, post, dom, meta_elem, root):
|
||||||
name = root.getAttribute('name')
|
name = root.getAttribute('name')
|
||||||
if name is None:
|
if name is None:
|
||||||
|
@ -29,7 +61,13 @@ class Post(Index):
|
||||||
self.addError('name attribute \'' + name + '\' unknown for dyn:meta' )
|
self.addError('name attribute \'' + name + '\' unknown for dyn:meta' )
|
||||||
|
|
||||||
def _createPost(self, post, dom, post_elem, root):
|
def _createPost(self, post, dom, post_elem, root):
|
||||||
|
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_nodes = dom.getElementsByTagNameNS(self.URI, 'post')
|
post_nodes = dom.getElementsByTagNameNS(self.URI, 'post')
|
||||||
post_elem = post_nodes[0]
|
post_elem = post_nodes[0]
|
||||||
post_elem.parentNode.removeChild(post_elem)
|
post_elem.parentNode.removeChild(post_elem)
|
||||||
|
@ -43,8 +81,10 @@ class Post(Index):
|
||||||
node.appendChild(dom.createTextNode(post.title))
|
node.appendChild(dom.createTextNode(post.title))
|
||||||
|
|
||||||
def _generate(self, blog, src, output, posts):
|
def _generate(self, blog, src, output, posts):
|
||||||
|
import xml
|
||||||
hooks = {'post' : self._createPost,
|
hooks = {'post' : self._createPost,
|
||||||
'meta' : self.createMetas}
|
'meta' : self.createMetas,
|
||||||
|
'comments' : self.createComments}
|
||||||
|
|
||||||
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')
|
||||||
|
@ -63,6 +103,7 @@ class Post(Index):
|
||||||
#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, 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") + '/'
|
||||||
if not os.path.exists(filename):
|
if not os.path.exists(filename):
|
||||||
|
|
|
@ -260,9 +260,9 @@ 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)
|
||||||
date = models.DateTimeField(max_length=255)
|
date = models.DateTimeField()
|
||||||
author = models.CharField(max_length=255)
|
author = models.CharField(max_length=255)
|
||||||
email = models.EmailField(max_length=255)
|
email = models.EmailField(max_length=255, blank=True)
|
||||||
the_comment = models.TextField(max_length=255)
|
the_comment = models.TextField(max_length=255)
|
||||||
|
|
||||||
@receiver(post_init, sender=Blog)
|
@receiver(post_init, sender=Blog)
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<form id="previewForm" action="/post/edit/{{ blog_id }}" method="post">{% csrf_token %}
|
<form id="previewForm" action="/post/edit/{{ post_id }}" method="post">{% csrf_token %}
|
||||||
{{ form.as_p }}
|
{{ form.as_p }}
|
||||||
<textarea name="content" class="mceAdvanced">{{ content }}</textarea>
|
<textarea name="content" class="mceAdvanced">{{ content }}</textarea>
|
||||||
<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" />
|
||||||
|
|
75
views.py
75
views.py
|
@ -14,7 +14,7 @@ from django.template import Variable, VariableDoesNotExist
|
||||||
|
|
||||||
@register.filter
|
@register.filter
|
||||||
def hash(object, attr):
|
def hash(object, attr):
|
||||||
return None
|
|
||||||
pseudo_context = { 'object' : object }
|
pseudo_context = { 'object' : object }
|
||||||
try:
|
try:
|
||||||
value = Variable('object.%s' % attr).resolve(pseudo_context)
|
value = Variable('object.%s' % attr).resolve(pseudo_context)
|
||||||
|
@ -30,13 +30,11 @@ def have_I_right(request, blog_id=None, post_id=None, must_be_superuser=False):
|
||||||
raise Http404
|
raise Http404
|
||||||
|
|
||||||
if not post_id is None:
|
if not post_id is None:
|
||||||
p = Post.objects.filter(pk=post_id)
|
p = Post.objects.get(pk=post_id)
|
||||||
|
|
||||||
if p is None:
|
if p is None:
|
||||||
raise Http404
|
raise Http404
|
||||||
|
|
||||||
p = p[0]
|
|
||||||
|
|
||||||
blog_id = p.blog.id
|
blog_id = p.blog.id
|
||||||
|
|
||||||
if not blog_id is None:
|
if not blog_id is None:
|
||||||
|
@ -269,10 +267,11 @@ def view_blog(request, blog_id):
|
||||||
comments = Comment.objects.all()
|
comments = Comment.objects.all()
|
||||||
dict_comments = {}
|
dict_comments = {}
|
||||||
for comment in comments:
|
for comment in comments:
|
||||||
if not str(comment.post.id) in dict_comments:
|
key = comment.post.id
|
||||||
dict_comments[str(comment.post.id)] = 1
|
if not key in dict_comments:
|
||||||
|
dict_comments[key] = 1
|
||||||
else:
|
else:
|
||||||
dict_comments[str(comment.post.id)] = dict_comments[comment.post.id] + 1
|
dict_comments[key] = dict_comments[key] + 1
|
||||||
|
|
||||||
c = {'blog' : b, 'posts' : posts, 'form' : form, 'comments' : dict_comments}
|
c = {'blog' : b, 'posts' : posts, 'form' : form, 'comments' : dict_comments}
|
||||||
|
|
||||||
|
@ -347,8 +346,8 @@ def edit_post(request, post_id):
|
||||||
if form.is_valid(): # All validation rules pass
|
if form.is_valid(): # All validation rules pass
|
||||||
if title != request.POST['title']:
|
if title != request.POST['title']:
|
||||||
post.remove()
|
post.remove()
|
||||||
post.createPost(request.POST['content'])
|
|
||||||
form.save()
|
form.save()
|
||||||
|
post.createPost(request.POST['content'])
|
||||||
# Process the data in form.cleaned_data
|
# Process the data in form.cleaned_data
|
||||||
# ...
|
# ...
|
||||||
return HttpResponseRedirect('/blog/' + str(blog_id)) # Redirect after POST
|
return HttpResponseRedirect('/blog/' + str(blog_id)) # Redirect after POST
|
||||||
|
@ -377,8 +376,8 @@ def edit_post(request, post_id):
|
||||||
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] == comment.parent:
|
if comment_list[0].id == comment.parent_id:
|
||||||
comment_list[0].append(comment)
|
comment_list.append(comment)
|
||||||
break
|
break
|
||||||
except Comment.DoesNotExist:
|
except Comment.DoesNotExist:
|
||||||
comment_list_list.append([comment])
|
comment_list_list.append([comment])
|
||||||
|
@ -409,22 +408,16 @@ def generate(request, blog_id):
|
||||||
b = Blog.objects.get(pk=blog_id)
|
b = Blog.objects.get(pk=blog_id)
|
||||||
form = BlogForm(instance=b)
|
form = BlogForm(instance=b)
|
||||||
|
|
||||||
comments = Comment.objects.filter(post=post).order_by('date')
|
comments = Comment.objects.all()
|
||||||
|
dict_comments = {}
|
||||||
comment_list_list = []
|
|
||||||
for comment in comments:
|
for comment in comments:
|
||||||
try:
|
key = comment.post.id
|
||||||
if comment.parent.id is None:
|
if not key in dict_comments:
|
||||||
comment_list_list.append([comment])
|
dict_comments[key] = 1
|
||||||
else:
|
else:
|
||||||
for comment_list in comment_list_list:
|
dict_comments[key] = dict_comments[key] + 1
|
||||||
if comment_list[0] == comment.parent:
|
|
||||||
comment_list[0].append(comment)
|
|
||||||
break
|
|
||||||
except Comment.DoesNotExist:
|
|
||||||
comment_list_list.append([comment])
|
|
||||||
|
|
||||||
c = {'blog' : b, 'posts' : posts, 'form' : form, 'report': report, 'comments' : comment_list_list}
|
c = {'blog' : b, 'posts' : posts, 'form' : form, 'report': report, 'comments' : dict_comments}
|
||||||
|
|
||||||
return render(request, 'templates/generate.html', c)
|
return render(request, 'templates/generate.html', c)
|
||||||
|
|
||||||
|
@ -525,14 +518,20 @@ def add_comment(request, post_id, parent_id):
|
||||||
|
|
||||||
ref = request.META['HTTP_REFERER']
|
ref = request.META['HTTP_REFERER']
|
||||||
|
|
||||||
post = Post(pk=post_id)
|
post = Post.objects.get(pk=post_id)
|
||||||
if post is None:
|
if post is None:
|
||||||
|
print 'no post'
|
||||||
return HttpResponseRedirect(ref)
|
return HttpResponseRedirect(ref)
|
||||||
|
|
||||||
blog = post.blog
|
blog = Blog.objects.get(pk=post.blog_id)
|
||||||
|
|
||||||
|
if blog is None:
|
||||||
|
print 'no blog ' + str(post.blog.id)
|
||||||
|
return HttpResponseRedirect(ref)
|
||||||
|
|
||||||
|
parent_id = int(parent_id)
|
||||||
if parent_id != 0:
|
if parent_id != 0:
|
||||||
parentComment = Comment(pk=parent_id)
|
parentComment = Comment.objects.get(pk=parent_id)
|
||||||
if parentComment is None:
|
if parentComment is None:
|
||||||
return HttpResponseRedirect(ref)
|
return HttpResponseRedirect(ref)
|
||||||
else:
|
else:
|
||||||
|
@ -549,11 +548,12 @@ def add_comment(request, post_id, parent_id):
|
||||||
engine = globals()['post']
|
engine = globals()['post']
|
||||||
blog.create_paths()
|
blog.create_paths()
|
||||||
|
|
||||||
|
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") \
|
||||||
and obj.__module__.endswith("post"):
|
and obj.__module__.endswith("post"):
|
||||||
e = obj()
|
e = obj()
|
||||||
content = e._generate(blog, b.src_path, b.output_path, post)
|
content = e._generate(blog, blog.src_path, blog.output_path, post_list)
|
||||||
break
|
break
|
||||||
|
|
||||||
# Send emails
|
# Send emails
|
||||||
|
@ -564,21 +564,19 @@ def add_comment(request, post_id, parent_id):
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
def edit_comment(request, comment_id):
|
def edit_comment(request, comment_id):
|
||||||
comment = Comment(pk=comment_id)
|
comment = Comment.objects.get(pk=comment_id)
|
||||||
|
|
||||||
if comment is None:
|
if comment is None:
|
||||||
return Http404
|
return Http404
|
||||||
|
|
||||||
(b, post) = have_I_right(request, None, comment.post.id)
|
(b, post) = have_I_right(request, None, comment.post_id)
|
||||||
post_id = post.id
|
post_id = comment.post_id
|
||||||
|
|
||||||
if request.method == 'POST': # If the form has been submitted...
|
if request.method == 'POST': # If the form has been submitted...
|
||||||
if 'edit' in request.POST:
|
if 'edit' in request.POST:
|
||||||
form = UserForm(request.POST) # A form bound to the POST data
|
form = CommentForm(request.POST, instance=comment) # A form bound to the POST data
|
||||||
if form.is_valid(): # All validation rules pass
|
if form.is_valid(): # All validation rules pass
|
||||||
form = form.save()
|
form = form.save()
|
||||||
user = User.objects.get(pk=form.id)
|
|
||||||
user.set_password(request.POST['password'])
|
|
||||||
user.save()
|
|
||||||
# Process the data in form.cleaned_data
|
# Process the data in form.cleaned_data
|
||||||
# ...
|
# ...
|
||||||
return HttpResponseRedirect('/post/edit/' + str(post_id))
|
return HttpResponseRedirect('/post/edit/' + str(post_id))
|
||||||
|
@ -586,7 +584,7 @@ def edit_comment(request, comment_id):
|
||||||
return HttpResponseRedirect('/post/edit/' + str(post_id))
|
return HttpResponseRedirect('/post/edit/' + str(post_id))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
form = CommentForm() # An unbound form
|
form = CommentForm(instance=comment) # An unbound form
|
||||||
|
|
||||||
return render(request, 'edit_comment.html', {
|
return render(request, 'edit_comment.html', {
|
||||||
'form': form, 'comment':comment
|
'form': form, 'comment':comment
|
||||||
|
@ -594,13 +592,12 @@ def edit_comment(request, comment_id):
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
def delete_comment(request, comment_id):
|
def delete_comment(request, comment_id):
|
||||||
comment = Comment(pk=comment_id)
|
comment = Comment.objects.get(pk=comment_id)
|
||||||
if comment is None:
|
if comment is None:
|
||||||
return Http404
|
return Http404
|
||||||
|
|
||||||
(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 = post.id
|
|
||||||
|
|
||||||
childs = Comment.objects.filter(parent=comment)
|
childs = Comment.objects.filter(parent=comment)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user