From 00b885deaab1be707fa2a79ad9308e8f7d8ab9bf Mon Sep 17 00:00:00 2001 From: David Baumgold Date: Tue, 11 Jun 2013 15:08:05 -0400 Subject: [PATCH] Update pdf textbooks backend to respond to AJAX requests with JSON --- cms/djangoapps/contentstore/views/course.py | 51 +++++++++++++-------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/cms/djangoapps/contentstore/views/course.py b/cms/djangoapps/contentstore/views/course.py index 0293421bea..0457c93916 100644 --- a/cms/djangoapps/contentstore/views/course.py +++ b/cms/djangoapps/contentstore/views/course.py @@ -12,6 +12,8 @@ from django.core.urlresolvers import reverse from mitxmako.shortcuts import render_to_response from xmodule.modulestore.django import modulestore +from xmodule.modulestore.inheritance import own_metadata + from xmodule.modulestore.exceptions import ( ItemNotFoundError, InvalidLocationError) from xmodule.modulestore import Location @@ -425,26 +427,35 @@ def textbook_index(request, org, course, name): org, course, name: Attributes of the Location for the item to edit """ - upload_asset_callback_url = reverse('upload_asset', kwargs={ - 'org': org, - 'course': course, - 'coursename': name, - }) - asset_index_url = reverse('asset_index', kwargs={ - 'org': org, - 'course': course, - 'name': name, - }) - course_reference = StaticContent.compute_location(org, course, name) - assets_db_objs = contentstore().get_all_content_for_course(course_reference) + store = contentstore() + assets_db_objs = store.get_all_content_for_course(course_reference) assets_json_objs = assets_to_json_dict(assets_db_objs) + assets_pdfs = [asset for asset in assets_json_objs if asset["path"].endswith(".pdf")] location = get_location_and_verify_access(request, org, course, name) - course_obj = modulestore().get_item(location, depth=3) - return render_to_response('textbooks.html', { - 'context_course': course_obj, - 'course': course_obj, - 'assets': assets_json_objs, - 'upload_asset_callback_url': upload_asset_callback_url, - 'asset_index_url': asset_index_url, - }) + course_module = modulestore().get_item(location, depth=3) + + if request.is_ajax(): + if request.method == 'GET': + return HttpResponse(json.dumps(course_module.pdf_textbooks), content_type="application/json") + elif request.method == 'POST': + store.update_metadata(course.location, own_metadata(request.POST)) + return HttpResponse('', content_type="application/json", status=201) + else: + upload_asset_callback_url = reverse('upload_asset', kwargs={ + 'org': org, + 'course': course, + 'coursename': name, + }) + asset_index_url = reverse('asset_index', kwargs={ + 'org': org, + 'course': course, + 'name': name, + }) + return render_to_response('textbooks.html', { + 'context_course': course_module, + 'course': course_module, + 'assets': assets_pdfs, + 'upload_asset_callback_url': upload_asset_callback_url, + 'asset_index_url': asset_index_url, + })