Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding post coments deletion feature at issue2 page #1470

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions blt/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@
path("issue/<str:issue_pk>/comment/", website.views.comment_on_issue, name="comment_on_issue"),
# UPDATE COMMENT
path("issue/<str:issue_pk>/comment/update/<str:comment_pk>/", website.views.update_comment, name="update_comment"),
# delete_comment
path("issue2/comment/delete/", website.views.delete_comment, name="delete_comment"),
re_path(r"^issue/(?P<slug>\w+)/$", IssueView.as_view(), name="issue_view"),
re_path(r"^issue2/(?P<slug>\w+)/$", IssueView2.as_view(), name="issue_view2"),
re_path(r"^follow/(?P<user>[^/]+)/", website.views.follow_user, name="follow_user"),
Expand Down
46 changes: 41 additions & 5 deletions website/templates/comments2.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ <h2 class="text-xl lg:text-3xl font-bold text-gray-900 ">Comments ({{ all_commen
<a class="block py-2 px-4 hover:bg-gray-100 edit_comment cursor-pointer" data-name="{{comment.id}}">Edit</a>
</li>
<li>
<a href="#"
class="block py-2 px-4 hover:bg-gray-100 ">Delete</a>
<a class="block py-2 px-4 hover:bg-gray-100 del_comment_issue2 cursor-pointer" data-name="{{ comment.id }}">Delete</a>
</li>
<li>
<a href="#"
Expand Down Expand Up @@ -139,8 +138,7 @@ <h2 class="text-xl lg:text-3xl font-bold text-gray-900 ">Comments ({{ all_commen
<a class="block py-2 px-4 hover:bg-gray-100 edit_comment cursor-pointer" data-name="{{child_comment.id}}">Edit</a>
</li>
<li>
<a href="#"
class="block py-2 px-4 hover:bg-gray-100 ">Delete</a>
<a class="block py-2 px-4 hover:bg-gray-100 del_comment_issue2 cursor-pointer" data-name="{{ child_comment.id }}">Delete</a>
</li>
<li>
<a href="#"
Expand Down Expand Up @@ -221,7 +219,7 @@ <h2 class="text-xl lg:text-3xl font-bold text-gray-900 ">Comments ({{ all_commen
xhr.setRequestHeader("X-CSRFToken", csrftoken); // Set the CSRF token in the request header
},
success: function (data) {
$('#comment_root').html(data);
$('#comment_root').replaceWith(data);
document.querySelectorAll("[data-dropdown-toggle]").forEach((toggle) => {
toggle.addEventListener("click", () => {
const { dropdownToggle } = toggle.dataset;
Expand Down Expand Up @@ -300,6 +298,44 @@ <h2 class="text-xl lg:text-3xl font-bold text-gray-900 ">Comments ({{ all_commen
// Bind a new click event for the update_comment_btn
$('#update_comment_btn').off('click').on('click', (e) => update_comment(e, comment_id));
});
$('body').off('click', '.del_comment_issue2').on('click', '.del_comment_issue2', function (e) {
e.preventDefault();

// Store a reference to the clicked element
var clickedElement = $(this);
if (confirm("Delete this comment?") == true) {
var csrftoken = $("[name=csrfmiddlewaretoken]").val();

$.ajax({
type: 'POST',
url: "/issue2/comment/delete/",
data: {
"comment_pk": clickedElement.attr('data-name'),
"issue_pk": '{{ object.pk|safe }}',
},
beforeSend: function (xhr, settings) {
xhr.setRequestHeader("X-CSRFToken", csrftoken); // Set the CSRF token in the request header
},
success: function (data) {
// Replace the content of #comment_root with the new data
$('#comment_root').replaceWith(data);

// Rest of your logic to toggle the dropdown
document.querySelectorAll("[data-dropdown-toggle]").forEach((toggle) => {
toggle.addEventListener("click", () => {
const { dropdownToggle } = toggle.dataset;
const dropdown = document.getElementById(dropdownToggle);
dropdown.classList.toggle("hidden");
dropdown.classList.toggle("block");
dropdown.style.position = "absolute";
dropdown.style.top = toggle.offsetTop + toggle.offsetHeight + "px";
dropdown.style.left = toggle.offsetLeft - 50 + "px";
});
});
},
});
}
});
// refresh comments after x interval
// function get_comments(){
// let issue_pk = '{{ object.pk|safe }}';
Expand Down
19 changes: 18 additions & 1 deletion website/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2031,7 +2031,6 @@ def comment_on_issue(request, issue_pk):
}

return render(request, "comments2.html",context)

# get issue and comment id from url
def update_comment(request, issue_pk, comment_pk):
issue = Issue.objects.filter(pk=issue_pk).first()
Expand All @@ -2040,6 +2039,24 @@ def update_comment(request, issue_pk, comment_pk):

comment.text = request.POST.get("comment","")
comment.save()
def delete_comment(request):
int_issue_pk = int(request.POST['issue_pk'])
issue = Issue.objects.get(pk=int_issue_pk)
if request.method == "POST":
all_comment = Comment.objects.filter(issue=issue)
comment = Comment.objects.get(pk=int(request.POST['comment_pk']))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also add user to the query

if request.user.username != comment.author:
return HttpResponse("You are not authorized to delete this comment")
try:
show = comment.parent.pk
except:
show = -1
comment.delete()
context = {
"all_comment": Comment.objects.filter(issue__id=int_issue_pk).order_by("-created_date"),
"object": issue,
}
return render(request, "comments2.html", context)

context = {
"all_comment": Comment.objects.filter(issue__id=issue_pk).order_by("-created_date"),
Expand Down