Merge pull request #12708 from edx/peter-fogg/fix-catalog-404

Fix 500 error when searching for a user without catalogs.
This commit is contained in:
Peter Fogg
2016-06-09 14:30:49 -04:00
2 changed files with 18 additions and 5 deletions

View File

@@ -278,6 +278,13 @@ class CatalogListViewTest(CatalogTest):
self.assertEqual(response.status_code, 200)
self.assertIn(catalog.name, response.content.decode('utf-8'))
@httpretty.activate
def test_get_no_catalogs(self):
"""Verify that the view works when no catalogs are set up."""
self.mock_catalog_api('api/v1/catalogs/', {}, status_code=404)
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
@httpretty.activate
def test_post(self):
catalog_data = {

View File

@@ -13,6 +13,7 @@ from django.views.generic.edit import CreateView
from oauth2_provider.generators import generate_client_secret, generate_client_id
from oauth2_provider.models import get_application_model
from oauth2_provider.views import ApplicationRegistration
from slumber.exceptions import HttpNotFoundError
from edxmako.shortcuts import render_to_response
from openedx.core.djangoapps.api_admin.decorators import require_api_access
@@ -140,11 +141,18 @@ class CatalogListView(View):
template = 'api_admin/catalogs/list.html'
def _get_catalogs(self, client, username):
"""Retrieve catalogs for a user. Returns the empty list if none are found."""
try:
response = client.api.v1.catalogs.get(username=username)
return [Catalog(attributes=catalog) for catalog in response['results']]
except HttpNotFoundError:
return []
def get(self, request, username):
"""Display a list of a user's catalogs."""
client = course_discovery_api_client(request.user)
response = client.api.v1.catalogs.get(username=username)
catalogs = [Catalog(attributes=catalog) for catalog in response['results']]
catalogs = self._get_catalogs(client, username)
return render_to_response(self.template, {
'username': username,
'catalogs': catalogs,
@@ -157,10 +165,8 @@ class CatalogListView(View):
"""Create a new catalog for a user."""
form = CatalogForm(request.POST)
client = course_discovery_api_client(request.user)
if not form.is_valid():
response = client.api.v1.catalogs.get(username=username)
catalogs = [Catalog(attributes=catalog) for catalog in response['results']]
catalogs = self._get_catalogs(client, username)
return render_to_response(self.template, {
'form': form,
'catalogs': catalogs,