From 96c79f573a73983403124f06cbc9784e2d6e9512 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Dlouh=C3=BD?= Date: Wed, 29 Sep 2021 11:07:24 +0200 Subject: [PATCH 1/3] fix to enable using UUIDs/non-default PKs --- django_comments_xtd/api/frontend.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/django_comments_xtd/api/frontend.py b/django_comments_xtd/api/frontend.py index 10dffd0a..d77c0b57 100644 --- a/django_comments_xtd/api/frontend.py +++ b/django_comments_xtd/api/frontend.py @@ -61,7 +61,7 @@ def _reverse(*args, **kwargs): form = CommentSecurityForm(obj) ctype = ContentType.objects.get_for_model(obj) queryset = XtdComment.objects.filter(content_type=ctype, - object_pk=obj.pk, + object_pk=obj._get_pk_val(), site__pk=get_current_site_id(request), is_public=True) ctype_slug = "%s-%s" % (ctype.app_label, ctype.model) @@ -86,10 +86,10 @@ def _reverse(*args, **kwargs): "flag_url": _reverse("comments-flag", args=(0,)), "list_url": _reverse('comments-xtd-api-list', kwargs={'content_type': ctype_slug, - 'object_pk': obj.id}), + 'object_pk': obj._get_pk_val()}), "count_url": _reverse('comments-xtd-api-count', kwargs={'content_type': ctype_slug, - 'object_pk': obj.id}), + 'object_pk': obj._get_pk_val()}), "send_url": _reverse("comments-xtd-api-create"), "preview_url": _reverse("comments-xtd-api-preview"), "form": { From bf9552f4f1704bb5f3f0275eee8d308415583aa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Dlouh=C3=BD?= Date: Thu, 30 Sep 2021 12:08:25 +0200 Subject: [PATCH 2/3] simplify getting content_type for comment --- django_comments_xtd/api/serializers.py | 3 +-- django_comments_xtd/utils.py | 3 +-- django_comments_xtd/views.py | 6 +++--- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/django_comments_xtd/api/serializers.py b/django_comments_xtd/api/serializers.py index 71d004f2..5661abc0 100644 --- a/django_comments_xtd/api/serializers.py +++ b/django_comments_xtd/api/serializers.py @@ -1,5 +1,4 @@ from django.apps import apps -from django.contrib.contenttypes.models import ContentType from django.contrib.sites.shortcuts import get_current_site from django.utils import formats, timezone from django.utils.html import escape @@ -210,7 +209,7 @@ def validate(self, data): elif data['flag'] == 'report': option = 'allow_flagging' comment = data['comment'] - ctype = ContentType.objects.get_for_model(comment.content_object) + ctype = comment.content_type key = "%s.%s" % (ctype.app_label, ctype.model) if not get_app_model_options(content_type=key)[option]: raise serializers.ValidationError( diff --git a/django_comments_xtd/utils.py b/django_comments_xtd/utils.py index aa138f70..5b429c0c 100644 --- a/django_comments_xtd/utils.py +++ b/django_comments_xtd/utils.py @@ -14,7 +14,6 @@ from urllib import urlencode from django.core.mail import EmailMultiAlternatives -from django.contrib.contenttypes.models import ContentType from django.contrib.sites.shortcuts import get_current_site from django.utils.crypto import salted_hmac @@ -86,7 +85,7 @@ def get_app_model_options(comment=None, content_type=None): default = copy(settings.COMMENTS_XTD_APP_MODEL_OPTIONS['default']) if comment: - content_type = ContentType.objects.get_for_model(comment.content_object) + content_type = comment.content_type key = "%s.%s" % (content_type.app_label, content_type.model) elif content_type: key = content_type diff --git a/django_comments_xtd/views.py b/django_comments_xtd/views.py index 3b4e186c..ee653be0 100644 --- a/django_comments_xtd/views.py +++ b/django_comments_xtd/views.py @@ -391,7 +391,7 @@ def flag(request, comment_id, next=None): get_comment_model(), pk=comment_id, site__pk=get_current_site_id(request)) if not get_app_model_options(comment=comment)['allow_flagging']: - ctype = ContentType.objects.get_for_model(comment.content_object) + ctype = comment.content_type raise Http404("Comments posted to instances of '%s.%s' are not " "explicitly allowed to receive 'removal suggestion' " "flags. Check the COMMENTS_XTD_APP_MODEL_OPTIONS " @@ -423,7 +423,7 @@ def like(request, comment_id, next=None): comment = get_object_or_404(get_comment_model(), pk=comment_id, site__pk=get_current_site_id(request)) if not get_app_model_options(comment=comment)['allow_feedback']: - ctype = ContentType.objects.get_for_model(comment.content_object) + ctype = comment.content_type raise Http404("Comments posted to instances of '%s.%s' are not " "explicitly allowed to receive 'liked it' flags. " "Check the COMMENTS_XTD_APP_MODEL_OPTIONS " @@ -459,7 +459,7 @@ def dislike(request, comment_id, next=None): comment = get_object_or_404(get_comment_model(), pk=comment_id, site__pk=get_current_site_id(request)) if not get_app_model_options(comment=comment)['allow_feedback']: - ctype = ContentType.objects.get_for_model(comment.content_object) + ctype = comment.content_type raise Http404("Comments posted to instances of '%s.%s' are not " "explicitly allowed to receive 'disliked it' flags. " "Check the COMMENTS_XTD_APP_MODEL_OPTIONS " From 51a1844b95db52ef3b64d5367fdde206d5b2ffaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Dlouh=C3=BD?= Date: Thu, 30 Sep 2021 14:47:13 +0200 Subject: [PATCH 3/3] pass for_concrete_model settings to ContentType --- django_comments_xtd/api/frontend.py | 8 +++++++- django_comments_xtd/models.py | 4 ++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/django_comments_xtd/api/frontend.py b/django_comments_xtd/api/frontend.py index d77c0b57..fb15d5a3 100644 --- a/django_comments_xtd/api/frontend.py +++ b/django_comments_xtd/api/frontend.py @@ -12,6 +12,10 @@ ) +COMMENTS_FOR_CONCRETE_MODEL = \ + getattr(settings, 'COMMENTS_FOR_CONCRETE_MODEL', True) + + XtdComment = get_comment_model() @@ -59,7 +63,9 @@ def _reverse(*args, **kwargs): return reverse(*args, **kwargs) form = CommentSecurityForm(obj) - ctype = ContentType.objects.get_for_model(obj) + ctype = ContentType.objects.get_for_model( + obj, for_concrete_model=COMMENTS_FOR_CONCRETE_MODEL, + ) queryset = XtdComment.objects.filter(content_type=ctype, object_pk=obj._get_pk_val(), site__pk=get_current_site_id(request), diff --git a/django_comments_xtd/models.py b/django_comments_xtd/models.py index 8d92af99..cec258ac 100644 --- a/django_comments_xtd/models.py +++ b/django_comments_xtd/models.py @@ -220,6 +220,10 @@ def get_comment_dict(obj): return dic_list +XtdComment.content_object.for_concrete_model = \ + getattr(settings, 'COMMENTS_FOR_CONCRETE_MODEL', True) + + def publish_or_unpublish_nested_comments(comment, are_public=False): qs = get_model().norel_objects.filter(~Q(pk=comment.id), parent_id=comment.id)