Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
course-discovery
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
course-discovery
Commits
0a75314a
Commit
0a75314a
authored
Nov 16, 2017
by
Simon Chen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add management command to republish course images from publisher to discovery courses
EDUCATOR-1761
parent
d7be3278
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
139 additions
and
0 deletions
+139
-0
course_discovery/apps/publisher/management/commands/republish_course_images.py
+46
-0
course_discovery/apps/publisher/management/commands/tests/test_republish_course_images.py
+93
-0
No files found.
course_discovery/apps/publisher/management/commands/republish_course_images.py
0 → 100644
View file @
0a75314a
import
logging
from
django.core.management
import
BaseCommand
from
course_discovery.apps.course_metadata.models
import
Course
as
DiscoveryCourse
from
course_discovery.apps.publisher.models
import
Course
as
PublisherCourse
logger
=
logging
.
getLogger
(
__name__
)
class
Command
(
BaseCommand
):
help
=
'Re-publish the course images we have on publisher to discovery course objects'
def
add_arguments
(
self
,
parser
):
parser
.
add_argument
(
'--start_id'
,
action
=
'store'
,
dest
=
'start_id'
,
required
=
True
,
help
=
'The Publisher course Primary key value starting id to re-publish the course image.'
)
parser
.
add_argument
(
'--end_id'
,
action
=
'store'
,
dest
=
'end_id'
,
required
=
True
,
help
=
'To this id of the Publisher Course the course image will be re-published.'
)
def
handle
(
self
,
*
args
,
**
options
):
start_id
=
options
.
get
(
'start_id'
)
end_id
=
options
.
get
(
'end_id'
)
publisher_courses
=
PublisherCourse
.
objects
.
filter
(
id__range
=
(
start_id
,
end_id
),
image__isnull
=
False
)
for
publisher_course
in
publisher_courses
:
discovery_course
=
None
try
:
discovery_course
=
publisher_course
.
discovery_counterpart
except
DiscoveryCourse
.
DoesNotExist
:
logger
.
warning
(
'Publisher course {} has no discovery counterpart!'
.
format
(
publisher_course
.
number
))
if
discovery_course
:
logger
.
info
(
'Re-render course image for course [{}]'
.
format
(
publisher_course
.
key
))
discovery_course
.
image
.
save
(
publisher_course
.
image
.
name
,
publisher_course
.
image
.
file
)
course_discovery/apps/publisher/management/commands/tests/test_republish_course_images.py
0 → 100644
View file @
0a75314a
import
ddt
from
django.core.management
import
CommandError
,
call_command
from
django.test
import
TestCase
from
testfixtures
import
LogCapture
from
course_discovery.apps.core.tests.helpers
import
make_image_file
from
course_discovery.apps.course_metadata.tests.factories
import
CourseFactory
as
DiscoveryCourseFactory
from
course_discovery.apps.publisher.management.commands.republish_course_images
import
logger
as
command_logger
from
course_discovery.apps.publisher.tests
import
factories
@ddt.ddt
class
RepublishCourseImageCommandTests
(
TestCase
):
def
setUp
(
self
):
super
(
RepublishCourseImageCommandTests
,
self
)
.
setUp
()
self
.
command_name
=
'republish_course_images'
@ddt.data
(
[
''
],
# Raises error because both args are missing
[
'--start_id=1'
],
# Raises error because 'start_id' is not provided
[
'--end_id=2'
],
# Raises error because 'end_id' is not provided
)
def
test_missing_required_arguments
(
self
,
command_args
):
""" Verify CommandError is raised when required arguments are missing """
# If a required argument is not specified the system should raise a CommandError
with
self
.
assertRaises
(
CommandError
):
call_command
(
self
.
command_name
,
*
command_args
)
@ddt.data
(
[
'--start_id=1'
,
'--end_id=x'
],
# Raises error because 'end_id has invalid value
[
'--start_id=x'
,
'--end_id=1'
]
# Raises error because both args has invalid value
)
def
test_with_invalid_arguments
(
self
,
command_args
):
""" Verify CommandError is raised when required arguments has invalid values """
with
self
.
assertRaises
(
ValueError
):
call_command
(
self
.
command_name
,
*
command_args
)
class
RepublishCourseImagesTest
(
TestCase
):
def
setUp
(
self
):
super
(
RepublishCourseImagesTest
,
self
)
.
setUp
()
self
.
course_image
=
make_image_file
(
'testimage.jpg'
)
self
.
publisher_course
=
factories
.
CourseFactory
(
image__from_file
=
self
.
course_image
)
org_extension
=
factories
.
OrganizationExtensionFactory
()
organization
=
org_extension
.
organization
self
.
publisher_course
.
organizations
.
add
(
organization
)
# pylint: disable=no-member
self
.
command_name
=
'republish_course_images'
self
.
command_args
=
[
'--start_id={}'
.
format
(
self
.
publisher_course
.
id
),
'--end_id={}'
.
format
(
self
.
publisher_course
.
id
)
]
def
_assert_images_exist
(
self
,
discovery_course
):
assert
discovery_course
.
image
.
url
is
not
None
assert
discovery_course
.
image
.
small
.
url
is
not
None
def
test_command_success
(
self
):
discovery_course
=
DiscoveryCourseFactory
(
partner
=
self
.
publisher_course
.
partner
,
key
=
self
.
publisher_course
.
key
)
discovery_course
.
image
.
delete
()
discovery_course
.
save
()
call_command
(
self
.
command_name
,
*
self
.
command_args
)
self
.
_assert_images_exist
(
self
.
publisher_course
.
discovery_counterpart
)
def
test_command_override_existing_image
(
self
):
DiscoveryCourseFactory
(
partner
=
self
.
publisher_course
.
partner
,
key
=
self
.
publisher_course
.
key
)
discovery_course
=
self
.
publisher_course
.
discovery_counterpart
assert
discovery_course
.
image
.
url
is
not
None
call_command
(
self
.
command_name
,
*
self
.
command_args
)
self
.
_assert_images_exist
(
discovery_course
)
def
test_command_invalid_discovery_counterpart
(
self
):
""" Verify that if the publisher course has no discovery counterpart
then we log the exception and move onto the next course
"""
with
LogCapture
(
command_logger
.
name
)
as
log_capture
:
call_command
(
self
.
command_name
,
*
self
.
command_args
)
log_capture
.
check
(
(
command_logger
.
name
,
'WARNING'
,
'Publisher course {} has no discovery counterpart!'
.
format
(
self
.
publisher_course
.
number
)
)
)
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