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

Issue 194 #209

Merged
merged 22 commits into from
Oct 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
00aee63
Fix get_xtdcomment_count. Take into account additional details.
danirus Aug 14, 2020
0541f07
Fixes render_last_xtdcomments, adds unit tests.
danirus Aug 15, 2020
eb899c6
Merge branch 'master' of github.com:danirus/django-comments-xtd into …
danirus Aug 15, 2020
d08c752
Extend test case for get_xtdcomment_count templatetag.
danirus Aug 16, 2020
f396708
Extends tests for get_last_xtdcomments templatetag.
danirus Aug 16, 2020
f160cf0
Make RenderXtdCommentTreeNode aware of COMMENTS_HIDE_REMOVED.
danirus Aug 27, 2020
588565e
PEP8 compliance changes.
danirus Aug 27, 2020
5f34faa
Merge branch 'master' of github.com:danirus/django-comments-xtd into …
danirus Sep 29, 2020
19048ae
Extend test cases for get_xtdcomment_count.
danirus Sep 30, 2020
838659f
Fix issue 221
Oct 1, 2020
9732c40
Add tests
Oct 1, 2020
f351792
Extend templatetags test cases to cover changes on issue 194.
danirus Oct 2, 2020
734b43a
Address comments
Oct 2, 2020
b061864
Merge pull request #223 from mckinly/issue-221
danirus Oct 3, 2020
b241baf
{render|get}_xtdcomment_tree must use get_model() instead of XtdComment.
danirus Oct 3, 2020
3da29c6
Update ChangeLog, authors list and docs/index.rst.
danirus Oct 3, 2020
8e2c5e1
Add missing template for tests.
danirus Oct 3, 2020
9f9988d
Merge branch 'master' of github.com:danirus/django-comments-xtd into …
danirus Oct 3, 2020
e8c4005
Adds test cases for API CommentCount and CommentList.
danirus Oct 5, 2020
337cf4f
PEP8 compliant changes.
danirus Oct 6, 2020
4f63e4d
Extend test_api_views with cases for COMMENTS_HIDE_REMOVED.
danirus Oct 7, 2020
fa5d44a
Update settings.rst on COMMENTS_HIDE_REMOVED and the new setting.
danirus Oct 10, 2020
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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Yngve Høiseth, @yhoiseth
Ashwani Gupta, @ashwani99
Jean-Paul Ladage, @jladage
Михаил Рыбкин, @MikerStudio
McKinly Springer, @mckinly

Main developer
Daniel Rus Morales, @danirus
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Change Log

## [3.0.0] -

* Fixes issue #194. Take into account setting COMMENTS_HIDE_REMOVED from
parent's app django-comments. This is a backwards incompatible change.
When COMMENTS_HIDE_REMOVED is not defined or is defined as True, removed
comments should not be retrieved nor counted. <document this>

## [2.8.1] -

* Fixes issue #221, about the get_version function. Now it returns the full
version number <major>.<minor>.<patch>. Thanks to @mckinly.

## [2.8.0] - 2020-09-26

* Fixes issue #106, which is about computing the number of nested comments
Expand Down
4 changes: 1 addition & 3 deletions django_comments_xtd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ def get_form():


def get_version():
version = '%s.%s' % (VERSION[0], VERSION[1])
if VERSION[2]:
version = '%s.%s' % (version, VERSION[2])
version = '%s.%s.%s' % (VERSION[0], VERSION[1], VERSION[2])
if VERSION[3] != 'f':
version = '%s%s%s' % (version, VERSION[3], VERSION[4])
return version
37 changes: 26 additions & 11 deletions django_comments_xtd/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.utils.module_loading import import_string

