From 5d55853d7fa8d0f2e9c1ccaa5f0ac71e3333a706 Mon Sep 17 00:00:00 2001 From: Bobby Date: Tue, 20 Sep 2022 21:01:14 -0400 Subject: Ability to edit comments --- blog/urls.py | 1 + blog/views.py | 26 ++++++++++++++++++++++++++ templates/blog/post.html | 31 ++++++++++++++++++++++++++++++- 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/blog/urls.py b/blog/urls.py index 2b4f8b6d..09161c16 100644 --- a/blog/urls.py +++ b/blog/urls.py @@ -12,5 +12,6 @@ urlpatterns = [ path('register/refresh_captcha/', name='refresh_captcha', view=views.refresh_captcha), path('post/', views.post, name='post'), path('post//comment', views.comment, name='comment'), + path('post//edit_comment', views.edit_comment, name='edit_comment'), # path('my/homepage', views.homepage, name='homepage'), ] diff --git a/blog/views.py b/blog/views.py index e8206ce2..6930e6b4 100644 --- a/blog/views.py +++ b/blog/views.py @@ -1,3 +1,4 @@ +from datetime import datetime from django.shortcuts import render, redirect from django.http import HttpResponse from users.models import UserProfile, CaptchaStore @@ -117,3 +118,28 @@ def comment(request, slug): return HttpResponse('Post not found!', status=404) except Post.DoesNotExist: return HttpResponse('Post not found!', status=404) + + else: + return redirect('blog:home') + else: + return redirect('blog:home') + +def edit_comment(request, slug): + if request.method == 'POST': + if request.user.is_authenticated: + try: + comment = Comment.objects.get(id=request.POST.get('comment_id')) + if comment.user == request.user: + comment.body = request.POST.get('body') + comment.edited = True + comment.edited_at = datetime.now() + comment.save() + return redirect('blog:post', slug=slug) + else: + return HttpResponse('Unauthorized!', status=401) + except Comment.DoesNotExist: + return HttpResponse('Comment not found!', status=404) + else: + return redirect('blog:home') + else: + return redirect('blog:home') diff --git a/templates/blog/post.html b/templates/blog/post.html index 9116daeb..712d7168 100644 --- a/templates/blog/post.html +++ b/templates/blog/post.html @@ -28,8 +28,26 @@

{{ comment.user.username }} on {{ comment.created_at | date:"M d, Y" }} + {% if comment.edited %} + (Edited) + {% endif %} + {% if comment.user == user %} +   •   + Edit +   •   + Delete + {% endif %} +

+

+ {{ comment.body }} +

-

{{ comment.body }}

{% endfor %} @@ -50,5 +68,16 @@ + {% endblock %} -- cgit v1.2.3