-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mark channel and percolatequery is deleted and update memeberships (#…
- Loading branch information
Tasawer Nawaz
authored
Apr 24, 2019
1 parent
7a1ff74
commit 69698a4
Showing
14 changed files
with
194 additions
and
10 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
44 changes: 44 additions & 0 deletions
44
discussions/management/commands/mark_channel_deleted_and_update_memberships.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,44 @@ | ||
""" | ||
Mark Channel and PercolateQuery deleted and updates all the memberships to is_member=False, need_update=True | ||
""" | ||
from django.core.management import BaseCommand, CommandError | ||
|
||
from discussions.models import Channel | ||
from discussions.tasks import remove_moderators_from_channel | ||
|
||
|
||
class Command(BaseCommand): | ||
""" | ||
Mark Channel and PercolateQuery deleted and updates all the memberships to is_member=False, need_update=True | ||
""" | ||
help = 'Mark Channel and PercolateQuery deleted and updates all the memberships to ' \ | ||
'is_member=False, need_update=True.' | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument('channel_name', type=str) | ||
|
||
def handle(self, *args, **kwargs): # pylint: disable=unused-argument | ||
channel_name = kwargs.get('channel_name') | ||
|
||
try: | ||
channel = Channel.objects.get(name=channel_name) | ||
except Channel.DoesNotExist: | ||
raise CommandError('Channel does not exists with name={channel_name}'.format(channel_name=channel_name)) | ||
|
||
channel.is_deleted = True | ||
channel.save() | ||
|
||
percolate_query = channel.query | ||
percolate_query.is_deleted = True | ||
percolate_query.save() | ||
|
||
percolate_query.percolate_memberships.update(is_member=False, needs_update=True) | ||
|
||
self.stdout.write( | ||
self.style.SUCCESS('Channel and PercolateQuery marked as deleted and related memberships are update.') | ||
) | ||
|
||
remove_moderators_from_channel.delay(channel_name) | ||
self.stdout.write( | ||
self.style.SUCCESS('Async job to remove moderators is submitted') | ||
) |
48 changes: 48 additions & 0 deletions
48
discussions/management/commands/mark_channel_deleted_and_update_memberships_test.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 @@ | ||
"""Tests for management command `mark_channel_deleted_and_update_memberships`""" | ||
import pytest | ||
from django.core.management import call_command, CommandError | ||
|
||
from discussions.factories import ChannelFactory | ||
from search.factories import PercolateQueryMembershipFactory | ||
|
||
pytestmark = [ | ||
pytest.mark.django_db, | ||
] | ||
|
||
|
||
def test_without_argument(): | ||
"""Tests that commands raises commands error if required argument not provided. """ | ||
with pytest.raises(CommandError) as ex: | ||
call_command('mark_channel_deleted_and_update_memberships') | ||
|
||
assert str(ex.value) == 'Error: the following arguments are required: channel_name' | ||
|
||
|
||
def test_channel_name_does_not_exist(): | ||
"""Test that command raises an error if channel name does not exists.""" | ||
fake_name = 'fake_name' | ||
with pytest.raises(CommandError) as ex: | ||
call_command('mark_channel_deleted_and_update_memberships', 'fake_name') | ||
|
||
assert str(ex.value) == 'Channel does not exists with name={}'.format(fake_name) | ||
|
||
|
||
def test_channel_marked_deleted(): | ||
""" Test that command will mark channel as deleted.""" | ||
|
||
channel = ChannelFactory() | ||
membership = PercolateQueryMembershipFactory.create(query=channel.query, is_member=True, needs_update=False) | ||
|
||
assert not channel.is_deleted | ||
assert not channel.query.is_deleted | ||
|
||
call_command('mark_channel_deleted_and_update_memberships', channel.name) | ||
|
||
channel.refresh_from_db() | ||
assert channel.is_deleted | ||
assert channel.query.is_deleted | ||
|
||
# is_member and needs_update should be updated | ||
membership.refresh_from_db() | ||
assert not membership.is_member | ||
assert membership.needs_update |
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 2.1.5 on 2019-03-22 07:29 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('discussions', '0005_timestamped_discussions_models'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='channel', | ||
name='is_deleted', | ||
field=models.BooleanField(default=False), | ||
), | ||
] |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 2.1.5 on 2019-03-22 07:29 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('search', '0004_add_percolate_query_membership'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='percolatequery', | ||
name='is_deleted', | ||
field=models.BooleanField(default=False), | ||
), | ||
] |
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