You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1.2 KiB
39 lines
1.2 KiB
from django.shortcuts import render, reverse |
|
from posts.models import Post |
|
from django.http import Http404, HttpResponseRedirect |
|
from .models import Comment |
|
from datetime import datetime |
|
from django.core.exceptions import ObjectDoesNotExist |
|
|
|
|
|
def leave_comment(request, post_id): |
|
parent: Comment |
|
|
|
try: |
|
post = Post.objects.get(id=post_id) |
|
except ObjectDoesNotExist: |
|
raise Http404("Пост не найден") |
|
|
|
# сделать проверку более вменяемой |
|
parent_id = request.POST["reply_to"] |
|
|
|
if parent_id == "": |
|
parent = None |
|
else: |
|
try: |
|
parent = Comment.objects.get(id=parent_id) |
|
# если комментарий, на который отвечает пользователь не к |
|
# этой статье, то игнорируем поле ответа |
|
if not parent in post.comment_set.all(): |
|
parent = None |
|
|
|
except ObjectDoesNotExist: |
|
parent = None |
|
|
|
post.comment_set.create( |
|
author_name=request.POST["name"], |
|
comment_text=request.POST["text"], |
|
reply=parent, |
|
) |
|
|
|
return HttpResponseRedirect(reverse("posts:detail", args=(post.id,)))
|
|
|