Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(experimental) Allow using proxy models and change the pk with them. #345

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions django_comments_xtd/api/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
)


COMMENTS_FOR_CONCRETE_MODEL = \
getattr(settings, 'COMMENTS_FOR_CONCRETE_MODEL', True)


XtdComment = get_comment_model()


Expand Down Expand Up @@ -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)
Expand All @@ -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": {
Expand Down
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
4 changes: 4 additions & 0 deletions django_comments_xtd/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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