-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #217 from danirus/issue-106
Issue 106
- Loading branch information
Showing
11 changed files
with
522 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
django_comments_xtd/management/commands/initialize_nested_count.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from django.db.utils import ConnectionDoesNotExist | ||
from django.core.management.base import BaseCommand | ||
|
||
from django_comments_xtd.models import XtdComment | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Initialize the nested_count field for all the comments in the DB." | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument('using', nargs='*', type=str) | ||
|
||
def initialize_nested_count(self, using): | ||
# Control break. | ||
active_thread_id = -1 | ||
parents = {} | ||
|
||
qs = XtdComment.objects.using(using).order_by('thread_id', '-order') | ||
|
||
for comment in qs: | ||
# Clean up parents when there is a control break. | ||
if comment.thread_id != active_thread_id: | ||
parents = {} | ||
active_thread_id = comment.thread_id | ||
|
||
nested_count = parents.get(comment.comment_ptr_id, 0) | ||
parents.setdefault(comment.parent_id, 0) | ||
if nested_count > 0: | ||
parents[comment.parent_id] += 1 + nested_count | ||
else: | ||
parents[comment.parent_id] += 1 | ||
comment.nested_count = nested_count | ||
comment.save() | ||
|
||
return qs.count() | ||
|
||
def handle(self, *args, **options): | ||
total = 0 | ||
using = options['using'] or ['default'] | ||
|
||
for db_conn in using: | ||
try: | ||
total += self.initialize_nested_count(db_conn) | ||
except ConnectionDoesNotExist: | ||
self.stdout.write("DB connection '%s' does not exist." % | ||
db_conn) | ||
continue | ||
self.stdout.write("Updated %d XtdComment object(s)." % total) |
18 changes: 18 additions & 0 deletions
18
django_comments_xtd/migrations/0007_xtdcomment_nested_count.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 3.0.1 on 2020-09-12 20:08 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('django_comments_xtd', '0006_auto_20181204_0948'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='xtdcomment', | ||
name='nested_count', | ||
field=models.IntegerField(db_index=True, default=0), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Generated by Django 3.0.1 on 2020-09-20 18:37 | ||
from django.core.management import call_command | ||
from django.db import migrations | ||
|
||
|
||
def populate_nested_count(*args): | ||
call_command('initialize_nested_count') | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('django_comments_xtd', '0007_xtdcomment_nested_count'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(populate_nested_count, | ||
reverse_code=migrations.RunPython.noop) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
django_comments_xtd/tests/test_cmd_initialize_nested_count.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from io import StringIO | ||
from django.core.management import call_command | ||
from django.test import TestCase | ||
|
||
from django_comments_xtd.models import XtdComment | ||
from django_comments_xtd.tests.models import Article | ||
from django_comments_xtd.tests.test_models import ( | ||
thread_test_step_1, thread_test_step_2, thread_test_step_3, | ||
thread_test_step_4, thread_test_step_5 | ||
) | ||
|
||
|
||
class InitializeNesteCoundCmdTest(TestCase): | ||
def setUp(self): | ||
self.article_1 = Article.objects.create( | ||
title="September", slug="september", body="During September...") | ||
thread_test_step_1(self.article_1) | ||
thread_test_step_2(self.article_1) | ||
thread_test_step_3(self.article_1) | ||
thread_test_step_4(self.article_1) | ||
thread_test_step_5(self.article_1) | ||
self.check_nested_count() | ||
|
||
def check_nested_count(self): | ||
( # content -> cmt.id thread_id parent_id level order nested | ||
self.c1, # -> 1 1 1 0 1 4 | ||
self.c3, # -> 3 1 1 1 2 1 | ||
self.c8, # -> 8 1 3 2 3 0 | ||
self.c4, # -> 4 1 1 1 4 1 | ||
self.c7, # -> 7 1 4 2 5 0 | ||
self.c2, # -> 2 2 2 0 1 2 | ||
self.c5, # -> 5 2 2 1 2 1 | ||
self.c6, # -> 6 2 5 2 3 0 | ||
self.c9 # -> 9 9 9 0 1 0 | ||
) = XtdComment.objects.all() | ||
self.assertEqual(self.c1.nested_count, 4) | ||
self.assertEqual(self.c3.nested_count, 1) | ||
self.assertEqual(self.c8.nested_count, 0) | ||
self.assertEqual(self.c4.nested_count, 1) | ||
self.assertEqual(self.c7.nested_count, 0) | ||
self.assertEqual(self.c2.nested_count, 2) | ||
self.assertEqual(self.c5.nested_count, 1) | ||
self.assertEqual(self.c6.nested_count, 0) | ||
self.assertEqual(self.c9.nested_count, 0) | ||
|
||
def test_calling_command_computes_nested_count(self): | ||
# Set all comments nested_count field to 0. | ||
XtdComment.objects.update(nested_count=0) | ||
out = StringIO() | ||
call_command('initialize_nested_count', stdout=out) | ||
self.assertIn("Updated 9 XtdComment object(s).", out.getvalue()) | ||
self.check_nested_count() | ||
|
||
def test_command_is_idempotent(self): | ||
out = StringIO() | ||
call_command('initialize_nested_count', stdout=out) | ||
call_command('initialize_nested_count', stdout=out) | ||
self.assertIn("Updated 9 XtdComment object(s).", out.getvalue()) | ||
self.check_nested_count() |
Oops, something went wrong.