Example #1
0
def detail(request,article_id):
    try:
        article = models.Article.objects.get(pk=article_id)#pk=id
        comment_list = models.Comment.objects.all()
    except models.Article.DoesNotExist:
        raise Http404

    if request.method=='POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/blog/%s/'%article_id)
    else:
        form = CommentForm()
    return render_to_response('detail.html',{'article':article,'comment_list':comment_list,'form':form})
Example #2
0
def add_comment(request,article_id):
    a = Article.objects.get(id=article_id)
    
    if request.method == "POST":
	f =CommentForm(request.POST)
	if f.is_valid():
		# This means that save this instance of form but do not
                # push anything to databases(commit = False) as we are 
                # not finished yet
	    c = f.save(commit =False)
	    c.pub_date = timezone.now()
	    c.article = a 
	    c.save()
            # by default commit = True, therefore the comment will be 
            # saved to the database.
	    
	    return HttpResponseRedirect('/articles/get/%s' % article_id )
    else:
	f = CommentForm()
	    
    args = {}
    args.update(csrf(request))
    args['article'] = a
    args['form'] = f
	    
    return render_to_response('add_comment.html',args)
Example #3
0
def post(request, post_slug):

    post = Post.objects.get(slug=post_slug)
    user = request.user

    form = CommentForm(request.POST or None)
    if request.method == "POST":
        if form.is_valid():
            temp = form.save(commit=False)
            temp.post = post
            temp.user = user
            parent = form['parent'].value()
            print parent
            if parent == '':
                # Set a blank path then save it to get an ID
                temp.path = []
                temp.save()
                temp.path = [temp.id]
            else:
                # Get the parent node
                node = Comment.objects.get(id=parent)
                temp.depth = node.depth + 1
                temp.path = node.path

                # Store parents path then apply comment ID
                temp.save()
                temp.path.append(temp.id)

            # Final save for parents and children
            temp.save()
            form = CommentForm() # Reset the form, clean the fields. (AJAX doesn't refresh the page)
    # Retrieve all comments and sort them by path
    comment_tree = Comment.objects.filter(post=post).order_by('path')

    return render(request, 'reddit/post.html', locals())