Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-platform
Commits
877c9faf
Commit
877c9faf
authored
Nov 09, 2017
by
Troy Sankey
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
teams management commands cleanup for django 1.11
parent
b07402e7
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
67 additions
and
42 deletions
+67
-42
lms/djangoapps/teams/management/commands/reindex_course_team.py
+33
-31
lms/djangoapps/teams/management/commands/tests/test_reindex_course_team.py
+34
-11
No files found.
lms/djangoapps/teams/management/commands/reindex_course_team.py
View file @
877c9faf
""" 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
)
lms/djangoapps/teams/management/commands/tests/test_reindex_course_team.py
View file @
877c9faf
""" 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
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment