Commit 35e5ea8a by Clinton Blackburn

Merge pull request #225 from edx/publish-api

Added API endpoint for course publishing
parents 992d83e6 9c10fb5e
......@@ -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
......
......@@ -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.')
......@@ -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()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment