From 877c9fafc36a537eaed8bc34873552d1effb6957 Mon Sep 17 00:00:00 2001
From: Troy Sankey <sankeytms@gmail.com>
Date: Thu, 9 Nov 2017 14:16:46 -0500
Subject: [PATCH] teams management commands cleanup for django 1.11

---
 lms/djangoapps/teams/management/commands/reindex_course_team.py            | 64 +++++++++++++++++++++++++++++++++-------------------------------
 lms/djangoapps/teams/management/commands/tests/test_reindex_course_team.py | 45 ++++++++++++++++++++++++++++++++++-----------
 2 files changed, 67 insertions(+), 42 deletions(-)

diff --git a/lms/djangoapps/teams/management/commands/reindex_course_team.py b/lms/djangoapps/teams/management/commands/reindex_course_team.py
index cb5647e..76183b6 100644
--- a/lms/djangoapps/teams/management/commands/reindex_course_team.py
+++ b/lms/djangoapps/teams/management/commands/reindex_course_team.py
@@ -1,45 +1,42 @@
-""" Management command to update course_teams' search index. """
-from optparse import make_option
+"""
+Management command to update course_teams' search index.
+"""
+from __future__ import print_function, unicode_literals
+
 from textwrap import dedent
 
 from django.conf import settings
 from django.core.exceptions import ObjectDoesNotExist
 from django.core.management import BaseCommand, CommandError
-
 from lms.djangoapps.teams.models import CourseTeam
 
 
 class Command(BaseCommand):
     """
-    Command to reindex course_teams (single, multiple or all available).
-
-    Examples:
-
-        ./manage.py reindex_course_team team1 team2 - reindexes course teams with team_ids team1 and team2
-        ./manage.py reindex_course_team --all - reindexes all available course teams
+    Reindex course_teams (single, multiple or all available).
     """
     help = dedent(__doc__)
 
-    can_import_settings = True
-
-    args = "<course_team_id course_team_id ...>"
-
-    option_list = BaseCommand.option_list + (
-        make_option(
-            '--all',
-            action='store_true',
-            dest='all',
-            default=False,
-            help='Reindex all course teams'
-        ),
-    )
+    def add_arguments(self, parser):
+        # Mutually exclusive groups do not work here because nargs=* arguments
+        # are "required", but required args are not allowed to be part of a
+        # mutually exclusive group.
+        parser.add_argument('--all',
+                            action='store_true',
+                            help='reindex all course teams (do not specify any course teams)')
+        parser.add_argument('course_team_ids',
+                            nargs='*',
+                            metavar='course_team_id',
+                            help='a specific course team to reindex')
 
     def _get_course_team(self, team_id):
-        """ Returns course_team object from team_id. """
+        """
+        Returns course_team object from team_id.
+        """
         try:
             result = CourseTeam.objects.get(team_id=team_id)
         except ObjectDoesNotExist:
-            raise CommandError(u"Argument {0} is not a course_team team_id".format(team_id))
+            raise CommandError('Argument {} is not a course_team team_id'.format(team_id))
 
         return result
 
@@ -52,16 +49,21 @@ class Command(BaseCommand):
         # happen anywhere else that I can't figure out how to avoid it :(
         from ...search_indexes import CourseTeamIndexer
 
-        if len(args) == 0 and not options.get('all', False):
-            raise CommandError(u"reindex_course_team requires one or more arguments: <course_team_id>")
-        elif not settings.FEATURES.get('ENABLE_TEAMS', False):
-            raise CommandError(u"ENABLE_TEAMS must be enabled to use course team indexing")
+        if options['all']:
+            if len(options['course_team_ids']) > 0:
+                raise CommandError('Course teams cannot be specified when --all is also specified')
+        else:
+            if len(options['course_team_ids']) == 0:
+                raise CommandError('At least one course_team_id or --all needs to be specified')
+
+        if not settings.FEATURES.get('ENABLE_TEAMS', False):
+            raise CommandError('ENABLE_TEAMS must be enabled to use course team indexing')
 
-        if options.get('all', False):
+        if options['all']:
             course_teams = CourseTeam.objects.all()
         else:
-            course_teams = map(self._get_course_team, args)
+            course_teams = map(self._get_course_team, options['course_team_ids'])
 
         for course_team in course_teams:
-            print "Indexing {id}".format(id=course_team.team_id)
+            print('Indexing {}'.format(course_team.team_id))
             CourseTeamIndexer.index(course_team)
diff --git a/lms/djangoapps/teams/management/commands/tests/test_reindex_course_team.py b/lms/djangoapps/teams/management/commands/tests/test_reindex_course_team.py
index f04a731..5bf099a 100644
--- a/lms/djangoapps/teams/management/commands/tests/test_reindex_course_team.py
+++ b/lms/djangoapps/teams/management/commands/tests/test_reindex_course_team.py
@@ -1,4 +1,6 @@
-""" Tests for course_team reindex command """
+"""
+Tests for course_team reindex command
+"""
 
 import ddt
 from django.core.management import CommandError, call_command
@@ -16,7 +18,9 @@ COURSE_KEY1 = CourseKey.from_string('edx/history/1')
 
 @ddt.ddt
 class ReindexCourseTeamTest(SharedModuleStoreTestCase):
-    """Tests for the ReindexCourseTeam command"""
+    """
+    Tests for the ReindexCourseTeam command
+    """
 
     def setUp(self):
         """
@@ -31,33 +35,50 @@ class ReindexCourseTeamTest(SharedModuleStoreTestCase):
         self.search_engine = SearchEngine.get_search_engine(index='index_course_team')
 
     def test_given_no_arguments_raises_command_error(self):
-        """ Test that raises CommandError for incorrect arguments. """
-        with self.assertRaisesRegexp(CommandError, ".* requires one or more arguments.*"):
+        """
+        Test that raises CommandError for incorrect arguments.
+        """
+        with self.assertRaisesRegexp(CommandError, '.*At least one course_team_id or --all needs to be specified.*'):
             call_command('reindex_course_team')
 
+    def test_given_conflicting_arguments_raises_command_error(self):
+        """
+        Test that raises CommandError for incorrect arguments.
+        """
+        with self.assertRaisesRegexp(CommandError, '.*Course teams cannot be specified when --all is also specified.*'):
+            call_command('reindex_course_team', self.team1.team_id, all=True)
+
     @patch.dict('django.conf.settings.FEATURES', {'ENABLE_TEAMS': False})
     def test_teams_search_flag_disabled_raises_command_error(self):
-        """ Test that raises CommandError for disabled feature flag. """
-        with self.assertRaisesRegexp(CommandError, ".*ENABLE_TEAMS must be enabled.*"):
+        """
+        Test that raises CommandError for disabled feature flag.
+        """
+        with self.assertRaisesRegexp(CommandError, '.*ENABLE_TEAMS must be enabled.*'):
             call_command('reindex_course_team', self.team1.team_id)
 
     def test_given_invalid_team_id_raises_command_error(self):
-        """ Test that raises CommandError for invalid team id. """
+        """
+        Test that raises CommandError for invalid team id.
+        """
         team_id = u'team4'
-        error_str = 'Argument {0} is not a course_team team_id'.format(team_id)
+        error_str = 'Argument {} is not a course_team team_id'.format(team_id)
         with self.assertRaisesRegexp(CommandError, error_str):
             call_command('reindex_course_team', team_id)
 
     @patch.object(CourseTeamIndexer, 'index')
     def test_single_team_id(self, mock_index):
-        """ Test that command indexes a single passed team. """
+        """
+        Test that command indexes a single passed team.
+        """
         call_command('reindex_course_team', self.team1.team_id)
         mock_index.assert_called_once_with(self.team1)
         mock_index.reset_mock()
 
     @patch.object(CourseTeamIndexer, 'index')
     def test_multiple_team_id(self, mock_index):
-        """ Test that command indexes multiple passed teams. """
+        """
+        Test that command indexes multiple passed teams.
+        """
         call_command('reindex_course_team', self.team1.team_id, self.team2.team_id)
         mock_index.assert_any_call(self.team1)
         mock_index.assert_any_call(self.team2)
@@ -65,7 +86,9 @@ class ReindexCourseTeamTest(SharedModuleStoreTestCase):
 
     @patch.object(CourseTeamIndexer, 'index')
     def test_all_teams(self, mock_index):
-        """ Test that command indexes all teams. """
+        """
+        Test that command indexes all teams.
+        """
         call_command('reindex_course_team', all=True)
         mock_index.assert_any_call(self.team1)
         mock_index.assert_any_call(self.team2)
--
libgit2 0.26.0