Skip to content

Commit

Permalink
simplify getting content_type for comment
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrDlouhy committed Oct 1, 2021
1 parent 96c79f5 commit bf9552f
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 7 deletions.
3 changes: 1 addition & 2 deletions django_comments_xtd/api/serializers.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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(
Expand Down
3 changes: 1 addition & 2 deletions django_comments_xtd/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions django_comments_xtd/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand Down Expand Up @@ -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 "
Expand Down Expand Up @@ -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 "
Expand Down

0 comments on commit bf9552f

Please sign in to comment.