Skip to content

Commit 5cd2916

Browse files
committed
allow to make comments for non-abstract models by COMMENTS_FOR_CONCRETE_MODEL setting
1 parent c5014f3 commit 5cd2916

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

django_comments/forms.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,12 @@ def get_comment_create_data(self, site_id=None):
137137
custom comment apps that override get_comment_model can override this
138138
method to add extra fields onto a custom comment model.
139139
"""
140+
COMMENTS_FOR_CONCRETE_MODEL = getattr(settings, 'COMMENTS_FOR_CONCRETE_MODEL', True)
140141
return dict(
141-
content_type=ContentType.objects.get_for_model(self.target_object),
142+
content_type=ContentType.objects.get_for_model(
143+
self.target_object,
144+
for_concrete_model=COMMENTS_FOR_CONCRETE_MODEL,
145+
),
142146
object_pk=force_str(self.target_object._get_pk_val()),
143147
user_name=self.cleaned_data["name"],
144148
user_email=self.cleaned_data["email"],

tests/testapp/models.py

+17
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""
55

66
from django.db import models
7+
import uuid
78

89

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

1617

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

23+
def save(self, *args, **kwargs):
24+
import pudb; pudb.set_trace()
25+
if not self.uuid:
26+
self.uuid = uuid.uuid4()
27+
2128
def __str__(self):
2229
return self.headline
2330

2431

32+
class UUIDArticle(Article):
33+
""" Override _get_pk_val to use UUID as PK """
34+
35+
def _get_pk_val(self, meta=None):
36+
return self.uuid
37+
38+
class Meta:
39+
proxy = True
40+
41+
2542
class Entry(models.Model):
2643
title = models.CharField(max_length=250)
2744
body = models.TextField()

tests/testapp/tests/test_comment_form.py

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import time
22

33
from django.conf import settings
4+
from django.contrib.contenttypes.models import ContentType
45
from django.contrib.sites.models import Site
56

67
from django_comments.forms import CommentForm
78
from django_comments.models import Comment
89

910
from . import CommentTestCase
10-
from testapp.models import Article
11+
from testapp.models import UUIDArticle, Article
12+
from django.test.utils import override_settings
1113

1214

1315
class CommentFormTests(CommentTestCase):
@@ -75,6 +77,34 @@ def testGetCommentObject(self):
7577
c = f.get_comment_object(site_id=self.site_2.id)
7678
self.assertEqual(c.site_id, self.site_2.id)
7779

80+
def testGetCommentCreateData(self):
81+
"""
82+
Test that get_comment_create_data() returns
83+
content_type for Article even if the proxy model UUIDArticle
84+
is set to CommentForm.
85+
"""
86+
a = UUIDArticle.objects.get(pk=1)
87+
d = self.getValidData(a)
88+
d["comment"] = "testGetCommentObject with a site"
89+
f = CommentForm(UUIDArticle.objects.get(pk=1), data=d)
90+
self.assertTrue(f.is_valid())
91+
c = f.get_comment_create_data(site_id=self.site_2.id)
92+
self.assertEqual(c["content_type"], ContentType.objects.get_for_model(Article, for_concrete_model=False))
93+
94+
@override_settings(COMMENTS_FOR_CONCRETE_MODEL=False)
95+
def testGetCommentCreateDataConcreteModel(self):
96+
"""
97+
Test that get_comment_create_data() returns
98+
content_type for UUIDArticle if COMMENTS_FOR_CONCRETE_MODEL is False.
99+
"""
100+
a = UUIDArticle.objects.get(pk=1)
101+
d = self.getValidData(a)
102+
d["comment"] = "testGetCommentObject with a site"
103+
f = CommentForm(UUIDArticle.objects.get(pk=1), data=d)
104+
self.assertTrue(f.is_valid())
105+
c = f.get_comment_create_data(site_id=self.site_2.id)
106+
self.assertEqual(c["content_type"], ContentType.objects.get_for_model(UUIDArticle, for_concrete_model=False))
107+
78108
def testProfanities(self):
79109
"""Test COMMENTS_ALLOW_PROFANITIES and PROFANITIES_LIST settings"""
80110
a = Article.objects.get(pk=1)

0 commit comments

Comments
 (0)