from django_comments.models import CommentFlag
from django_comments_xtd import get_model as get_comment_model
from django_comments.views.moderation import perform_flag
from rest_framework import generics, mixins, permissions, status
from rest_framework.decorators import api_view
Expand Down Expand Up @@ -60,15 +61,21 @@ def get_queryset(self, **kwargs):
CommentFlag.SUGGEST_REMOVAL, LIKEDIT_FLAG, DISLIKEDIT_FLAG
]).prefetch_related('user')
prefetch = Prefetch('flags', queryset=flags_qs)
qs = XtdComment\
site_id = getattr(settings, "SITE_ID", None)
if not site_id:
site_id = get_current_site_id(self.request)
fkwds = {
"content_type": content_type,
"object_pk": object_pk_arg,
"site__pk": site_id,
"is_public": True
}
if getattr(settings, 'COMMENTS_HIDE_REMOVED', True):
fkwds['is_removed'] = False
qs = get_comment_model()\
.objects\
.prefetch_related(prefetch)\
.filter(
content_type=content_type,
object_pk=object_pk_arg,
site__pk=get_current_site_id(self.request),
is_public=True
)
.filter(**fkwds)
return qs


Expand All @@ -81,10 +88,18 @@ def get_queryset(self):
object_pk_arg = self.kwargs.get('object_pk', None)
app_label, model = content_type_arg.split("-")
content_type = ContentType.objects.get_by_natural_key(app_label, model)
qs = XtdComment.objects.filter(content_type=content_type,
object_pk=object_pk_arg,
is_public=True)
return qs
site_id = getattr(settings, "SITE_ID", None)
if not site_id:
site_id = get_current_site_id(self.request)
fkwds = {
"content_type": content_type,
"object_pk": object_pk_arg,
"site__pk": site_id,
"is_public": True
}
if getattr(settings, 'COMMENTS_HIDE_REMOVED', True):
fkwds["is_removed"] = False
return get_comment_model().objects.filter(**fkwds)

def get(self, request, *args, **kwargs):
return Response({'count': self.get_queryset().count()})
Expand Down
13 changes: 9 additions & 4 deletions django_comments_xtd/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ class CommentsXtdConfig(AppConfig):

def ready(self):
from django_comments_xtd import get_model
from django_comments_xtd.models import publish_or_unpublish_on_pre_save
from django_comments_xtd.conf import settings
from django_comments_xtd.models import publish_or_withhold_on_pre_save

model_app_label = get_model()._meta.label
pre_save.connect(publish_or_unpublish_on_pre_save,
sender=model_app_label)
if (
getattr(settings, 'COMMENTS_HIDE_REMOVED', True) or
getattr(settings, 'COMMENTS_XTD_PUBLISH_OR_WITHHOLD_NESTED', True)
):
model_app_label = get_model()._meta.label
pre_save.connect(publish_or_withhold_on_pre_save,
sender=model_app_label)
4 changes: 4 additions & 0 deletions django_comments_xtd/conf/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
}
}

# When True, an approval or removal operation on a comment will
# trigger a publishing or withholding operation on its nested comments.
COMMENTS_XTD_PUBLISH_OR_WITHHOLD_NESTED = True


# Define a function to return the user representation. Used by
# the web API to represent user strings in response objects.
Expand Down
14 changes: 7 additions & 7 deletions django_comments_xtd/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,20 +209,20 @@ def get_comment_dict(obj):
return dic_list


def publish_or_unpublish_nested_comments(comment, are_public=False):
def publish_or_withhold_nested_comments(comment, shall_be_public=False):
qs = get_model().objects.filter(~Q(pk=comment.id), parent_id=comment.id)
nested = [cm.id for cm in qs]
qs.update(is_public=are_public)
qs.update(is_public=shall_be_public)
while len(nested):
cm_id = nested.pop()
qs = XtdComment.objects.filter(~Q(pk=cm_id), parent_id=cm_id)
nested.extend([cm.id for cm in qs])
qs.update(is_public=are_public)
qs.update(is_public=shall_be_public)
# Update nested_count in parents comments in the same thread.
# The comment.nested_count doesn't change because the comment's is_public
# attribute is not changing, only its nested comments change, and it will
# help to re-populate nested_count should it be published again.
if are_public:
if shall_be_public:
op = F('nested_count') + comment.nested_count
else:
op = F('nested_count') - comment.nested_count
Expand All @@ -232,10 +232,10 @@ def publish_or_unpublish_nested_comments(comment, are_public=False):
.update(nested_count=op)


