Example #1
0
    def post(self, request, slug=None, *args, **kwargs):
        post = get_object_or_404(Post, slug=slug)
        # import pdb;pdb.set_trace()
        if request.method == 'POST':
            comment_form = CommentForm(request.POST or None)
            if comment_form.is_valid() and request.user.is_authenticated():
                # content = comment_form.clean_message()
                temp = comment_form.save(commit=False)
                parent = comment_form['parent'].value()
                content_data = comment_form.clean_message()
                user1 = User.objects.filter(id=post.user_id.id)
                if parent == '':
                    # Set a blank path then save it to get an ID
                    temp.path = []
                    temp.author = request.user
                    temp.post = post
                    temp.save()

                    temp.path = [temp.id]
                    notify.send(request.user,
                                recipient=user1,
                                verb='Post comment',
                                action_object=post)
                else:
                    # Get the parent node
                    node = Comment.objects.filter(id=parent)
                    if node.exists() and node.count() == 1:
                        node = node.first()
                    temp.depth = node.depth + 1
                    temp.path = node.path
                    temp.author = request.user
                    temp.post = post
                    # Store parents path then apply comment ID
                    temp.save()
                    temp.path.append(temp.id)
                    notify.send(request.user,
                                recipient=user1,
                                verb='You have reply on your comment')

                # Final save for parents and children
                temp.save()
        comments = Comment.objects.all().order_by('path')
        return HttpResponseRedirect('/{0}'.format(post.slug))