Commit 6a3360ce by David Baumgold

Full front-end/back-end connection via Backbone.sync. No data validation whatsoever.

parent 22a7735a
...@@ -427,19 +427,21 @@ def textbook_index(request, org, course, name): ...@@ -427,19 +427,21 @@ def textbook_index(request, org, course, name):
org, course, name: Attributes of the Location for the item to edit org, course, name: Attributes of the Location for the item to edit
""" """
course_reference = StaticContent.compute_location(org, course, name)
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) location = get_location_and_verify_access(request, org, course, name)
course_module = modulestore().get_item(location, depth=3) store = modulestore()
course_module = store.get_item(location, depth=3)
if request.is_ajax(): if request.is_ajax():
if request.method == 'GET': if request.method == 'GET':
return HttpResponse(json.dumps(course_module.pdf_textbooks), content_type="application/json") return HttpResponse(json.dumps(course_module.pdf_textbooks), content_type="application/json")
elif request.method == 'POST': elif request.method == 'POST':
store.update_metadata(course.location, own_metadata(request.POST)) try:
obj = json.loads(request.raw_post_data)
except ValueError:
msg = {"error": "invalid JSON"}
return HttpResponseBadRequest(json.dumps(msg), content_type="application/json")
course_module.pdf_textbooks = obj
store.update_metadata(course_module.location, own_metadata(course_module))
return HttpResponse('', content_type="application/json", status=201) return HttpResponse('', content_type="application/json", status=201)
else: else:
upload_asset_callback_url = reverse('upload_asset', kwargs={ upload_asset_callback_url = reverse('upload_asset', kwargs={
...@@ -447,7 +449,7 @@ def textbook_index(request, org, course, name): ...@@ -447,7 +449,7 @@ def textbook_index(request, org, course, name):
'course': course, 'course': course,
'coursename': name, 'coursename': name,
}) })
asset_index_url = reverse('asset_index', kwargs={ textbook_url = reverse('textbook_index', kwargs={
'org': org, 'org': org,
'course': course, 'course': course,
'name': name, 'name': name,
...@@ -455,7 +457,6 @@ def textbook_index(request, org, course, name): ...@@ -455,7 +457,6 @@ def textbook_index(request, org, course, name):
return render_to_response('textbooks.html', { return render_to_response('textbooks.html', {
'context_course': course_module, 'context_course': course_module,
'course': course_module, 'course': course_module,
'assets': assets_pdfs,
'upload_asset_callback_url': upload_asset_callback_url, 'upload_asset_callback_url': upload_asset_callback_url,
'asset_index_url': asset_index_url, 'textbook_url': textbook_url,
}) })
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
<script type="text/javascript" src="${static.url('js/vendor/backbone-associations-min.js')}"></script> <script type="text/javascript" src="${static.url('js/vendor/backbone-associations-min.js')}"></script>
<script type="text/javascript"> <script type="text/javascript">
window.UPLOAD_ASSET_CALLBACK_URL = "${upload_asset_callback_url}" window.UPLOAD_ASSET_CALLBACK_URL = "${upload_asset_callback_url}"
window.ASSET_INDEX_URL = "${asset_index_url}" window.TEXTBOOK_URL = "${textbook_url}"
CMS.Models.Textbook = Backbone.AssociatedModel.extend({ CMS.Models.Textbook = Backbone.AssociatedModel.extend({
defaults: function() { defaults: function() {
...@@ -56,6 +56,12 @@ CMS.Models.Textbook = Backbone.AssociatedModel.extend({ ...@@ -56,6 +56,12 @@ CMS.Models.Textbook = Backbone.AssociatedModel.extend({
delete response.url; delete response.url;
} }
return response; return response;
},
toJSON: function() {
return {
tab_title: this.get('name'),
chapters: this.get('chapters').toJSON()
}
} }
}) })
CMS.Views.TextbookShow = Backbone.View.extend({ CMS.Views.TextbookShow = Backbone.View.extend({
...@@ -70,9 +76,7 @@ CMS.Views.TextbookShow = Backbone.View.extend({ ...@@ -70,9 +76,7 @@ CMS.Views.TextbookShow = Backbone.View.extend({
"click .hide-chapters": "hideChapters" "click .hide-chapters": "hideChapters"
}, },
render: function() { render: function() {
var attrs = $.extend({}, this.model.attributes); this.$el.html(this.template(this.model.attributes));
attrs.chapters = this.model.chapters;
this.$el.html(this.template(attrs));
return this; return this;
}, },
editTextbook: function(e) { editTextbook: function(e) {
...@@ -95,9 +99,10 @@ CMS.Views.TextbookShow = Backbone.View.extend({ ...@@ -95,9 +99,10 @@ CMS.Views.TextbookShow = Backbone.View.extend({
CMS.Views.TextbookEdit = Backbone.View.extend({ CMS.Views.TextbookEdit = Backbone.View.extend({
initialize: function() { initialize: function() {
this.template = _.template($("#new-textbook-tpl").text()); this.template = _.template($("#new-textbook-tpl").text());
this.listenTo(this.model.chapters, "add", this.addOne); var chapters = this.model.get('chapters');
this.listenTo(this.model.chapters, "reset", this.addAll); this.listenTo(chapters, "add", this.addOne);
this.listenTo(this.model.chapters, "all", this.render); this.listenTo(chapters, "reset", this.addAll);
this.listenTo(chapters, "all", this.render);
this.listenTo(this.model.collection, "editOne", this.remove); this.listenTo(this.model.collection, "editOne", this.remove);
}, },
tagName: "form", tagName: "form",
...@@ -122,19 +127,19 @@ CMS.Views.TextbookEdit = Backbone.View.extend({ ...@@ -122,19 +127,19 @@ CMS.Views.TextbookEdit = Backbone.View.extend({
return this; return this;
}, },
addAll: function() { addAll: function() {
this.model.chapters.each(this.addOne, this); this.model.get('chapters').each(this.addOne, this);
}, },
createChapter: function(e) { createChapter: function(e) {
if(e && e.preventDefault) { e.preventDefault(); } if(e && e.preventDefault) { e.preventDefault(); }
this.save(); this.save();
this.model.chapters.add([{}]) this.model.get('chapters').add([{}]);
}, },
save: function() { save: function() {
var name = this.$("#textbook-name-input").val(); var name = this.$("#textbook-name-input").val();
var textbook = this.model; var textbook = this.model;
textbook.set("name", name); textbook.set("name", name);
_.each(this.$("li"), function(li, i) { _.each(this.$("li"), function(li, i) {
var chapter = textbook.chapters.at(i); var chapter = textbook.get('chapters').at(i);
chapter.set({ chapter.set({
"name": $(".chapter-name", li).val(), "name": $(".chapter-name", li).val(),
"asset_path": $(".chapter-asset-path", li).val() "asset_path": $(".chapter-asset-path", li).val()
...@@ -144,7 +149,25 @@ CMS.Views.TextbookEdit = Backbone.View.extend({ ...@@ -144,7 +149,25 @@ CMS.Views.TextbookEdit = Backbone.View.extend({
}, },
saveAndClose: function(e) { saveAndClose: function(e) {
if(e && e.preventDefault) { e.preventDefault(); } if(e && e.preventDefault) { e.preventDefault(); }
return this.save().close(); this.save();
msg = new CMS.Models.SystemFeedback({
intent: "saving",
title: gettext("Saving&hellip;")
});
notif = new CMS.Views.Notification({
model: msg,
closeIcon: false,
minShown: 1250
});
var that = this;
this.model.collection.save({
success: function() {
that.close();
},
complete: function() {
notif.hide();
}
})
}, },
cancel: function(e) { cancel: function(e) {
if(e && e.preventDefault) { e.preventDefault(); } if(e && e.preventDefault) { e.preventDefault(); }
...@@ -164,12 +187,15 @@ CMS.Views.TextbookEdit = Backbone.View.extend({ ...@@ -164,12 +187,15 @@ CMS.Views.TextbookEdit = Backbone.View.extend({
}) })
CMS.Collections.TextbookSet = Backbone.Collection.extend({ CMS.Collections.TextbookSet = Backbone.Collection.extend({
model: CMS.Models.Textbook, model: CMS.Models.Textbook,
url: ASSET_INDEX_URL, url: TEXTBOOK_URL,
initialize: function() { initialize: function() {
this.listenTo(this, "editOne", this.editOne); this.listenTo(this, "editOne", this.editOne);
}, },
editOne: function(textbook) { editOne: function(textbook) {
this.editing = textbook; this.editing = textbook;
},
save: function(options) {
return this.sync('update', this, options);
} }
}) })
CMS.Views.ListTextbooks = Backbone.View.extend({ CMS.Views.ListTextbooks = Backbone.View.extend({
...@@ -229,6 +255,12 @@ CMS.Models.Chapter = Backbone.AssociatedModel.extend({ ...@@ -229,6 +255,12 @@ CMS.Models.Chapter = Backbone.AssociatedModel.extend({
delete response.url; delete response.url;
} }
return response; return response;
},
toJSON: function() {
return {
title: this.get('name'),
url: this.get('asset_path')
}
} }
}) })
CMS.Collections.ChapterSet = Backbone.Collection.extend({ CMS.Collections.ChapterSet = Backbone.Collection.extend({
...@@ -391,7 +423,7 @@ var section = new CMS.Models.Section({ ...@@ -391,7 +423,7 @@ var section = new CMS.Models.Section({
id: "${course.id}", id: "${course.id}",
name: "${course.display_name_with_default | h}" name: "${course.display_name_with_default | h}"
}); });
var textbooks = new CMS.Collections.TextbookSet(${json.dumps(assets)}); var textbooks = new CMS.Collections.TextbookSet(${json.dumps(course.pdf_textbooks)}, {parse: true});
var tbView = new CMS.Views.ListTextbooks({collection: textbooks}); var tbView = new CMS.Views.ListTextbooks({collection: textbooks});
$(function() { $(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