Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
ecommerce
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
ecommerce
Commits
35e5ea8a
Commit
35e5ea8a
authored
Jul 24, 2015
by
Clinton Blackburn
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #225 from edx/publish-api
Added API endpoint for course publishing
parents
992d83e6
9c10fb5e
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
54 additions
and
1 deletions
+54
-1
ecommerce/courses/publishers.py
+1
-1
ecommerce/extensions/api/v2/tests/views/test_courses.py
+33
-0
ecommerce/extensions/api/v2/views.py
+20
-0
No files found.
ecommerce/courses/publishers.py
View file @
35e5ea8a
...
...
@@ -45,7 +45,7 @@ class LMSPublisher(object):
if
not
settings
.
COMMERCE_API_URL
:
logger
.
error
(
'COMMERCE_API_URL is not set. Commerce data will not be published!'
)
return
return
False
modes
=
[
self
.
serialize_seat_for_commerce_api
(
seat
)
for
seat
in
course
.
seat_products
]
course_id
=
course
.
id
...
...
ecommerce/extensions/api/v2/tests/views/test_courses.py
View file @
35e5ea8a
...
...
@@ -3,9 +3,12 @@ import json
from
django.core.urlresolvers
import
reverse
from
django.test
import
TestCase
import
mock
from
oscar.core.loading
import
get_model
,
get_class
from
waffle
import
Switch
from
ecommerce.courses.models
import
Course
from
ecommerce.courses.publishers
import
LMSPublisher
from
ecommerce.extensions.api.v2.tests.views
import
JSON_CONTENT_TYPE
,
TestServerUrlMixin
from
ecommerce.extensions.catalogue.tests.mixins
import
CourseCatalogTestMixin
from
ecommerce.tests.mixins
import
UserMixin
...
...
@@ -111,3 +114,33 @@ class CourseViewSetTests(TestServerUrlMixin, CourseCatalogTestMixin, UserMixin,
response
=
self
.
client
.
delete
(
path
)
self
.
assertEqual
(
response
.
status_code
,
405
)
self
.
assertTrue
(
Course
.
objects
.
filter
(
id
=
course_id
)
.
exists
())
def
assert_publish_response
(
self
,
response
,
status_code
,
msg
):
self
.
assertEqual
(
response
.
status_code
,
status_code
)
self
.
assertDictEqual
(
json
.
loads
(
response
.
content
),
{
'status'
:
msg
.
format
(
course_id
=
self
.
course
.
id
)})
def
test_publish
(
self
):
""" Verify the view publishes course data to LMS. """
course_id
=
self
.
course
.
id
path
=
reverse
(
'api:v2:course-publish'
,
kwargs
=
{
'pk'
:
course_id
})
# Method should return a 500 if the switch is inactive
switch
,
_created
=
Switch
.
objects
.
get_or_create
(
name
=
'publish_course_modes_to_lms'
,
active
=
False
)
response
=
self
.
client
.
post
(
path
)
msg
=
'Course [{course_id}] was not published to LMS '
\
'because the switch [publish_course_modes_to_lms] is disabled.'
self
.
assert_publish_response
(
response
,
500
,
msg
)
switch
.
active
=
True
switch
.
save
()
with
mock
.
patch
.
object
(
LMSPublisher
,
'publish'
)
as
mock_publish
:
# If publish fails, return a 500
mock_publish
.
return_value
=
False
response
=
self
.
client
.
post
(
path
)
self
.
assert_publish_response
(
response
,
500
,
'An error occurred while publishing [{course_id}] to LMS.'
)
# If publish succeeds, return a 200
mock_publish
.
return_value
=
True
response
=
self
.
client
.
post
(
path
)
self
.
assert_publish_response
(
response
,
200
,
'Course [{course_id}] was successfully published to LMS.'
)
ecommerce/extensions/api/v2/views.py
View file @
35e5ea8a
...
...
@@ -5,10 +5,12 @@ from django.conf import settings
from
django.contrib.auth
import
get_user_model
from
oscar.core.loading
import
get_model
from
rest_framework
import
status
,
generics
,
viewsets
,
mixins
from
rest_framework.decorators
import
detail_route
from
rest_framework.exceptions
import
ParseError
from
rest_framework.permissions
import
IsAuthenticated
,
DjangoModelPermissions
,
IsAdminUser
from
rest_framework.response
import
Response
from
rest_framework_extensions.mixins
import
NestedViewSetMixin
import
waffle
from
ecommerce.courses.models
import
Course
from
ecommerce.extensions.analytics.utils
import
audit_log
...
...
@@ -496,6 +498,24 @@ class CourseViewSet(NonDestroyableModelViewSet):
serializer_class
=
serializers
.
CourseSerializer
permission_classes
=
(
IsAuthenticated
,
IsAdminUser
,)
@detail_route
(
methods
=
[
'post'
])
def
publish
(
self
,
request
,
pk
=
None
):
# pylint: disable=unused-argument
""" Publish the course to LMS. """
course
=
self
.
get_object
()
published
=
False
msg
=
'Course [{course_id}] was not published to LMS '
\
'because the switch [publish_course_modes_to_lms] is disabled.'
if
waffle
.
switch_is_active
(
'publish_course_modes_to_lms'
):
published
=
course
.
publish_to_lms
()
if
published
:
msg
=
'Course [{course_id}] was successfully published to LMS.'
else
:
msg
=
'An error occurred while publishing [{course_id}] to LMS.'
return
Response
({
'status'
:
msg
.
format
(
course_id
=
course
.
id
)},
status
=
status
.
HTTP_200_OK
if
published
else
status
.
HTTP_500_INTERNAL_SERVER_ERROR
)
class
ProductViewSet
(
NestedViewSetMixin
,
NonDestroyableModelViewSet
):
queryset
=
Product
.
objects
.
all
()
...
...
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