diff --git a/blt/urls.py b/blt/urls.py index c59da8c4b..eda5e9977 100644 --- a/blt/urls.py +++ b/blt/urls.py @@ -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, @@ -120,7 +120,7 @@ submit_bug, submit_pr, unsave_issue, - update_comment, + update_content_comment, vote_count, ) from website.views.organization import ( @@ -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//comment/", - comment_on_issue, - name="comment_on_issue", + "content//comment/", + comment_on_content, + name="comment_on_content", ), - # UPDATE COMMENT + # update comment path( - "issue//comment/update//", - update_comment, - name="update_comment", + "content//comment/update//", + 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\w+)/$", IssueView.as_view(), name="issue_view"), re_path(r"^follow/(?P[^/]+)/", follow_user, name="follow_user"), re_path(r"^all_activity/$", AllIssuesView.as_view(), name="all_activity"), diff --git a/comments/admin.py b/comments/admin.py index cd523ddba..543c2f553 100644 --- a/comments/admin.py +++ b/comments/admin.py @@ -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) diff --git a/comments/migrations/0007_remove_comment_issue_comment_content_type_and_more.py b/comments/migrations/0007_remove_comment_issue_comment_content_type_and_more.py new file mode 100644 index 000000000..245979f8e --- /dev/null +++ b/comments/migrations/0007_remove_comment_issue_comment_content_type_and_more.py @@ -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, + ), + ] diff --git a/comments/models.py b/comments/models.py index 2493fca80..767b51ac1 100644 --- a/comments/models.py +++ b/comments/models.py @@ -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) diff --git a/website/models.py b/website/models.py index 4e4735a7b..2d0168710 100644 --- a/website/models.py +++ b/website/models.py @@ -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 @@ -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 @@ -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" diff --git a/website/templates/blog/post_details.html b/website/templates/blog/post_details.html index 5cd2399ad..5c2e01342 100644 --- a/website/templates/blog/post_details.html +++ b/website/templates/blog/post_details.html @@ -119,5 +119,6 @@

{{ post.title }}

Delete Post {% endif %} +
{% include "../comments2.html" %}
{% endblock content %} diff --git a/website/templates/comments2.html b/website/templates/comments2.html index 730507ad8..5211a942c 100644 --- a/website/templates/comments2.html +++ b/website/templates/comments2.html @@ -9,6 +9,10 @@

Comments ({{ all_commen
+