Beispiel #1
0
def detail(request, post_id):
    """

    :param request:
    :param post_id:
    :return:
    """
    post = get_object_or_404(Post, pk=post_id)
    parents = post.item.all()
    content = ContentType.objects.get_for_model(Comment)
    subs = Comment.objects.filter(content_type=content).filter(is_active=1).filter(parent=post_id)
    if request.method == "POST":
        form = CommentForm(request.POST)
        if form.is_valid():
            text = form.cleaned_data["text"]
            email = form.cleaned_data["email"]
            c_type = request.POST.get("c_type")
            obj_id = request.POST.get("replyfor")

            if c_type == "Comment":
                c_model = Comment

            if c_type == "Post":
                c_model = Post

            con_type = ContentType.objects.get_for_model(c_model)
            new_comment = Comment(email=email,
                                  text=text,
                                  content_type=con_type,
                                  parent=post_id,
                                  object_id=obj_id,
                                  pubdate=datetime.now(),
                                  is_active=0)
            new_comment.save()

            if request.user.is_authenticated():
                new_comment.is_active = 1
                new_comment.user = request.user
                new_comment.save()
            else:

                if email_check(email):
                    return redirect('/')
                else:
                    act_code = produce_val()
                    send_act_code.delay(act_code,
                                        new_comment.email)
                    new_activation = Activation(conf_code=act_code,
                                                comment=new_comment,
                                                exp_date=datetime.now(), )
                    new_activation.save()
        form = CommentForm()
    else:
        form = CommentForm()

    return render(request, 'blog/deneme.html', {'parents': parents,
                                                'subs': subs, 'post': post,
                                                'r_user': request.user,
                                                'form': form, })