Skip to content

Commit

Permalink
Merge branch 'task-scheduler' of https://github.com/SahilDhillon21/BLT
Browse files Browse the repository at this point in the history
…into task-scheduler
  • Loading branch information
SahilDhillon21 committed Jan 9, 2025
2 parents 914f386 + e0ea993 commit 9cbc3e9
Show file tree
Hide file tree
Showing 12 changed files with 249 additions and 90 deletions.
25 changes: 13 additions & 12 deletions blt/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@
SpecificIssuesView,
UpdateIssue,
change_bid_status,
comment_on_issue,
comment_on_content,
create_github_issue,
delete_comment,
delete_content_comment,
delete_issue,
dislike_issue,
fetch_current_bid,
Expand All @@ -120,7 +120,7 @@
submit_bug,
submit_pr,
unsave_issue,
update_comment,
update_content_comment,
vote_count,
)
from website.views.organization import (
Expand Down Expand Up @@ -458,19 +458,20 @@
),
re_path(r"^issue/edit/$", IssueEdit, name="edit_issue"),
re_path(r"^issue/update/$", UpdateIssue, name="update_issue"),
# comment on content
path(
"issue/<str:issue_pk>/comment/",
comment_on_issue,
name="comment_on_issue",
"content/<str:content_pk>/comment/",
comment_on_content,
name="comment_on_content",
),
# UPDATE COMMENT
# update comment
path(
"issue/<str:issue_pk>/comment/update/<str:comment_pk>/",
update_comment,
name="update_comment",
"content/<str:content_pk>/comment/update/<str:comment_pk>/",
update_content_comment,
name="update_content_comment",
),
# delete_comment
path("issue2/comment/delete/", delete_comment, name="delete_comment"),
# delete comment
path("content/comment/delete/", delete_content_comment, name="delete_content_comment"),
re_path(r"^issue/(?P<slug>\w+)/$", IssueView.as_view(), name="issue_view"),
re_path(r"^follow/(?P<user>[^/]+)/", follow_user, name="follow_user"),
re_path(r"^all_activity/$", AllIssuesView.as_view(), name="all_activity"),
Expand Down
7 changes: 6 additions & 1 deletion comments/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@


class MyCommentsAdmin(admin.ModelAdmin):
list_display = ("id", "author", "issue", "text", "created_date")
list_display = ("id", "author", "get_related_object", "text", "created_date")

def get_related_object(self, obj):
return obj.content_object

get_related_object.short_description = "Related Object"


admin.site.register(Comment, MyCommentsAdmin)
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Generated by Django 5.1.4 on 2025-01-04 19:10

import django.db.models.deletion
import django.utils.timezone
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("comments", "0006_comment_author_fk"),
("contenttypes", "0002_remove_content_type_name"),
]

operations = [
migrations.RemoveField(
model_name="comment",
name="issue",
),
migrations.AddField(
model_name="comment",
name="content_type",
field=models.ForeignKey(
default=1,
on_delete=django.db.models.deletion.CASCADE,
to="contenttypes.contenttype",
),
preserve_default=False,
),
migrations.AddField(
model_name="comment",
name="object_id",
field=models.PositiveIntegerField(default=1),
preserve_default=False,
),
]
8 changes: 6 additions & 2 deletions comments/models.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.utils import timezone

from website.models import Issue, UserProfile
from website.models import UserProfile

# Create your models here.


class Comment(models.Model):
parent = models.ForeignKey("self", null=True, on_delete=models.CASCADE)
issue = models.ForeignKey(Issue, on_delete=models.CASCADE, related_name="comments")
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey("content_type", "object_id")
author = models.CharField(max_length=200)
author_fk = models.ForeignKey(UserProfile, null=True, on_delete=models.SET_NULL)
author_url = models.CharField(max_length=200)
Expand Down
4 changes: 3 additions & 1 deletion website/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from colorthief import ColorThief
from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
from django.contrib.contenttypes.models import ContentType
from django.core.cache import cache
from django.core.exceptions import ValidationError
Expand Down Expand Up @@ -362,6 +362,7 @@ class Issue(models.Model):
cve_id = models.CharField(max_length=16, null=True, blank=True)
cve_score = models.DecimalField(max_digits=2, decimal_places=1, null=True, blank=True)
tags = models.ManyToManyField(Tag, blank=True)
comments = GenericRelation("comments.Comment")

