diff --git a/forms.py b/forms.py
index 7cc8f75..c9551fe 100644
--- a/forms.py
+++ b/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')
diff --git a/generators/post.py b/generators/post.py
index 227837b..989a60a 100644
--- a/generators/post.py
+++ b/generators/post.py
@@ -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,16 +50,14 @@ 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
@@ -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()
diff --git a/models.py b/models.py
index 3c6317b..d0f5049 100644
--- a/models.py
+++ b/models.py
@@ -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)
diff --git a/templates/category.html b/templates/category.html
index b5a82e2..78453d4 100644
--- a/templates/category.html
+++ b/templates/category.html
@@ -5,9 +5,11 @@
{% if categories|length == 0 %}
Any category available
{% else %}
+
{% for category in categories %}
-
{{ category.id }}{{ category.name }}Edit{% if user.is_superuser %}Delete{% endif %}
+ {{ category.id }} | {{ category.name }} | Edit | {% if user.is_superuser %}Delete | {% endif %}
{% endfor %}
+
{% endif %}
{% if user.is_superuser %}
Add a category
diff --git a/templates/edit_comment.html b/templates/edit_comment.html
new file mode 100644
index 0000000..4cd4a2b
--- /dev/null
+++ b/templates/edit_comment.html
@@ -0,0 +1,9 @@
+{% extends "templates/base.html" %}
+
+{% block content %}
+
+{% endblock %}
diff --git a/templates/edit_post.html b/templates/edit_post.html
index 6a032d9..61584a6 100644
--- a/templates/edit_post.html
+++ b/templates/edit_post.html
@@ -14,4 +14,20 @@
+
+{% endfor %}
{% endblock %}
diff --git a/templates/generate.html b/templates/generate.html
index 8fc683d..6c6c82b 100644
--- a/templates/generate.html
+++ b/templates/generate.html
@@ -24,7 +24,7 @@
{% else %}
{% for post in posts %}
- {{ post.id }} | {{ post.title }} | {{ post.category.name }} | {{ post.creation_date }} | {{ post.modification_date }} | {{ post.published }} | {{ post.front_page }} | Delete |
+ {{ post.id }} | {{ post.title }} | {{ post.category.name }} | {{ post.creation_date }} | {{ post.modification_date }} | {{ post.published }} | {{ post.front_page }} | {{ comments|hash:cur_id|default_if_none:"0" }} comment{{ comments|hash:cur_id|pluralize }} | Delete |
{% endfor %}
{% endif %}
diff --git a/templates/view_blog.html b/templates/view_blog.html
index b006f24..dca0a0e 100644
--- a/templates/view_blog.html
+++ b/templates/view_blog.html
@@ -17,7 +17,9 @@
{% else %}
{% for post in posts %}
- {{ post.id }} | {{ post.title }} | {{ post.category.name }} | {{ post.creation_date }} | {{ post.modification_date }} | {{ post.published }} | {{ post.front_page }} | Delete |
+{% with post.id as cur_id %}
+ {{ post.id }} | {{ post.title }} | {{ post.category.name }} | {{ post.creation_date }} | {{ post.modification_date }} | {{ post.published }} | {{ post.front_page }} | {{ comments|hash:cur_id|default_if_none:"0" }} comment{{ comments|hash:cur_id|pluralize }} | Delete |
+ {% endwith %}
{% endfor %}
{% endif %}
diff --git a/urls.py b/urls.py
index 87e3ac4..ac27bf8 100644
--- a/urls.py
+++ b/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:
diff --git a/views.py b/views.py
index 10c7694..d046cfe 100644
--- a/views.py
+++ b/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,7 +59,11 @@ def index(request):
login_failed = True
else:
login(request, user)
- return HttpResponseRedirect('/blog')
+ print request.GET
+ if 'next' in request.GET:
+ return HttpResponseRedirect(request.GET['next'])
+ else:
+ return HttpResponseRedirect('/blog')
c = {'auth_key': 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',\
'login_failed' : login_failed}
@@ -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))