def publish_or_unpublish_on_pre_save(sender, instance, raw, using, **kwargs):
def publish_or_withhold_on_pre_save(sender, instance, raw, using, **kwargs):
if not raw and instance and instance.id:
are_public = (not instance.is_removed) and instance.is_public
publish_or_unpublish_nested_comments(instance, are_public=are_public)
shall_be_public = (not instance.is_removed) and instance.is_public
publish_or_withhold_nested_comments(instance, shall_be_public)


# ----------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class CommentBox extends React.Component {
handle_comment_created() {
this.load_comments();
}

handle_preview(name, email, url, comment) {
this.setState({
preview: {name: name, email: email, url: url, comment: comment},
Expand Down Expand Up @@ -122,7 +122,7 @@ export class CommentBox extends React.Component {
return comments[index];
});
};

for (let item of data) {
incids.push(item.id);
comments[item.id] = item;
Expand Down Expand Up @@ -189,13 +189,13 @@ export class CommentBox extends React.Component {
}.bind(this)
});
}

componentDidMount() {
this.load_comments();
if(this.props.polling_interval)
setInterval(this.load_count.bind(this), this.props.polling_interval);
}

render() {
var settings = this.props;
var comment_counter = this.render_comment_counter();
Expand All @@ -211,7 +211,7 @@ export class CommentBox extends React.Component {
on_comment_created={this.handle_comment_created} />
);
}.bind(this));

