From 2d35e48b68ff51fae09369b4a1a00d7599c454c1 Mon Sep 17 00:00:00 2001 From: Chris Dodge Date: Thu, 20 Sep 2012 11:07:11 -0400 Subject: [PATCH 1/3] Fix JSON postback error where the content-type header line can contain more info than just the application/json descriptor. Now we just to a compare on the start of the header value. --- common/djangoapps/util/json_request.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/common/djangoapps/util/json_request.py b/common/djangoapps/util/json_request.py index 391905e574..169a7e3fb4 100644 --- a/common/djangoapps/util/json_request.py +++ b/common/djangoapps/util/json_request.py @@ -6,7 +6,9 @@ import json def expect_json(view_function): @wraps(view_function) def expect_json_with_cloned_request(request, *args, **kwargs): - if request.META['CONTENT_TYPE'] == "application/json": + # cdodge: fix postback errors in CMS. The POST 'content-type' header can include additional information + # e.g. 'charset', so we can't do a direct string compare + if request.META['CONTENT_TYPE'].lower().startswith("application/json"): cloned_request = copy.copy(request) cloned_request.POST = cloned_request.POST.copy() cloned_request.POST.update(json.loads(request.body)) From 1cd81a2a4fd6db9c46c0cc697e14470c5bd6b998 Mon Sep 17 00:00:00 2001 From: Chris Dodge Date: Fri, 21 Sep 2012 09:14:28 -0400 Subject: [PATCH 2/3] work-in-progress: As a problem author, I would like to be able to edit module metadata --- cms/djangoapps/contentstore/views.py | 6 ++++++ cms/static/coffee/src/models/module.coffee | 12 ++++++++++++ cms/templates/widgets/html-edit.html | 1 + cms/templates/widgets/metadata-edit.html | 8 ++++++++ cms/templates/widgets/raw-edit.html | 1 + common/lib/xmodule/xmodule/editing_module.py | 1 + 6 files changed, 29 insertions(+) create mode 100644 cms/templates/widgets/metadata-edit.html diff --git a/cms/djangoapps/contentstore/views.py b/cms/djangoapps/contentstore/views.py index 505b897497..bffadcf666 100644 --- a/cms/djangoapps/contentstore/views.py +++ b/cms/djangoapps/contentstore/views.py @@ -110,6 +110,7 @@ def edit_item(request): 'category': item.category, 'url_name': item.url_name, 'previews': get_module_previews(request, item), + 'metadata': item.metadata }) @@ -279,6 +280,11 @@ def save_item(request): data = json.loads(request.POST['data']) modulestore().update_item(item_location, data) + # cdodge: also commit any metadata which might have been passed along in the + # POST from the client + if request.POST['metadata']: + modulestore().update_metadata(item_location, request.POST['metadata']) + # Export the course back to github # This uses wildcarding to find the course, which requires handling # multiple courses returned, but there should only ever be one diff --git a/cms/static/coffee/src/models/module.coffee b/cms/static/coffee/src/models/module.coffee index 159172e852..b1bbb3bb72 100644 --- a/cms/static/coffee/src/models/module.coffee +++ b/cms/static/coffee/src/models/module.coffee @@ -2,14 +2,26 @@ class CMS.Models.Module extends Backbone.Model url: '/save_item' defaults: data: '' + metadata: {} loadModule: (element) -> elt = $(element).find('.xmodule_edit').first() @module = XModule.loadModule(elt) + # find the metadata edit region which should be setup server side, + # so that we can wire up posting back those changes + @metadata_elt = $(element).find('.metadata_edit') editUrl: -> "/edit_item?#{$.param(id: @get('id'))}" save: (args...) -> @set(data: JSON.stringify(@module.save())) if @module + # cdodge: package up metadata which is separated into a number of input fields + # there's probably a better way to do this, but at least this lets me continue to move onwards + if @metadata_elt + _metadata = {} + # walk through the set of elments which have the 'xmetadata_name' attribute and + # build up a object to pass back to the server on the subsequent POST + _metadata[el.getAttribute("xmetadata_name")]=el.value for el in $('[xmetadata_name]') + @set(metadata: _metadata) super(args...) diff --git a/cms/templates/widgets/html-edit.html b/cms/templates/widgets/html-edit.html index 1e86c6c734..9f7196b6e4 100644 --- a/cms/templates/widgets/html-edit.html +++ b/cms/templates/widgets/html-edit.html @@ -1,3 +1,4 @@ +<%include file="metadata-edit.html" />
diff --git a/cms/templates/widgets/metadata-edit.html b/cms/templates/widgets/metadata-edit.html new file mode 100644 index 0000000000..a130436e37 --- /dev/null +++ b/cms/templates/widgets/metadata-edit.html @@ -0,0 +1,8 @@ + diff --git a/cms/templates/widgets/raw-edit.html b/cms/templates/widgets/raw-edit.html index fbd1757be5..9488552be5 100644 --- a/cms/templates/widgets/raw-edit.html +++ b/cms/templates/widgets/raw-edit.html @@ -1,3 +1,4 @@ +<%include file="metadata-edit.html" />
diff --git a/common/lib/xmodule/xmodule/editing_module.py b/common/lib/xmodule/xmodule/editing_module.py index 67a4d66dad..c562617c98 100644 --- a/common/lib/xmodule/xmodule/editing_module.py +++ b/common/lib/xmodule/xmodule/editing_module.py @@ -21,6 +21,7 @@ class EditingDescriptor(MakoModuleDescriptor): return { 'module': self, 'data': self.definition.get('data', ''), + 'metadata': self.metadata # TODO (vshnayder): allow children and metadata to be edited. #'children' : self.definition.get('children, ''), From a7b7fd52d97786cbc08680975632b11d60d1fbe2 Mon Sep 17 00:00:00 2001 From: Chris Dodge Date: Fri, 21 Sep 2012 13:56:53 -0400 Subject: [PATCH 3/3] Support editing of metadata on Sequences --- cms/templates/widgets/metadata-edit.html | 2 ++ cms/templates/widgets/sequence-edit.html | 1 + common/lib/xmodule/xmodule/seq_module.py | 6 ++++-- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/cms/templates/widgets/metadata-edit.html b/cms/templates/widgets/metadata-edit.html index a130436e37..368959022a 100644 --- a/cms/templates/widgets/metadata-edit.html +++ b/cms/templates/widgets/metadata-edit.html @@ -1,3 +1,4 @@ +% if metadata: +% endif diff --git a/cms/templates/widgets/sequence-edit.html b/cms/templates/widgets/sequence-edit.html index d92ccbb7a7..dba6627bb2 100644 --- a/cms/templates/widgets/sequence-edit.html +++ b/cms/templates/widgets/sequence-edit.html @@ -29,6 +29,7 @@ + <%include file="metadata-edit.html" />
    diff --git a/common/lib/xmodule/xmodule/seq_module.py b/common/lib/xmodule/xmodule/seq_module.py index b05ea36e50..cc2bfe2bc1 100644 --- a/common/lib/xmodule/xmodule/seq_module.py +++ b/common/lib/xmodule/xmodule/seq_module.py @@ -9,6 +9,7 @@ from xmodule.x_module import XModule from xmodule.progress import Progress from xmodule.exceptions import NotFoundError from pkg_resources import resource_string +from .editing_module import EditingDescriptor log = logging.getLogger("mitx.common.lib.seq_module") @@ -94,7 +95,8 @@ class SequenceModule(XModule): 'element_id': self.location.html_id(), 'item_id': self.id, 'position': self.position, - 'tag': self.location.category} + 'tag': self.location.category + } self.content = self.system.render_template('seq_module.html', params) self.rendered = True @@ -109,7 +111,7 @@ class SequenceModule(XModule): return new_class -class SequenceDescriptor(MakoModuleDescriptor, XmlDescriptor): +class SequenceDescriptor(EditingDescriptor, XmlDescriptor): mako_template = 'widgets/sequence-edit.html' module_class = SequenceModule