Add primary comment support
This commit is contained in:
parent
6039dd7a8d
commit
d30bb18e43
5
forms.py
5
forms.py
|
@ -23,3 +23,8 @@ class UserForm(ModelForm):
|
|||
class Meta:
|
||||
model = User
|
||||
exclude = ('is_staff', 'is_active', 'last_login', 'last_joined', 'user_permissions', 'groups', 'date_joined')
|
||||
|
||||
class CommentForm(ModelForm):
|
||||
class Meta:
|
||||
model = Comment
|
||||
exclude = ('post', 'parent', 'date')
|
||||
|
|
|
@ -42,9 +42,7 @@ class Post(Index):
|
|||
node.removeChild(node.childNodes[0])
|
||||
node.appendChild(dom.createTextNode(post.title))
|
||||
|
||||
def generate(self, blog, src, output):
|
||||
from dynastie.models import Post, Blog
|
||||
|
||||
def generate(self, blog, src, output, posts):
|
||||
hooks = {'post' : self._createPost,
|
||||
'meta' : self.createMetas}
|
||||
|
||||
|
@ -52,17 +50,15 @@ class Post(Index):
|
|||
self.addError('No _post.html found, exiting')
|
||||
return self.report
|
||||
|
||||
if not os.path.exists(output + '/post'):
|
||||
os.mkdir(output + '/post')
|
||||
|
||||
try:
|
||||
dom = parse(src + '/_post.html')
|
||||
except xml.dom.DOMException as e:
|
||||
self.addError('Error parsing _post.html : ' + e)
|
||||
return self.report
|
||||
|
||||
if not os.path.exists(output + '/post'):
|
||||
os.mkdir(output + '/post')
|
||||
|
||||
posts = Post.objects.all()
|
||||
|
||||
for post in posts:
|
||||
#print 'Generate ' + filename
|
||||
nodes = dom.getElementsByTagName("*")
|
||||
|
@ -80,6 +76,13 @@ class Post(Index):
|
|||
|
||||
return self.report
|
||||
|
||||
def generate(self, blog, src, output):
|
||||
from dynastie.models import Post, Blog
|
||||
|
||||
posts = Post.objects.all()
|
||||
|
||||
return self.generate(blog, src, output, hooks, dom, posts)
|
||||
|
||||
def createPreview(self, values, dom, root, node):
|
||||
now = datetime.datetime.now()
|
||||
|
||||
|
|
|
@ -257,8 +257,8 @@ class Post(models.Model):
|
|||
|
||||
class Comment(models.Model):
|
||||
post = models.ForeignKey(Post)
|
||||
parent = models.ForeignKey('Comment')
|
||||
date = models.DateField(max_length=255)
|
||||
parent = models.ForeignKey('self', null=True)
|
||||
date = models.DateTimeField(max_length=255)
|
||||
author = models.CharField(max_length=255)
|
||||
email = models.EmailField(max_length=255)
|
||||
the_comment = models.TextField(max_length=255)
|
||||
|
|
|
@ -5,9 +5,11 @@
|
|||
{% if categories|length == 0 %}
|
||||
<b>Any category available</b><br/><br/>
|
||||
{% else %}
|
||||
<table>
|
||||
{% for category in categories %}
|
||||
<hr><hl>{{ category.id }}</hl><hl>{{ category.name }}</hl><hl><a href="/category/edit/{{ category.id }}">Edit</a></hl>{% if user.is_superuser %}<hl><a href="/category/delete/{{ category.id }}">Delete</a></hl>{% endif %}</hr>
|
||||
<tr><td>{{ category.id }}</td><td>{{ category.name }}</td><td><a href="/category/edit/{{ category.id }}">Edit</a></td>{% if user.is_superuser %}<td><a href="/category/delete/{{ category.id }}">Delete</a></td>{% endif %}</tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
{% endif %}
|
||||
{% if user.is_superuser %}
|
||||
<li><a href="/category/add">Add a category</a></li>
|
||||
|
|
9
templates/edit_comment.html
Normal file
9
templates/edit_comment.html
Normal file
|
@ -0,0 +1,9 @@
|
|||
{% extends "templates/base.html" %}
|
||||
|
||||
{% block content %}
|
||||
<form action="/comment/edit/{{ comment.id }}" method="post">
|
||||
{% csrf_token %}
|
||||
{{ form.as_p }}
|
||||
<input type="submit" name="edit" value="Edit" /><input type="submit" name="cancel" value="Cancel" />
|
||||
</form>
|
||||
{% endblock %}
|
|
@ -14,4 +14,20 @@
|
|||
<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" />
|
||||
</form>
|
||||
<div class="comments">
|
||||
{% for comment_list in comments %}
|
||||
{% for comment in comment_list %}
|
||||
<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>
|
||||
</div>
|
||||
<div class="content">
|
||||
{{ comment.the_comment }}
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% for comment in comment_list %}
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% endblock %}
|
||||
|
|
|
@ -24,7 +24,7 @@
|
|||
{% else %}
|
||||
<table>
|
||||
{% for post in posts %}
|
||||
<tr><td><a href="/post/edit/{{ post.id }}">{{ post.id }}</a></td><td>{{ post.title }}</td><td>{{ post.category.name }}</td><td>{{ post.creation_date }}</td><td>{{ post.modification_date }}</td><td>{{ post.published }}</td><td>{{ post.front_page }}</td><td><a href="/post/delete/{{ post.id }}">Delete</a></td></tr>
|
||||
<tr><td><a href="/post/edit/{{ post.id }}">{{ post.id }}</a></td><td>{{ post.title }}</td><td>{{ post.category.name }}</td><td>{{ post.creation_date }}</td><td>{{ post.modification_date }}</td><td>{{ post.published }}</td><td>{{ post.front_page }}</td><td>{{ comments|hash:cur_id|default_if_none:"0" }} comment{{ comments|hash:cur_id|pluralize }}</td><td><a href="/post/delete/{{ post.id }}">Delete</a></td></tr>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
</table>
|
||||
|
|
|
@ -17,7 +17,9 @@
|
|||
{% else %}
|
||||
<table>
|
||||
{% for post in posts %}
|
||||
<tr><td><a href="/post/edit/{{ post.id }}">{{ post.id }}</a></td><td>{{ post.title }}</td><td>{{ post.category.name }}</td><td>{{ post.creation_date }}</td><td>{{ post.modification_date }}</td><td>{{ post.published }}</td><td>{{ post.front_page }}</td><td><a href="/post/delete/{{ post.id }}">Delete</a></td></tr>
|
||||
{% with post.id as cur_id %}
|
||||
<tr><td><a href="/post/edit/{{ post.id }}">{{ post.id }}</a></td><td>{{ post.title }}</td><td>{{ post.category.name }}</td><td>{{ post.creation_date }}</td><td>{{ post.modification_date }}</td><td>{{ post.published }}</td><td>{{ post.front_page }}</td><td>{{ comments|hash:cur_id|default_if_none:"0" }} comment{{ comments|hash:cur_id|pluralize }}</td><td><a href="/post/delete/{{ post.id }}">Delete</a></td></tr>
|
||||
{% endwith %}
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
</table>
|
||||
|
|
3
urls.py
3
urls.py
|
@ -28,6 +28,9 @@ urlpatterns = patterns('',
|
|||
url(r'^preview/(\d+)$', 'dynastie.views.preview', name='preview'),
|
||||
url(r'^tinyMCEExternalList/post/add/(\d+)$', 'dynastie.views.tinymcelist_add', name='tinymce'),
|
||||
url(r'^tinyMCEExternalList/post/edit/(\d+)$', 'dynastie.views.tinymcelist_edit', name='tinymce'),
|
||||
url(r'^comment/add/(\d+)/(\d+)$', 'dynastie.views.add_comment', name='add_comment'),
|
||||
url(r'^comment/edit/(\d+)$', 'dynastie.views.edit_comment', name='edit_comment'),
|
||||
url(r'^comment/delete/(\d+)$','dynastie.views.delete_comment',name='delete_comment'),
|
||||
# url(r'^dynastie/', include('dynastie.foo.urls')),
|
||||
|
||||
# Uncomment the admin/doc line below to enable admin documentation:
|
||||
|
|
234
views.py
234
views.py
|
@ -4,10 +4,50 @@ from django.shortcuts import render
|
|||
from django.contrib.auth import authenticate, login, logout
|
||||
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 dynastie.models import *
|
||||
from dynastie.forms import *
|
||||
|
||||
from django.template.defaultfilters import register
|
||||
from django.template import Variable, VariableDoesNotExist
|
||||
|
||||
@register.filter
|
||||
def hash(object, attr):
|
||||
pseudo_context = { 'object' : object }
|
||||
try:
|
||||
value = Variable('object.%s' % attr).resolve(pseudo_context)
|
||||
except VariableDoesNotExist:
|
||||
value = None
|
||||
return value
|
||||
|
||||
def have_I_right(request, blog_id=None, post_id=None):
|
||||
b = None
|
||||
p = None
|
||||
|
||||
if not post_id is None:
|
||||
p = Post.objects.filter(pk=post_id)
|
||||
|
||||
if p is None:
|
||||
raise Http404
|
||||
|
||||
p = p[0]
|
||||
|
||||
blog_id = p.blog.id
|
||||
|
||||
if not blog_id is None:
|
||||
if not request.user.is_superuser:
|
||||
b = Blog.objects.filter(pk=blog_id).filter(writers=request.user.id)[0]
|
||||
else:
|
||||
b = Blog.objects.get(pk=post.blog.id)
|
||||
|
||||
if b is None:
|
||||
raise Http404
|
||||
|
||||
b = b
|
||||
|
||||
return (b, p)
|
||||
|
||||
def index(request):
|
||||
if request.user.is_authenticated():
|
||||
return HttpResponseRedirect('/blog')
|
||||
|
@ -19,6 +59,10 @@ def index(request):
|
|||
login_failed = True
|
||||
else:
|
||||
login(request, user)
|
||||
print request.GET
|
||||
if 'next' in request.GET:
|
||||
return HttpResponseRedirect(request.GET['next'])
|
||||
else:
|
||||
return HttpResponseRedirect('/blog')
|
||||
|
||||
c = {'auth_key': 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',\
|
||||
|
@ -213,7 +257,15 @@ def view_blog(request, blog_id):
|
|||
b = Blog.objects.get(pk=blog_id)
|
||||
form = BlogForm(instance=b)
|
||||
|
||||
c = {'blog' : b, 'posts' : posts, 'form' : form}
|
||||
comments = Comment.objects.all()
|
||||
dict_comments = {}
|
||||
for comment in comments:
|
||||
if not str(comment.post.id) in dict_comments:
|
||||
dict_comments[str(comment.post.id)] = 1
|
||||
else:
|
||||
dict_comments[str(comment.post.id)] = dict_comments[comment.post.id] + 1
|
||||
|
||||
c = {'blog' : b, 'posts' : posts, 'form' : form, 'comments' : dict_comments}
|
||||
|
||||
return render(request, 'templates/view_blog.html', c)
|
||||
|
||||
|
@ -249,10 +301,7 @@ def edit_blog(request, blog_id):
|
|||
@login_required
|
||||
def add_post(request, blog_id):
|
||||
if not request.user.is_superuser:
|
||||
b = Blog.objects.filter(id=blog_id).filter(writers=request.user.id)[0]
|
||||
|
||||
if b is None:
|
||||
raise Http404
|
||||
(b,) = have_I_right(request, blog_id)
|
||||
|
||||
if request.method == 'POST': # If the form has been submitted...
|
||||
if 'add' in request.POST:
|
||||
|
@ -277,22 +326,11 @@ def add_post(request, blog_id):
|
|||
|
||||
@login_required
|
||||
def edit_post(request, post_id):
|
||||
(b, post) = have_I_right(request, None, post_id)
|
||||
post = Post.objects.get(pk=post_id)
|
||||
|
||||
if post is None:
|
||||
raise Http404
|
||||
|
||||
title = post.title
|
||||
|
||||
blog_id = post.blog.id
|
||||
|
||||
if not request.user.is_superuser:
|
||||
b = Blog.objects.filter(pk=post.blog.id).filter(writers=request.user.id)[0]
|
||||
|
||||
if b is None:
|
||||
raise Http404
|
||||
else:
|
||||
b = Blog.objects.get(pk=post.blog.id)
|
||||
blog_id = b.id
|
||||
|
||||
if request.method == 'POST': # If the form has been submitted...
|
||||
if 'edit' in request.POST:
|
||||
|
@ -321,24 +359,32 @@ def edit_post(request, post_id):
|
|||
else:
|
||||
content = 'Empty post'
|
||||
|
||||
comments = Comment.objects.filter(post=post).order_by('date')
|
||||
|
||||
comment_list_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] == comment.parent:
|
||||
comment_list[0].append(comment)
|
||||
break
|
||||
except Comment.DoesNotExist:
|
||||
comment_list_list.append([comment])
|
||||
|
||||
return render(request, 'edit_post.html', {
|
||||
'form': form, 'post_id' : post_id, 'content' : content,
|
||||
'blog_id' : blog_id
|
||||
'blog_id' : blog_id, 'comments' : comment_list_list
|
||||
})
|
||||
|
||||
@login_required
|
||||
def delete_post(request, post_id):
|
||||
post = Post.objects.get(pk=post_id)
|
||||
(b, post) = have_I_right(request, None, post_id)
|
||||
|
||||
if post is None:
|
||||
raise Http404
|
||||
|
||||
b = Blog.objects.filter(writers=request.user.id).filter(pk=post.blog.pk)
|
||||
|
||||
if b is None:
|
||||
raise Http404
|
||||
|
||||
blog_id = post.blog.pk
|
||||
blog_id = b.id
|
||||
|
||||
post.delete()
|
||||
|
||||
|
@ -346,13 +392,7 @@ def delete_post(request, post_id):
|
|||
|
||||
@login_required
|
||||
def generate(request, blog_id):
|
||||
if not request.user.is_superuser:
|
||||
b = Blog.objects.filter(id=blog_id).filter(writers=request.user.id)[0]
|
||||
else:
|
||||
b = Blog.objects.get(pk=blog_id)
|
||||
|
||||
if b is None:
|
||||
raise Http404
|
||||
(b, post) = have_I_right(request, blog_id)
|
||||
|
||||
b.create_paths()
|
||||
report = b.generate()
|
||||
|
@ -374,13 +414,7 @@ def preview(request, blog_id):
|
|||
'content' : request.POST['content']
|
||||
}
|
||||
|
||||
if not request.user.is_superuser:
|
||||
b = Blog.objects.filter(id=blog_id).filter(writers=request.user.id)[0]
|
||||
else:
|
||||
b = Blog.objects.get(pk=blog_id)
|
||||
|
||||
if b is None:
|
||||
raise Http404
|
||||
(b, ) = have_I_right(request, blog_id)
|
||||
|
||||
b.create_paths()
|
||||
|
||||
|
@ -435,34 +469,122 @@ def _tinymcelist(request, b, year, month):
|
|||
|
||||
@login_required
|
||||
def tinymcelist_add(request, blog_id):
|
||||
from datetime import datetime
|
||||
|
||||
now = datetime.now()
|
||||
year = now.year
|
||||
month = now.month
|
||||
b = Blog.objects.filter(pk=blog_id).filter(writers=request.user.id)[0]
|
||||
if b is None:
|
||||
|
||||
try:
|
||||
(b, ) = have_I_right(request, blog_id)
|
||||
except Http404:
|
||||
return HttpResponse('', content_type='application/x-javascript')
|
||||
|
||||
ret = _tinymcelist(request, b, year, month)
|
||||
|
||||
print 'Ret1 ' + ret
|
||||
|
||||
return HttpResponse(ret, content_type='application/x-javascript')
|
||||
|
||||
@login_required
|
||||
def tinymcelist_edit(request, post_id):
|
||||
post = Post.objects.filter(pk=post_id)
|
||||
if post is None:
|
||||
return HttpResponse('', content_type='application/x-javascript')
|
||||
b = Blog.objects.filter(pk=post.blog).filter(writers=request.user.id)[0]
|
||||
if b is None:
|
||||
try:
|
||||
(b, post) = have_I_right(request, None, post_id)
|
||||
except Http404:
|
||||
return HttpResponse('', content_type='application/x-javascript')
|
||||
|
||||
year = post.creation_date.year()
|
||||
month = post.creation_date.month()
|
||||
|
||||
ret = _tinymcelist(request, b, year, month)
|
||||
|
||||
print 'Ret2 ' + ret
|
||||
|
||||
return HttpResponse(ret, content_type='application/x-javascript')
|
||||
|
||||
@csrf_exempt
|
||||
def add_comment(request, post_id, parent_id):
|
||||
from dynastie.generators import post
|
||||
|
||||
ref = request.META['HTTP_REFERER']
|
||||
|
||||
post = Post(pk=post_id)
|
||||
if post is None:
|
||||
return HttpResponseRedirect(ref)
|
||||
|
||||
blog = post.blog
|
||||
|
||||
if parent_id != 0:
|
||||
parentComment = Comment(pk=parent_id)
|
||||
if parentComment is None:
|
||||
return HttpResponseRedirect(ref)
|
||||
else:
|
||||
parentComment = None
|
||||
|
||||
if request.POST['author'] == '' or request.POST['the_comment'] == '':
|
||||
print 'Error on author or the_comment'
|
||||
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'])
|
||||
comment.save()
|
||||
|
||||
engine = globals()['post']
|
||||
blog.create_paths()
|
||||
|
||||
for name, obj in inspect.getmembers(engine):
|
||||
if inspect.isclass(obj) and obj.__module__.startswith("dynastie") \
|
||||
and obj.__module__.endswith("post"):
|
||||
e = obj()
|
||||
content = e.generate(blog, b.src_path, b.output_path, post)
|
||||
break
|
||||
|
||||
# Send emails
|
||||
|
||||
return HttpResponseRedirect(ref)
|
||||
|
||||
|
||||
|
||||
@login_required
|
||||
def edit_comment(request, comment_id):
|
||||
comment = Comment(pk=comment_id)
|
||||
if comment is None:
|
||||
return Http404
|
||||
|
||||
(b, post) = have_I_right(request, None, comment.post.id)
|
||||
post_id = post.id
|
||||
|
||||
if request.method == 'POST': # If the form has been submitted...
|
||||
if 'edit' in request.POST:
|
||||
form = UserForm(request.POST) # A form bound to the POST data
|
||||
if form.is_valid(): # All validation rules pass
|
||||
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
|
||||
# ...
|
||||
return HttpResponseRedirect('/post/edit/' + str(post_id))
|
||||
else:
|
||||
return HttpResponseRedirect('/post/edit/' + str(post_id))
|
||||
|
||||
else:
|
||||
form = CommentForm() # An unbound form
|
||||
|
||||
return render(request, 'edit_comment.html', {
|
||||
'form': form, 'comment':comment
|
||||
})
|
||||
|
||||
@login_required
|
||||
def delete_comment(request, comment_id):
|
||||
comment = Comment(pk=comment_id)
|
||||
if comment is None:
|
||||
return Http404
|
||||
|
||||
(b, post) = have_I_right(request, None, comment.post.id)
|
||||
|
||||
post_id = post.id
|
||||
|
||||
childs = Comment.objects.filter(parent=comment)
|
||||
|
||||
for child in childs:
|
||||
child.parent = comment.parent
|
||||
child.save()
|
||||
|
||||
comment.delete()
|
||||
|
||||
return HttpResponseRedirect('/post/edit/' + str(post_id))
|
||||
|
|
Loading…
Reference in New Issue
Block a user