def __unicode__(self):
return self.description
Expand Down Expand Up @@ -1204,6 +1205,7 @@ class Post(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
image = models.ImageField(upload_to="blog_posts")
comments = GenericRelation("comments.Comment")

class Meta:
db_table = "blog_post"
Expand Down
1 change: 1 addition & 0 deletions website/templates/blog/post_details.html
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,6 @@ <h1 class="post-title">{{ post.title }}</h1>
<a href="{% url 'post_delete' slug=post.slug %}" class="b-delete">Delete Post</a>
</div>
{% endif %}
<div class="px-10 rounded-lg bg-white">{% include "../comments2.html" %}</div>
</article>
{% endblock content %}
72 changes: 23 additions & 49 deletions website/templates/comments2.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ <h2 class="text-xl lg:text-3xl font-bold text-gray-900 ">Comments ({{ all_commen
<div class="py-2 px-4 mb-4 bg-white rounded-lg rounded-t-lg border border-gray-200 ">
<label for="comment" class="sr-only">Your comment</label>
<input type="text" id="replying_to_input" hidden>
<input type="hidden"
id="content_type"
name="content_type"
value="{{ content_type }}">
<div id="replying_to_root"></div>
<div id="add_update_label"></div>
<textarea id="comment"
Expand Down Expand Up @@ -82,7 +86,7 @@ <h2 class="text-xl lg:text-3xl font-bold text-gray-900 ">Comments ({{ all_commen
data-name="{{ comment.id }}">Edit</a>
</li>
<li>
<a class="block py-2 px-4 hover:bg-gray-100 del_comment_issue2 cursor-pointer"
<a class="block py-2 px-4 hover:bg-gray-100 del_comment cursor-pointer"
data-name="{{ comment.id }}">Delete</a>
</li>
<li>
Expand Down Expand Up @@ -151,7 +155,7 @@ <h2 class="text-xl lg:text-3xl font-bold text-gray-900 ">Comments ({{ all_commen
data-name="{{ child_comment.id }}">Edit</a>
</li>
<li>
<a class="block py-2 px-4 hover:bg-gray-100 del_comment_issue2 cursor-pointer"
<a class="block py-2 px-4 hover:bg-gray-100 del_comment cursor-pointer"
data-name="{{ child_comment.id }}">Delete</a>
</li>
<li>
Expand All @@ -162,12 +166,6 @@ <h2 class="text-xl lg:text-3xl font-bold text-gray-900 ">Comments ({{ all_commen
{% endif %}
</footer>
<p class="text-xl main_text">{{ child_comment.text | safe }}</p>
<!-- <div class="flex items-center mt-4 space-x-4">
<button type="button" class="flex items-center text-md text-gray-500 hover:underline ">
<svg aria-hidden="true" class="mr-1 w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 12h.01M12 12h.01M16 12h.01M21 12c0 4.418-4.03 8-9 8a9.863 9.863 0 01-4.255-.949L3 20l1.395-3.72C3.512 15.042 3 13.574 3 12c0-4.418 4.03-8 9-8s9 3.582 9 8z"></path></svg>
{{ child_comment.children | length }} Replies
</button>
</div> -->
</article>
{% endfor %}
</div>
Expand All @@ -192,7 +190,6 @@ <h2 class="text-xl lg:text-3xl font-bold text-gray-900 ">Comments ({{ all_commen
});
}


function remove_reply(){
document.getElementById("replying_to_cont").remove();
document.getElementById("replying_to_input").value = "";
Expand Down Expand Up @@ -221,9 +218,10 @@ <h2 class="text-xl lg:text-3xl font-bold text-gray-900 ">Comments ({{ all_commen
comment_container.toggleAttribute('hidden');
}

function comment_on_issue(e){
function comment_on_content(e){
e.preventDefault();
let issue_pk = '{{ object.pk|safe }}';
let content_pk = '{{ object.pk|safe }}';
let content_type = '{{ content_type }}';
var csrftoken = $("[name=csrfmiddlewaretoken]").val(); // Get the CSRF token value

let replying_to_input = document.getElementById("replying_to_input").value;
Expand All @@ -233,25 +231,26 @@ <h2 class="text-xl lg:text-3xl font-bold text-gray-900 ">Comments ({{ all_commen
}
$.ajax({
type: 'POST',
url: '/issue/' + issue_pk + '/comment/',
url: '/content/' + content_pk + '/comment/',
data: {
"replying_to_input":replying_to_input,
"comment":comment
"comment":comment,
"content_type": content_type
},
beforeSend: function(xhr, settings) {
xhr.setRequestHeader("X-CSRFToken", csrftoken); // Set the CSRF token in the request header
},
success: function (data) {
$('#comment_root').replaceWith(data);
// initDropDown();
location.reload()
},
});
}
$('#post_comment_btn').click((e)=>comment_on_issue(e))
$('#post_comment_btn').click((e)=>comment_on_content(e))
function update_comment(e,comment_id){
e.preventDefault();
let issue_pk = '{{ object.pk|safe }}';
let content_pk = '{{ object.pk|safe }}';
let content_type = '{{ content_type }}';
var csrftoken = $("[name=csrfmiddlewaretoken]").val(); // Get the CSRF token value

let comment = document.getElementById("comment").value;
Expand All @@ -260,16 +259,16 @@ <h2 class="text-xl lg:text-3xl font-bold text-gray-900 ">Comments ({{ all_commen
}
$.ajax({
type: 'POST',
url: '/issue/' + issue_pk + '/comment/update/' + comment_id + '/',
url: '/content/' + content_pk + '/comment/update/' + comment_id + '/',
data: {
"comment":comment
"comment":comment,
"content_type": content_type
},
beforeSend: function(xhr, settings) {
xhr.setRequestHeader("X-CSRFToken", csrftoken); // Set the CSRF token in the request header
},
success: function (data) {
$('#comment_root').replaceWith(data);
// initDropDown();
location.reload()
},
});
Expand All @@ -281,15 +280,13 @@ <h2 class="text-xl lg:text-3xl font-bold text-gray-900 ">Comments ({{ all_commen
$('#update_comment_btn').text('Post comment');
$('#update_comment_btn').attr('id', 'post_comment_btn');
$('#update_comment_btn').off('click');
$('#post_comment_btn').off('click').on('click', (e) => comment_on_issue(e));
$('#post_comment_btn').off('click').on('click', (e) => comment_on_content(e));
}
$('body').on('click', '.edit_comment', function (e) {
e.preventDefault();
let comment_id = $(this).data('name');
let comment_text = $(this).closest('article').find('.main_text').text();
// Unbind the click event for the post_comment_btn
$('#post_comment_btn').off('click');
//user can update the form using #comments form
$('#comment').val(comment_text);
$('#post_comment_btn').text('Update comment');
$('#post_comment_btn').attr('id', 'update_comment_btn');
Expand All @@ -298,59 +295,36 @@ <h2 class="text-xl lg:text-3xl font-bold text-gray-900 ">Comments ({{ all_commen
<p>Update your Comment<p>
<i onclick="remove_update_label()" class="fa-solid fa-xmark ml-3 text-3xl font-bold text-gray-600 hover:text-black"></i>
</div>`;
// 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) {
$('body').off('click', '.del_comment').on('click', '.del_comment', 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: "/issue/comment/delete/",
url: "/content/comment/delete/",
data: {
"comment_pk": clickedElement.attr('data-name'),
"issue_pk": '{{ object.pk|safe }}',
"content_pk": '{{ object.pk|safe }}',
"content_type": '{{ content_type }}'
},
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);

// initDropDown();
location.reload()
},
});
}
});
// refresh comments after x interval
// function get_comments(){
// let issue_pk = '{{ object.pk|safe }}';

// $.ajax({
// type: 'GET',
// url: '/issue/' + issue_pk + '/comment/',
// data: {},
// success: function (data) {
// $('#comment_root').html(data);
// }
// });
// }

// setInterval(get_comments, 10000);

$(document).ready(function(){
// bind event handlers when the page loads.
initDropDown();
});



</script>
</section>
4 changes: 2 additions & 2 deletions website/templates/sitemap.html
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,11 @@ <h2 class="text-6xl text-black font-bold">Sitemap</h2>
</li>
<li class="flex items-center space-x-3 text-black hover:text-red-600 hover:translate-x-1 transition duration-200 cursor-pointer">
<i class="fas fa-comment-dots w-5 h-5 mr-1 align-middle"></i>
<a href="{% url 'comment_on_issue' 1 %}">Comment on Issue // 1</a>
<a href="{% url 'comment_on_content' 1 %}">Comment on Content // 1</a>
</li>
<li class="flex items-center space-x-3 text-black hover:text-red-600 hover:translate-x-1 transition duration-200 cursor-pointer">
<i class="fas fa-sync-alt w-5 h-5 mr-1 align-middle"></i>
<a href="{% url 'update_comment' 1 1 %}">Update Comment // 1</a>
<a href="{% url 'update_content_comment' 1 1 %}">Update Comment // 1</a>
</li>
<li class="flex items-center space-x-3 text-black hover:text-red-600 hover:translate-x-1 transition duration-200 cursor-pointer">
<i class="fas fa-trash-alt w-5 h-5 mr-1 align-middle"></i>
Expand Down
Loading

0 comments on commit 9cbc3e9

Please sign in to comment.