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
d04db412
Commit
d04db412
authored
Jun 21, 2017
by
Awais
Committed by
Awais Qureshi
Jul 04, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add script to update the course-runs override fields in publisher app
EDUCATOR-716
parent
4728d89a
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
192 additions
and
2 deletions
+192
-2
course_discovery/apps/publisher/dataloader/update_course_runs.py
+39
-0
course_discovery/apps/publisher/management/commands/tests/test_import_courses.py
+113
-2
course_discovery/apps/publisher/management/commands/update_publisher_course_runs.py
+38
-0
course_discovery/apps/publisher/tests/factories.py
+2
-0
No files found.
course_discovery/apps/publisher/dataloader/update_course_runs.py
0 → 100644
View file @
d04db412
import
logging
from
course_discovery.apps.course_metadata.models
import
CourseRun
as
CourseRunMetaData
from
course_discovery.apps.publisher.models
import
CourseRun
logger
=
logging
.
getLogger
(
__name__
)
def
get_and_update_course_runs
(
start_id
,
end_id
):
""" Execute query according to the range."""
for
course_run
in
CourseRun
.
objects
.
filter
(
id__range
=
(
start_id
,
end_id
)):
update_course_run
(
course_run
)
def
update_course_run
(
publisher_course_run
):
""" Update the publisher course."""
try
:
if
publisher_course_run
.
lms_course_id
:
course_run_metadata
=
CourseRunMetaData
.
objects
.
filter
(
key
=
publisher_course_run
.
lms_course_id
)
.
first
()
if
(
course_run_metadata
and
(
course_run_metadata
.
short_description_override
or
course_run_metadata
.
full_description_override
or
course_run_metadata
.
title_override
)
):
publisher_course_run
.
short_description_override
=
course_run_metadata
.
short_description_override
publisher_course_run
.
full_description_override
=
course_run_metadata
.
full_description_override
publisher_course_run
.
title_override
=
course_run_metadata
.
title_override
publisher_course_run
.
save
()
logger
.
info
(
'Update course-run import with id [
%
s], lms_course_id [
%
s].'
,
publisher_course_run
.
id
,
publisher_course_run
.
lms_course_id
)
except
:
# pylint: disable=bare-except
logger
.
error
(
'Exception appear in updating course-run-id [
%
s].'
,
publisher_course_run
.
pk
)
course_discovery/apps/publisher/management/commands/tests/test_import_courses.py
View file @
d04db412
import
ddt
import
mock
from
django.core.management
import
CommandError
,
call_command
from
django.db
import
IntegrityError
from
django.test
import
TestCase
from
testfixtures
import
LogCapture
from
course_discovery.apps.course_metadata.models
import
CourseRun
from
course_discovery.apps.course_metadata.tests.factories
import
(
CourseFactory
,
CourseRunFactory
,
OrganizationFactory
,
PersonFactory
,
SeatFactory
,
SubjectFactory
)
from
course_discovery.apps.ietf_language_tags.models
import
LanguageTag
from
course_discovery.apps.publisher.dataloader.create_courses
import
logger
as
dataloader_logger
from
course_discovery.apps.publisher.dataloader.update_course_runs
import
logger
as
update_logger
from
course_discovery.apps.publisher.models
import
Course
as
Publisher_Course
from
course_discovery.apps.publisher.models
import
CourseRun
as
Publisher_CourseRun
from
course_discovery.apps.publisher.tests
import
factories
...
...
@@ -56,7 +59,7 @@ class ImportCoursesTests(TestCase):
self
.
course_2
=
CourseFactory
()
self
.
command_name
=
'import_metadata_courses'
self
.
command_args
=
[
'--start_id={}'
.
format
(
self
.
course
.
id
),
'--end_id={}'
.
format
(
format
(
self
.
course
.
id
)
)]
self
.
command_args
=
[
'--start_id={}'
.
format
(
self
.
course
.
id
),
'--end_id={}'
.
format
(
self
.
course
.
id
)]
@mock.patch
(
'course_discovery.apps.publisher.dataloader.create_courses.process_course'
)
def
test_query_return_correct_course
(
self
,
process_course
):
...
...
@@ -101,7 +104,7 @@ class CreateCoursesTests(TestCase):
self
.
course
=
CourseFactory
(
subjects
=
self
.
subjects
)
self
.
command_name
=
'import_metadata_courses'
self
.
command_args
=
[
'--start_id={}'
.
format
(
self
.
course
.
id
),
'--end_id={}'
.
format
(
format
(
self
.
course
.
id
)
)]
self
.
command_args
=
[
'--start_id={}'
.
format
(
self
.
course
.
id
),
'--end_id={}'
.
format
(
self
.
course
.
id
)]
# create multiple course-runs against course.
course_runs
=
CourseRunFactory
.
create_batch
(
...
...
@@ -294,3 +297,111 @@ class CreateCoursesTests(TestCase):
sorted
([(
seat
.
type
,
seat
.
price
,
seat
.
credit_provider
,
seat
.
currency
)
for
seat
in
metadata_seats
]),
sorted
([(
seat
.
type
,
seat
.
price
,
seat
.
credit_provider
,
seat
.
currency
)
for
seat
in
publisher_seats
])
)
# pylint: disable=no-member
@ddt.ddt
class
UpdateCourseRunsTests
(
TestCase
):
def
setUp
(
self
):
super
(
UpdateCourseRunsTests
,
self
)
.
setUp
()
self
.
update_command_name
=
'update_publisher_course_runs'
# create multiple course-runs against course.
self
.
metadata_course_runs
=
[]
self
.
publisher_runs
=
[]
# add metadata course-runs and add publisher course runs
for
i
in
range
(
1
,
3
):
course_run
=
CourseRunFactory
(
short_description_override
=
'short description {}'
.
format
(
i
),
full_description_override
=
'full description {}'
.
format
(
i
),
title_override
=
'title {}'
.
format
(
i
)
)
self
.
metadata_course_runs
.
append
(
course_run
)
self
.
publisher_runs
.
append
(
factories
.
CourseRunFactory
(
lms_course_id
=
course_run
.
key
))
self
.
command_args
=
[
'--start_id={}'
.
format
(
self
.
publisher_runs
[
0
]
.
id
),
'--end_id={}'
.
format
(
self
.
publisher_runs
[
-
1
]
.
id
)
]
def
test_course_run_update_successfully
(
self
):
""" Verify that publisher course run updates successfully."""
self
.
assert_publisher_course_runs_before_update
()
call_command
(
self
.
update_command_name
,
*
self
.
command_args
)
self
.
assert_updated_course_runs
()
def
test_course_run_does_not_update_if_values_empty
(
self
):
""" Verify that publisher course run not updates."""
self
.
assert_publisher_course_runs_before_update
()
for
run
in
self
.
metadata_course_runs
:
course_run
=
CourseRun
.
objects
.
filter
(
key
=
run
.
key
)
.
first
()
course_run
.
short_description_override
=
None
course_run
.
title_override
=
None
course_run
.
full_description_override
=
None
course_run
.
save
()
call_command
(
self
.
update_command_name
,
*
self
.
command_args
)
# values remains unchanged.
self
.
assert_publisher_course_runs_before_update
()
def
test_successfully_log_message
(
self
):
""" Verify that script logs the successful message after import."""
command_args
=
[
'--start_id={}'
.
format
(
self
.
publisher_runs
[
0
]
.
id
),
'--end_id={}'
.
format
(
self
.
publisher_runs
[
0
]
.
id
)
]
with
LogCapture
(
update_logger
.
name
)
as
log_capture
:
call_command
(
self
.
update_command_name
,
*
command_args
)
log_capture
.
check
(
(
update_logger
.
name
,
'INFO'
,
'Update course-run import with id [{}], lms_course_id [{}].'
.
format
(
self
.
publisher_runs
[
0
]
.
id
,
self
.
publisher_runs
[
0
]
.
lms_course_id
)
)
)
def
test_script_logs_error_message
(
self
):
""" Verify that script logs the error message if any error occur."""
command_args
=
[
'--start_id={}'
.
format
(
self
.
publisher_runs
[
0
]
.
id
),
'--end_id={}'
.
format
(
self
.
publisher_runs
[
0
]
.
id
)
]
with
mock
.
patch
.
object
(
Publisher_CourseRun
,
"save"
)
as
mock_method
:
mock_method
.
side_effect
=
IntegrityError
with
LogCapture
(
update_logger
.
name
)
as
log_capture
:
call_command
(
self
.
update_command_name
,
*
command_args
)
log_capture
.
check
(
(
update_logger
.
name
,
'ERROR'
,
'Exception appear in updating course-run-id [{}].'
.
format
(
self
.
publisher_runs
[
0
]
.
id
)
)
)
def
assert_publisher_course_runs_before_update
(
self
):
""" Assert method to verify the course runs before updation."""
for
course_run
in
self
.
metadata_course_runs
:
publisher_course_run
=
Publisher_CourseRun
.
objects
.
filter
(
lms_course_id
=
course_run
.
key
)
.
first
()
self
.
assertNotEqual
(
publisher_course_run
.
short_description_override
,
course_run
.
short_description_override
)
self
.
assertNotEqual
(
publisher_course_run
.
title_override
,
course_run
.
title_override
)
self
.
assertNotEqual
(
publisher_course_run
.
full_description_override
,
course_run
.
full_description_override
)
def
assert_updated_course_runs
(
self
):
""" Assert method to verify the course runs after updation."""
for
course_run
in
self
.
metadata_course_runs
:
publisher_course_run
=
Publisher_CourseRun
.
objects
.
filter
(
lms_course_id
=
course_run
.
key
)
.
first
()
self
.
assertEqual
(
publisher_course_run
.
short_description_override
,
course_run
.
short_description_override
)
self
.
assertEqual
(
publisher_course_run
.
title_override
,
course_run
.
title_override
)
self
.
assertEqual
(
publisher_course_run
.
full_description_override
,
course_run
.
full_description_override
)
course_discovery/apps/publisher/management/commands/update_publisher_course_runs.py
0 → 100644
View file @
d04db412
import
logging
from
django.core.management
import
BaseCommand
from
course_discovery.apps.publisher.dataloader.update_course_runs
import
get_and_update_course_runs
logger
=
logging
.
getLogger
(
__name__
)
class
Command
(
BaseCommand
):
help
=
'Update course-runs into publisher app.'
def
add_arguments
(
self
,
parser
):
parser
.
add_argument
(
'--start_id'
,
action
=
'store'
,
dest
=
'start_id'
,
default
=
None
,
required
=
True
,
help
=
'The Primary key value starting id to update the course-runs.'
)
parser
.
add_argument
(
'--end_id'
,
action
=
'store'
,
dest
=
'end_id'
,
default
=
None
,
required
=
True
,
help
=
'To this id course-runs will be updated.'
)
def
handle
(
self
,
*
args
,
**
options
):
""" Import the course according to the given range."""
start_id
=
options
.
get
(
'start_id'
)
end_id
=
options
.
get
(
'end_id'
)
get_and_update_course_runs
(
start_id
,
end_id
)
course_discovery/apps/publisher/tests/factories.py
View file @
d04db412
...
...
@@ -56,6 +56,8 @@ class CourseRunFactory(factory.DjangoModelFactory):
contacted_partner_manager
=
FuzzyChoice
((
True
,
False
))
video_language
=
factory
.
Iterator
(
LanguageTag
.
objects
.
all
())
short_description_override
=
FuzzyText
()
title_override
=
FuzzyText
()
full_description_override
=
FuzzyText
()
class
Meta
:
model
=
CourseRun
...
...
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