Commit 937bd0de by Bill DeRusha

Add csv download link to catalog list page

parent 81e5be22
......@@ -26,6 +26,7 @@ CatalogPreviewFactory({
% for catalog in catalogs:
<li>
<a href="${reverse('api_admin:catalog-edit', args=(catalog.id,))}">${catalog.name}</a>
<a href="${reverse('api_admin:catalog-csv', args=(catalog.id,))}">download CSV</a>
</li>
% endfor
</ul>
......
......@@ -6,7 +6,7 @@ from django.contrib.auth.decorators import login_required
from openedx.core.djangoapps.api_admin.decorators import api_access_enabled_or_404
from openedx.core.djangoapps.api_admin.views import (
ApiRequestView, ApiRequestStatusView, ApiTosView, CatalogListView, CatalogEditView,
ApiRequestView, ApiRequestStatusView, ApiTosView, CatalogCSVView, CatalogListView, CatalogEditView,
CatalogPreviewView, CatalogSearchView
)
......@@ -49,6 +49,15 @@ urlpatterns = (
name='catalog-edit',
),
url(
r'^catalogs/(?P<catalog_id>\d+)/csv$',
staff_member_required(
api_access_enabled_or_404(CatalogCSVView.as_view()),
login_url='dashboard',
redirect_field_name=None
),
name='catalog-csv',
),
url(
r'^catalogs/$',
staff_member_required(
api_access_enabled_or_404(CatalogSearchView.as_view()),
......
"""Views for API management."""
from datetime import datetime
import logging
from django.conf import settings
from django.contrib.sites.shortcuts import get_current_site
from django.core.urlresolvers import reverse_lazy, reverse
from django.http import HttpResponse
from django.http.response import JsonResponse
from django.shortcuts import redirect
from django.utils.translation import ugettext as _
......@@ -232,3 +234,18 @@ class CatalogPreviewView(View):
else:
results = {'count': 0, 'results': [], 'next': None, 'prev': None}
return JsonResponse(results)
class CatalogCSVView(View):
"""View to download a CSV of catalog courses."""
def get(self, request, catalog_id):
"""Download a CSV of course_runs in this catalog."""
client = course_discovery_api_client(request.user)
data = client.api.v1.catalogs(catalog_id).csv.get()
response = HttpResponse(data, content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="catalog_{id}_{date}.csv"'.format(
id=catalog_id, date=datetime.utcnow().strftime('%Y-%m-%d')
)
return response
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