diff --git a/django_comments_xtd/api/frontend.py b/django_comments_xtd/api/frontend.py index 10dffd0a..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,9 +63,11 @@ 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.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 +92,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": { 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/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) 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 "