return (
<div>
{comment_counter}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ <h6 class="mb-1 small d-flex">
{% endif %}
{% endif %}
</div>
{% if not item.comment.is_removed and item.children %}
{% if item.children and not hide_removed or item.children and hide_removed and not item.is_removed %}
{% render_xtdcomment_tree with comments=item.children %}
{% endif %}
</div>
Expand Down
83 changes: 57 additions & 26 deletions django_comments_xtd/templatetags/comments_xtd.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from django.urls import reverse
from django.utils.safestring import mark_safe

from django_comments_xtd.conf import settings
from django_comments.models import CommentFlag
from django_comments_xtd import get_model as get_comment_model
from django_comments_xtd.api import frontend
Expand All @@ -36,12 +37,16 @@ class XtdCommentCountNode(Node):
def __init__(self, as_varname, content_types):
"""Class method to parse get_xtdcomment_list and return a Node."""
self.as_varname = as_varname
self.qs = XtdComment.objects.for_content_types(content_types)
self.qs = get_comment_model().objects.for_content_types(content_types)
self.qs = self.qs.filter(is_public=True)
if getattr(settings, 'COMMENTS_HIDE_REMOVED', True):
self.qs = self.qs.filter(is_removed=False)

def render(self, context):
context[self.as_varname] = self.qs.filter(
site=get_current_site_id(context.get('request'))
).count()
site_id = getattr(settings, "SITE_ID", None)
if not site_id and ('request' in context):
site_id = get_current_site_id(context['request'])
context[self.as_varname] = self.qs.filter(site=site_id).count()
return ''


Expand Down Expand Up @@ -138,19 +143,26 @@ def __init__(self, count, content_types, template_path=None):


class RenderLastXtdCommentsNode(BaseLastXtdCommentsNode):

def render(self, context):
if not isinstance(self.count, int):
self.count = int(self.count.resolve(context))

self.qs = XtdComment.objects.for_content_types(
self.content_types,
site=get_current_site_id(context.get('request'))
).order_by('submit_date')[:self.count]
site_id = getattr(settings, "SITE_ID", None)
if not site_id and ('request' in context):
site_id = get_current_site_id(context['request'])

qs = get_comment_model().objects.for_content_types(
self.content_types, site=site_id)

qs = qs.filter(is_public=True)
if getattr(settings, 'COMMENTS_HIDE_REMOVED', True):
qs = qs.filter(is_removed=False)

qs = qs.order_by('submit_date')[:self.count]

strlist = []
context_dict = context.flatten()
for xtd_comment in self.qs:
for xtd_comment in qs:
if self.template_path:
template_arg = self.template_path
else:
Expand All @@ -168,20 +180,28 @@ def render(self, context):


class GetLastXtdCommentsNode(BaseLastXtdCommentsNode):

def __init__(self, count, as_varname, content_types):
super(GetLastXtdCommentsNode, self).__init__(count, content_types)
self.as_varname = as_varname

def render(self, context):
if not isinstance(self.count, int):
self.count = int(self.count.resolve(context))
self.qs = XtdComment.objects.for_content_types(
self.content_types,
site=get_current_site_id(context.get('request'))
)\
.order_by('submit_date')[:self.count]
context[self.as_varname] = self.qs

site_id = getattr(settings, "SITE_ID", None)
if not site_id and ('request' in context):
site_id = get_current_site_id(context['request'])

qs = get_comment_model().objects.for_content_types(
self.content_types, site=site_id)

qs = qs.filter(is_public=True)
if getattr(settings, 'COMMENTS_HIDE_REMOVED', True):
qs = qs.filter(is_removed=False)

qs = qs.order_by('submit_date')[:self.count]

context[self.as_varname] = qs
return ''


Expand Down Expand Up @@ -301,29 +321,40 @@ def render(self, context):
for attr in ['allow_flagging', 'allow_feedback', 'show_feedback']:
context_dict[attr] = (getattr(self, attr, False) or
context.get(attr, False))

hide_removed = getattr(settings, 'COMMENTS_HIDE_REMOVED', True)
context_dict['hide_removed'] = hide_removed

if self.obj:
obj = self.obj.resolve(context)
ctype = ContentType.objects.get_for_model(obj)
flags_qs = CommentFlag.objects.filter(flag__in=[
CommentFlag.SUGGEST_REMOVAL, LIKEDIT_FLAG, DISLIKEDIT_FLAG
]).prefetch_related('user')
prefetch = Prefetch('flags', queryset=flags_qs)
queryset = XtdComment\
site_id = getattr(settings, "SITE_ID", None)
if not site_id and ('request' in context):
site_id = get_current_site_id(context['request'])
fkwds = {
"content_type": ctype,
"object_pk": obj.pk,
"site__pk": site_id,
"is_public": True
}
if getattr(settings, 'COMMENTS_HIDE_REMOVED', True):
fkwds['is_removed'] = False
qs = get_comment_model()\
.objects\
.prefetch_related(prefetch)\
.filter(
content_type=ctype,
object_pk=obj.pk,
site__pk=get_current_site_id(context.get('request')),
is_public=True
)
.filter(**fkwds)
comments = XtdComment.tree_from_queryset(
queryset,
qs,
with_flagging=self.allow_flagging,
with_feedback=self.allow_feedback,
user=context['user']
)
context_dict['comments'] = comments

if self.cvars:
for vname, vobj in self.cvars:
context_dict[vname] = vobj.resolve(context)
Expand Down Expand Up @@ -363,7 +394,7 @@ def render(self, context):
CommentFlag.SUGGEST_REMOVAL, LIKEDIT_FLAG, DISLIKEDIT_FLAG
]).prefetch_related('user')
prefetch = Prefetch('flags', queryset=flags_qs)
queryset = XtdComment\
queryset = get_comment_model()\
.objects\
.prefetch_related(prefetch)\
.filter(
Expand Down
4 changes: 4 additions & 0 deletions django_comments_xtd/tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@

DEFAULT_FROM_EMAIL = "Alice Bloggs <[email protected]>"

COMMENTS_HIDE_REMOVED = True

COMMENTS_XTD_PUBLISH_OR_WITHHOLD_NESTED = True

COMMENTS_XTD_CONFIRM_EMAIL = True
COMMENTS_XTD_SALT = b"es-war-einmal-una-bella-princesa-in-a-beautiful-castle"
COMMENTS_XTD_MAX_THREAD_LEVEL = 2
Expand Down
Loading