diff --git a/blog/blog/urls.py b/blog/blog/urls.py index acce39f..e87b920 100644 --- a/blog/blog/urls.py +++ b/blog/blog/urls.py @@ -19,5 +19,6 @@ from django.urls import path, include urlpatterns = [ path("", include("mainpage.urls")), path("posts/", include("posts.urls")), + path("comments/", include("comments.urls")), path("admin/", admin.site.urls), ] diff --git a/blog/comments/eyerest/Main b/blog/comments/eyerest/Main deleted file mode 100644 index 2186f1f..0000000 --- a/blog/comments/eyerest/Main +++ /dev/null @@ -1 +0,0 @@ -((1 (((min-height . 8) (min-width . 20) (min-height-ignore . 6) (min-width-ignore . 8) (min-height-safe . 2) (min-width-safe . 4) (min-pixel-height . 160) (min-pixel-width . 180) (min-pixel-height-ignore . 120) (min-pixel-width-ignore . 72) (min-pixel-height-safe . 40) (min-pixel-width-safe . 36)) hc (pixel-width . 2044) (pixel-height . 1080) (total-width . 227) (total-height . 54) (normal-height . 1.0) (normal-width . 1.0) (combination-limit) (vc (pixel-width . 1017) (pixel-height . 1080) (total-width . 113) (total-height . 54) (normal-height . 1.0) (normal-width . 0.4975538160469667) (combination-limit) (leaf (pixel-width . 1017) (pixel-height . 540) (total-width . 113) (total-height . 27) (normal-height . 0.5) (normal-width . 1.0) (buffer "20220325142556-дневник_2022.org" (selected) (hscroll . 0) (fringes 0 0 nil nil) (margins 1 . 1) (scroll-bars nil 0 t nil 0 t nil) (vscroll . 0) (dedicated) (point . 72876) (start . 72360)) (prev-buffers ("20220409000136-вяленая_курица_джерки.org" 1 108) ("gui-config.org" 49463 49914))) (leaf (last . t) (pixel-width . 1017) (pixel-height . 540) (total-width . 113) (total-height . 27) (normal-height . 0.5) (normal-width . 1.0) (buffer "20220409000136-вяленая_курица_джерки.org" (selected) (hscroll . 0) (fringes 0 0 nil nil) (margins 1 . 1) (scroll-bars nil 0 t nil 0 t nil) (vscroll . 0) (dedicated) (point . 108) (start . 1)) (next-buffers "20220325142556-дневник_2022.org") (prev-buffers ("20220325142556-дневник_2022.org" 1 72377) ("20220409000136-вяленая_курица_джерки.org" 1 108)))) (vc (last . t) (pixel-width . 1027) (pixel-height . 1080) (total-width . 114) (total-height . 54) (normal-height . 1.0) (normal-width . 0.5024461839530333) (combination-limit) (leaf (pixel-width . 1027) (pixel-height . 540) (total-width . 114) (total-height . 27) (normal-height . 0.5) (normal-width . 1.0) (buffer "20220330104048-благоволительницы_джонатан_литтелл.org" (selected) (hscroll . 0) (fringes 0 0 nil nil) (margins 1 . 1) (scroll-bars nil 0 t nil 0 t nil) (vscroll . 0) (dedicated) (point . 1221) (start . 1)) (prev-buffers ("20220409000136-вяленая_курица_джерки.org" 1 108))) (leaf (last . t) (pixel-width . 1027) (pixel-height . 540) (total-width . 114) (total-height . 27) (normal-height . 0.5) (normal-width . 1.0) (buffer "20220408212219-jagten_2012.org" (selected . t) (hscroll . 0) (fringes 0 0 nil nil) (margins 1 . 1) (scroll-bars nil 0 t nil 0 t nil) (vscroll . 0) (dedicated) (point . 96) (start . 1)) (prev-buffers ("20220330104048-благоволительницы_джонатан_литтелл.org" 1 247))))) "personal") (2 (((min-height . 4) (min-width . 57) (min-height-ignore . 3) (min-width-ignore . 12) (min-height-safe . 1) (min-width-safe . 6) (min-pixel-height . 80) (min-pixel-width . 513) (min-pixel-height-ignore . 60) (min-pixel-width-ignore . 108) (min-pixel-height-safe . 20) (min-pixel-width-safe . 54)) hc (pixel-width . 2043) (pixel-height . 1080) (total-width . 227) (total-height . 54) (normal-height . 1.0) (normal-width . 1.0) (combination-limit) (leaf (pixel-width . 333) (pixel-height . 1080) (total-width . 37) (total-height . 54) (normal-height . 1.0) (normal-width . 0.16299559471365643) (parameters (window-side . left) (window-slot . 0)) (buffer " *Treemacs-Scoped-Buffer-#*" (selected) (hscroll . 0) (fringes 0 0 nil nil) (margins 1 . 1) (scroll-bars nil 0 t nil 0 t nil) (vscroll . 0) (dedicated . t) (point . 743) (start . 418))) (hc (last . t) (pixel-width . 1710) (pixel-height . 1080) (total-width . 190) (total-height . 54) (normal-height . 1.0) (normal-width . 0.8370044052863436) (combination-limit . t) (leaf (pixel-width . 855) (pixel-height . 1080) (total-width . 95) (total-height . 54) (normal-height . 1.0) (normal-width . 0.5) (buffer "models.py" (selected . t) (hscroll . 0) (fringes 0 0 nil nil) (margins 1 . 1) (scroll-bars nil 0 t nil 0 t nil) (vscroll . 0) (dedicated) (point . 430) (start . 1)) (prev-buffers ("*scratch*" 1 153) ("gui-config.org" 12662 49814))) (leaf (last . t) (pixel-width . 855) (pixel-height . 1080) (total-width . 95) (total-height . 54) (normal-height . 1.0) (normal-width . 0.5) (buffer "models.py" (selected) (hscroll . 0) (fringes 0 0 nil nil) (margins 1 . 1) (scroll-bars nil 0 t nil 0 t nil) (vscroll . 0) (dedicated) (point . 846) (start . 1)) (prev-buffers ("models.py" 1 846) ("models.py" 1 792) ("models.py" 1 61))))) "") (3 (((min-height . 4) (min-width . 10) (min-height-ignore . 3) (min-width-ignore . 4) (min-height-safe . 1) (min-width-safe . 2) (min-pixel-height . 80) (min-pixel-width . 90) (min-pixel-height-ignore . 60) (min-pixel-width-ignore . 36) (min-pixel-height-safe . 20) (min-pixel-width-safe . 18)) leaf (pixel-width . 2043) (pixel-height . 1080) (total-width . 227) (total-height . 54) (normal-height . 1.0) (normal-width . 1.0) (buffer "20210616121338-django.org" (selected . t) (hscroll . 0) (fringes 0 0 nil nil) (margins 1 . 1) (scroll-bars nil 0 t nil 0 t nil) (vscroll . 0) (dedicated) (point . 6700) (start . 1))) "") (4 (((min-height . 4) (min-width . 10) (min-height-ignore . 3) (min-width-ignore . 4) (min-height-safe . 1) (min-width-safe . 2) (min-pixel-height . 80) (min-pixel-width . 90) (min-pixel-height-ignore . 60) (min-pixel-width-ignore . 36) (min-pixel-height-safe . 20) (min-pixel-width-safe . 18)) leaf (pixel-width . 2043) (pixel-height . 1080) (total-width . 227) (total-height . 54) (normal-height . 1.0) (normal-width . 1.0) (buffer "gui-config.org" (selected . t) (hscroll . 0) (fringes 0 0 nil nil) (margins 1 . 1) (scroll-bars nil 0 t nil 0 t nil) (vscroll . 0) (dedicated) (point . 39115) (start . 11896)) (prev-buffers ("20210331101333-теги_для_hugo.org" 1 1) ("*scratch*" 1 1) ("gui-config.org" 12662 49814))) "")) \ No newline at end of file diff --git a/blog/comments/models.py b/blog/comments/models.py index a0cfacd..b3912fa 100644 --- a/blog/comments/models.py +++ b/blog/comments/models.py @@ -1,5 +1,7 @@ from django.db import models from posts.models import Post +from datetime import datetime +from django.utils import timezone class Comment(models.Model): @@ -8,9 +10,9 @@ class Comment(models.Model): post = models.ForeignKey(Post, on_delete=models.CASCADE) author_name = models.CharField("имя автора", max_length=60) author_secret = models.CharField("секретная строка", max_length=128, blank=True) - reply = models.IntegerField(null=True) + reply = models.ForeignKey("self", on_delete=models.SET_NULL, blank=True, null=True) comment_text = models.TextField() - date = models.DateTimeField() + date = models.DateTimeField(default=timezone.now) class CommentAuthor(models.Model): diff --git a/blog/comments/urls.py b/blog/comments/urls.py new file mode 100644 index 0000000..5d2c5c9 --- /dev/null +++ b/blog/comments/urls.py @@ -0,0 +1,7 @@ +from django.urls import path +from . import views + +app_name = "comments" +urlpatterns = [ + path("/leave_comment", views.leave_comment, name="leave_comment"), +] diff --git a/blog/comments/views.py b/blog/comments/views.py index 91ea44a..6c8caea 100644 --- a/blog/comments/views.py +++ b/blog/comments/views.py @@ -1,3 +1,39 @@ -from django.shortcuts import render +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 -# Create your views here. + +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,))) diff --git a/blog/posts/templates/posts/post.html b/blog/posts/templates/posts/post.html index beb537d..42f0755 100644 --- a/blog/posts/templates/posts/post.html +++ b/blog/posts/templates/posts/post.html @@ -20,10 +20,47 @@

Комменты

{% for comment in comments %} -

{{ comment.author_name }}

-

{{ comment.comment_text }}

-
- {% endfor %} + +
+ {% if comment.reply %} + +

[{{comment.id}}] {{ comment.author_name }} отвечает {{ comment.reply.author_name }}:

+
+
{{ comment.reply.comment_text }}
+ {% else %} + +

[{{comment.id}}] {{ comment.author_name }} говорит:

+
+ {% endif %} +

{{ comment.comment_text }}

+
+ Ответить +
+ + {% endfor %} + + +
+ {% csrf_token %} +
+ + +
+
+ + +
+
+
+ +
+ +
+
+
+ +
+ {% endautoescape %}