From 45b82f3a451be468fd83fce1d8b9a32765d6a755 Mon Sep 17 00:00:00 2001 From: Chris Dodge Date: Thu, 4 Oct 2012 10:58:48 -0400 Subject: [PATCH] update creating a new unit to use an empty template. Also twiddle the units.html and base.js where we put the unit Location on the
  • element --- cms/djangoapps/contentstore/views.py | 39 ++++++++++++++++++- cms/static/js/base.js | 30 +++++++++++--- cms/templates/widgets/units.html | 4 +- cms/urls.py | 1 + common/lib/xmodule/xmodule/vertical_module.py | 4 ++ 5 files changed, 69 insertions(+), 9 deletions(-) diff --git a/cms/djangoapps/contentstore/views.py b/cms/djangoapps/contentstore/views.py index b6aeebd602..53923e5c6b 100644 --- a/cms/djangoapps/contentstore/views.py +++ b/cms/djangoapps/contentstore/views.py @@ -45,6 +45,8 @@ from auth.authz import get_user_by_email, add_user_to_course_group, remove_user_ from auth.authz import ADMIN_ROLE_NAME, EDITOR_ROLE_NAME from .utils import get_course_location_for_item +from xmodule.templates import all_templates + log = logging.getLogger(__name__) @@ -126,9 +128,14 @@ def course_index(request, org, course, name): course = modulestore().get_item(location) sections = course.get_children() + # This knowledge of what the 'new template' should be seems like it needs to be kept deeper down in the + # code. We should probably refactor + template = modulestore().get_item(Location('i4x', 'edx', 'templates', 'vertical', 'Empty')) + return render_to_response('overview.html', { 'sections': sections, - 'upload_asset_callback_url': upload_asset_callback_url + 'upload_asset_callback_url': upload_asset_callback_url, + 'create_new_unit_template': template.location }) @@ -144,8 +151,14 @@ def edit_subsection(request, location): if item.location.category != 'sequential': return HttpResponseBadRequest + # This knowledge of what the 'new template' should be seems like it needs to be kept deeper down in the + # code. We should probably refactor + template = modulestore().get_item(Location('i4x', 'edx', 'templates', 'vertical', 'Empty')) + return render_to_response('edit_subsection.html', - {'subsection':item}) + {'subsection':item, + 'create_new_unit_template' : template.location + }) @login_required def edit_unit(request, location): @@ -407,6 +420,20 @@ def delete_item(request): return HttpResponse() +@login_required +@expect_json +def create_item(request): + # parent_location should be the location of the parent container + parent_location = request.POST['parent_id'] + + # which type of item to create + category = request.POST['category'] + + # check permissions for this user within this course + if not has_access(request.user, parent_location): + raise PermissionDenied() + + @login_required @expect_json @@ -446,6 +473,10 @@ def save_item(request): def clone_item(request): parent_location = Location(request.POST['parent_location']) template = Location(request.POST['template']) + + display_name = None + if 'display_name' in request.POST: + display_name = request.POST['display_name'] if not has_access(request.user, parent_location): raise PermissionDenied() @@ -457,6 +488,10 @@ def clone_item(request): # TODO: This needs to be deleted when we have proper storage for static content new_item.metadata['data_dir'] = parent.metadata['data_dir'] + + # replace the display name with an optional parameter passed in from the caller + if display_name is not None: + new_item.metadata['display_name'] = display_name modulestore().update_metadata(new_item.location.url(), new_item.own_metadata) modulestore().update_children(parent_location, parent.definition.get('children', []) + [new_item.location.url()]) diff --git a/cms/static/js/base.js b/cms/static/js/base.js index 8d1af795b3..364cb61f3d 100644 --- a/cms/static/js/base.js +++ b/cms/static/js/base.js @@ -24,19 +24,39 @@ $(document).ready(function() { $('.assets .upload-button').bind('click', showUploadModal); $('.upload-modal .close-button').bind('click', hideModal); $('.unit .item-actions .delete-button').bind('click', deleteUnit); + $('.new-unit-item').bind('click', createNewUnit); }); +function createNewUnit(e) { + e.preventDefault(); + + parent = $(this).data('parent'); + template = $(this).data('template'); + + $.post('/clone_item', + {'parent_location' : parent, + 'template' : template, + 'display_name': 'New Unit', + }, + function(data) { + // redirect to the edit page + window.location = "/edit/" + data['id']; + }); +} + function deleteUnit(e) { e.preventDefault(); - var id = $(this).data('id'); - var _this = $(this); + + if(!confirm('Are you sure you wish to delete this item. It cannot be reversed!')) + return; + + var _li_el = $(this).parents('li.leaf'); + var id = _li_el.data('id'); $.post('/delete_item', {'id': id, 'delete_children' : 'true'}, function(data) { - // remove 'leaf' class containing
  • element - var parent = _this.parents('li.leaf'); - parent.remove(); + _li_el.remove(); }); } diff --git a/cms/templates/widgets/units.html b/cms/templates/widgets/units.html index 2605277e71..9d9c7943da 100644 --- a/cms/templates/widgets/units.html +++ b/cms/templates/widgets/units.html @@ -6,7 +6,7 @@ This def will enumerate through a passed in subsection and list all of the units <%def name="enum_units(subsection)">
      % for unit in subsection.get_children(): -
    1. +
    2. % endfor
    3. - + New Unit
    4. diff --git a/cms/urls.py b/cms/urls.py index ae22220511..f57f8d91d2 100644 --- a/cms/urls.py +++ b/cms/urls.py @@ -13,6 +13,7 @@ urlpatterns = ('', url(r'^subsection/(?P.*?)$', 'contentstore.views.edit_subsection', name='edit_subsection'), url(r'^preview_component/(?P.*?)$', 'contentstore.views.preview_component', name='preview_component'), url(r'^save_item$', 'contentstore.views.save_item', name='save_item'), + url(r'^create_item$', 'contentstore.views.create_item', name='create_item'), url(r'^delete_item$', 'contentstore.views.delete_item', name='delete_item'), url(r'^clone_item$', 'contentstore.views.clone_item', name='clone_item'), url(r'^(?P[^/]+)/(?P[^/]+)/course/(?P[^/]+)$', diff --git a/common/lib/xmodule/xmodule/vertical_module.py b/common/lib/xmodule/xmodule/vertical_module.py index f0c26e045f..738ee9ac5f 100644 --- a/common/lib/xmodule/xmodule/vertical_module.py +++ b/common/lib/xmodule/xmodule/vertical_module.py @@ -45,5 +45,9 @@ class VerticalModule(XModule): class VerticalDescriptor(SequenceDescriptor): module_class = VerticalModule + # cdodge: override the SequenceDescript's template_dir_name to point to default template directory + template_dir_name = "default" + js = {'coffee': [resource_string(__name__, 'js/src/vertical/edit.coffee')]} js_module_name = "VerticalDescriptor" +