Full front-end/back-end connection via Backbone.sync. No data validation whatsoever.
This commit is contained in:
@@ -427,19 +427,21 @@ def textbook_index(request, org, course, name):
|
||||
|
||||
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)
|
||||
course_module = modulestore().get_item(location, depth=3)
|
||||
store = modulestore()
|
||||
course_module = store.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))
|
||||
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)
|
||||
else:
|
||||
upload_asset_callback_url = reverse('upload_asset', kwargs={
|
||||
@@ -447,7 +449,7 @@ def textbook_index(request, org, course, name):
|
||||
'course': course,
|
||||
'coursename': name,
|
||||
})
|
||||
asset_index_url = reverse('asset_index', kwargs={
|
||||
textbook_url = reverse('textbook_index', kwargs={
|
||||
'org': org,
|
||||
'course': course,
|
||||
'name': name,
|
||||
@@ -455,7 +457,6 @@ def textbook_index(request, org, course, 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,
|
||||
'textbook_url': textbook_url,
|
||||
})
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
<script type="text/javascript" src="${static.url('js/vendor/backbone-associations-min.js')}"></script>
|
||||
<script type="text/javascript">
|
||||
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({
|
||||
defaults: function() {
|
||||
@@ -56,6 +56,12 @@ CMS.Models.Textbook = Backbone.AssociatedModel.extend({
|
||||
delete response.url;
|
||||
}
|
||||
return response;
|
||||
},
|
||||
toJSON: function() {
|
||||
return {
|
||||
tab_title: this.get('name'),
|
||||
chapters: this.get('chapters').toJSON()
|
||||
}
|
||||
}
|
||||
})
|
||||
CMS.Views.TextbookShow = Backbone.View.extend({
|
||||
@@ -70,9 +76,7 @@ CMS.Views.TextbookShow = Backbone.View.extend({
|
||||
"click .hide-chapters": "hideChapters"
|
||||
},
|
||||
render: function() {
|
||||
var attrs = $.extend({}, this.model.attributes);
|
||||
attrs.chapters = this.model.chapters;
|
||||
this.$el.html(this.template(attrs));
|
||||
this.$el.html(this.template(this.model.attributes));
|
||||
return this;
|
||||
},
|
||||
editTextbook: function(e) {
|
||||
@@ -95,9 +99,10 @@ CMS.Views.TextbookShow = Backbone.View.extend({
|
||||
CMS.Views.TextbookEdit = Backbone.View.extend({
|
||||
initialize: function() {
|
||||
this.template = _.template($("#new-textbook-tpl").text());
|
||||
this.listenTo(this.model.chapters, "add", this.addOne);
|
||||
this.listenTo(this.model.chapters, "reset", this.addAll);
|
||||
this.listenTo(this.model.chapters, "all", this.render);
|
||||
var chapters = this.model.get('chapters');
|
||||
this.listenTo(chapters, "add", this.addOne);
|
||||
this.listenTo(chapters, "reset", this.addAll);
|
||||
this.listenTo(chapters, "all", this.render);
|
||||
this.listenTo(this.model.collection, "editOne", this.remove);
|
||||
},
|
||||
tagName: "form",
|
||||
@@ -122,19 +127,19 @@ CMS.Views.TextbookEdit = Backbone.View.extend({
|
||||
return this;
|
||||
},
|
||||
addAll: function() {
|
||||
this.model.chapters.each(this.addOne, this);
|
||||
this.model.get('chapters').each(this.addOne, this);
|
||||
},
|
||||
createChapter: function(e) {
|
||||
if(e && e.preventDefault) { e.preventDefault(); }
|
||||
this.save();
|
||||
this.model.chapters.add([{}])
|
||||
this.model.get('chapters').add([{}]);
|
||||
},
|
||||
save: function() {
|
||||
var name = this.$("#textbook-name-input").val();
|
||||
var textbook = this.model;
|
||||
textbook.set("name", name);
|
||||
_.each(this.$("li"), function(li, i) {
|
||||
var chapter = textbook.chapters.at(i);
|
||||
var chapter = textbook.get('chapters').at(i);
|
||||
chapter.set({
|
||||
"name": $(".chapter-name", li).val(),
|
||||
"asset_path": $(".chapter-asset-path", li).val()
|
||||
@@ -144,7 +149,25 @@ CMS.Views.TextbookEdit = Backbone.View.extend({
|
||||
},
|
||||
saveAndClose: function(e) {
|
||||
if(e && e.preventDefault) { e.preventDefault(); }
|
||||
return this.save().close();
|
||||
this.save();
|
||||
msg = new CMS.Models.SystemFeedback({
|
||||
intent: "saving",
|
||||
title: gettext("Saving…")
|
||||
});
|
||||
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) {
|
||||
if(e && e.preventDefault) { e.preventDefault(); }
|
||||
@@ -164,12 +187,15 @@ CMS.Views.TextbookEdit = Backbone.View.extend({
|
||||
})
|
||||
CMS.Collections.TextbookSet = Backbone.Collection.extend({
|
||||
model: CMS.Models.Textbook,
|
||||
url: ASSET_INDEX_URL,
|
||||
url: TEXTBOOK_URL,
|
||||
initialize: function() {
|
||||
this.listenTo(this, "editOne", this.editOne);
|
||||
},
|
||||
editOne: function(textbook) {
|
||||
this.editing = textbook;
|
||||
},
|
||||
save: function(options) {
|
||||
return this.sync('update', this, options);
|
||||
}
|
||||
})
|
||||
CMS.Views.ListTextbooks = Backbone.View.extend({
|
||||
@@ -229,6 +255,12 @@ CMS.Models.Chapter = Backbone.AssociatedModel.extend({
|
||||
delete response.url;
|
||||
}
|
||||
return response;
|
||||
},
|
||||
toJSON: function() {
|
||||
return {
|
||||
title: this.get('name'),
|
||||
url: this.get('asset_path')
|
||||
}
|
||||
}
|
||||
})
|
||||
CMS.Collections.ChapterSet = Backbone.Collection.extend({
|
||||
@@ -391,7 +423,7 @@ var section = new CMS.Models.Section({
|
||||
id: "${course.id}",
|
||||
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});
|
||||
|
||||
$(function() {
|
||||
|
||||
Reference in New Issue
Block a user