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] 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 "