Skip to content

Commit

Permalink
allow to make comments for non-abstract models by COMMENTS_FOR_CONCRE…
Browse files Browse the repository at this point in the history
…TE_MODEL setting
  • Loading branch information
PetrDlouhy committed Jul 7, 2022
1 parent c5014f3 commit 5cd2916
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
6 changes: 5 additions & 1 deletion django_comments/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,12 @@ def get_comment_create_data(self, site_id=None):
custom comment apps that override get_comment_model can override this
method to add extra fields onto a custom comment model.
"""
COMMENTS_FOR_CONCRETE_MODEL = getattr(settings, 'COMMENTS_FOR_CONCRETE_MODEL', True)
return dict(
content_type=ContentType.objects.get_for_model(self.target_object),
content_type=ContentType.objects.get_for_model(
self.target_object,
for_concrete_model=COMMENTS_FOR_CONCRETE_MODEL,
),
object_pk=force_str(self.target_object._get_pk_val()),
user_name=self.cleaned_data["name"],
user_email=self.cleaned_data["email"],
Expand Down
17 changes: 17 additions & 0 deletions tests/testapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""

from django.db import models
import uuid


class Author(models.Model):
Expand All @@ -15,13 +16,29 @@ def __str__(self):


class Article(models.Model):
uuid = models.UUIDField(editable=False, null=True)
author = models.ForeignKey(Author, on_delete=models.CASCADE)
headline = models.CharField(max_length=100)

def save(self, *args, **kwargs):
import pudb; pudb.set_trace()
if not self.uuid:
self.uuid = uuid.uuid4()

def __str__(self):
return self.headline


class UUIDArticle(Article):
""" Override _get_pk_val to use UUID as PK """

def _get_pk_val(self, meta=None):
return self.uuid

class Meta:
proxy = True


class Entry(models.Model):
title = models.CharField(max_length=250)
body = models.TextField()
Expand Down
32 changes: 31 additions & 1 deletion tests/testapp/tests/test_comment_form.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import time

from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.contrib.sites.models import Site

from django_comments.forms import CommentForm
from django_comments.models import Comment

from . import CommentTestCase
from testapp.models import Article
from testapp.models import UUIDArticle, Article
from django.test.utils import override_settings


class CommentFormTests(CommentTestCase):
Expand Down Expand Up @@ -75,6 +77,34 @@ def testGetCommentObject(self):
c = f.get_comment_object(site_id=self.site_2.id)
self.assertEqual(c.site_id, self.site_2.id)

def testGetCommentCreateData(self):
"""
Test that get_comment_create_data() returns
content_type for Article even if the proxy model UUIDArticle
is set to CommentForm.
"""
a = UUIDArticle.objects.get(pk=1)
d = self.getValidData(a)
d["comment"] = "testGetCommentObject with a site"
f = CommentForm(UUIDArticle.objects.get(pk=1), data=d)
self.assertTrue(f.is_valid())
c = f.get_comment_create_data(site_id=self.site_2.id)
self.assertEqual(c["content_type"], ContentType.objects.get_for_model(Article, for_concrete_model=False))

@override_settings(COMMENTS_FOR_CONCRETE_MODEL=False)
def testGetCommentCreateDataConcreteModel(self):
"""
Test that get_comment_create_data() returns
content_type for UUIDArticle if COMMENTS_FOR_CONCRETE_MODEL is False.
"""
a = UUIDArticle.objects.get(pk=1)
d = self.getValidData(a)
d["comment"] = "testGetCommentObject with a site"
f = CommentForm(UUIDArticle.objects.get(pk=1), data=d)
self.assertTrue(f.is_valid())
c = f.get_comment_create_data(site_id=self.site_2.id)
self.assertEqual(c["content_type"], ContentType.objects.get_for_model(UUIDArticle, for_concrete_model=False))

def testProfanities(self):
"""Test COMMENTS_ALLOW_PROFANITIES and PROFANITIES_LIST settings"""
a = Article.objects.get(pk=1)
Expand Down

0 comments on commit 5cd2916

Please sign in to comment.