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 1 commit
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 @@ -310,6 +310,8 @@
re_path(r"^issue/edit/$", website.views.IssueEdit, name="edit_issue"),
re_path(r"^issue/update/$", website.views.UpdateIssue, name="update_issue"),
path("issue/<str:issue_pk>/comment/", website.views.comment_on_issue, name="comment_on_issue"),
# 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
45 changes: 40 additions & 5 deletions website/templates/comments2.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ <h2 class="text-xl lg:text-3xl font-bold text-gray-900 ">Comments ({{ all_commen
class="block py-2 px-4 hover:bg-gray-100 ">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 @@ -138,8 +137,7 @@ <h2 class="text-xl lg:text-3xl font-bold text-gray-900 ">Comments ({{ all_commen
class="block py-2 px-4 hover:bg-gray-100 ">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 @@ -220,7 +218,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 All @@ -237,7 +235,44 @@ <h2 class="text-xl lg:text-3xl font-bold text-gray-900 ">Comments ({{ all_commen
});
}
$('#post_comment_btn').click((e)=>comment_on_issue(e))
$('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
18 changes: 18 additions & 0 deletions website/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2032,6 +2032,24 @@ def comment_on_issue(request, issue_pk):

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

def delete_comment(request):
issue = Issue.objects.get(pk=request.POST['issue_pk'])
Copy link
Collaborator

Choose a reason for hiding this comment

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

Check this is an int

if request.method == "POST":
all_comment = Comment.objects.filter(issue=issue)
print(request.POST['issue_pk'])
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
print(request.POST['issue_pk'])

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm very sorry sir ,i didn't notice that

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

try:
show = comment.parent.pk
except:
show = -1
if request.user.username != comment.author:
return HttpResponse("Cannot delete this comment")
comment.delete()
context = {
"all_comment": Comment.objects.filter(issue__id=int(request.POST['issue_pk'])).order_by("-created_date"),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Reuse the query from above

"object": issue,
}
return render(request, "comments2.html", context)

class CustomObtainAuthToken(ObtainAuthToken):
def post(self, request, *args, **kwargs):
Expand Down