Commit e607bc70 by David Baumgold

Pull assets into Mako template for PDF textbooks

parent 19d65f81
......@@ -35,6 +35,35 @@ from .access import get_location_and_verify_access
__all__ = ['asset_index', 'upload_asset', 'import_course', 'generate_export_course', 'export_course']
def assets_to_json_dict(assets):
ret = []
for asset in assets:
obj = {
"name": asset.get("displayname", ""),
"chunkSize": asset.get("chunkSize", 0),
"path": asset.get("filename", ""),
"length": asset.get("length", 0),
}
uploaded = asset.get("uploadDate")
if uploaded:
obj["uploaded"] = uploaded.isoformat()
thumbnail = asset.get("thumbnail_location")
if thumbnail:
obj["thumbnail"] = thumbnail
idInfo = asset.get("_id")
if idInfo:
obj["id"] = "/{tag}/{org}/{course}/{revision}/{category}/{name}".format(
org=idInfo.get("org", ""),
course=idInfo.get("course", ""),
revision=idInfo.get("revision", ""),
tag=idInfo.get("tag", ""),
category=idInfo.get("category", ""),
name=idInfo.get("name", ""),
)
ret.append(obj)
return ret
@login_required
@ensure_csrf_cookie
def asset_index(request, org, course, name):
......@@ -59,6 +88,9 @@ def asset_index(request, org, course, name):
# sort in reverse upload date order
assets = sorted(assets, key=lambda asset: asset['uploadDate'], reverse=True)
if request.META.get('HTTP_ACCEPT', "").startswith("application/json"):
return HttpResponse(json.dumps(assets_to_json_dict(assets)), content_type="application/json")
asset_display = []
for asset in assets:
asset_id = asset['_id']
......
......@@ -16,6 +16,9 @@ from xmodule.modulestore.exceptions import (
ItemNotFoundError, InvalidLocationError)
from xmodule.modulestore import Location
from xmodule.contentstore.django import contentstore
from xmodule.contentstore.content import StaticContent
from contentstore.course_info_model import (
get_course_updates, update_course_updates, delete_course_update)
from contentstore.utils import (
......@@ -29,6 +32,7 @@ from auth.authz import create_all_course_groups, is_user_in_creator_group
from util.json_request import expect_json
from .access import has_access, get_location_and_verify_access
from .assets import assets_to_json_dict
from .requests import get_request_method
from .tabs import initialize_course_tabs
from .component import (
......@@ -424,13 +428,23 @@ def textbook_index(request, org, course, name):
upload_asset_callback_url = reverse('upload_asset', kwargs={
'org': org,
'course': course,
'coursename': name
'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)
assets_json_objs = assets_to_json_dict(assets_db_objs)
location = get_location_and_verify_access(request, org, course, name)
course = modulestore().get_item(location, depth=3)
course_obj = modulestore().get_item(location, depth=3)
return render_to_response('textbooks.html', {
'context_course': course,
'course': course,
'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,
})
<%inherit file="base.html" />
<%namespace name='static' file='static_content.html'/>
<%! import json %>
<%block name="title">Textbooks</%block>
<%block name="bodyclass">is-signedin course textbooks</%block>
......@@ -25,6 +26,7 @@
<%block name="jsextra">
<script type="text/javascript">
window.UPLOAD_ASSET_CALLBACK_URL = "${upload_asset_callback_url}"
window.ASSET_INDEX_URL = "${asset_index_url}"
CMS.Models.Textbook = Backbone.Model.extend({
defaults: {
......@@ -146,6 +148,7 @@ CMS.Views.TextbookEdit = Backbone.View.extend({
})
CMS.Collections.TextbookSet = Backbone.Collection.extend({
model: CMS.Models.Textbook,
url: ASSET_INDEX_URL,
initialize: function() {
this.listenTo(this, "editOne", this.editOne);
},
......@@ -358,7 +361,7 @@ var section = new CMS.Models.Section({
id: "${course.id}",
name: "${course.display_name_with_default | h}"
});
var textbooks = new CMS.Collections.TextbookSet();
var textbooks = new CMS.Collections.TextbookSet(${json.dumps(assets)});
var tbView = new CMS.Views.ListTextbooks({collection: textbooks});
$(function() {
......
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