From 97f1e042827a8b1f80d35bd5781fe66fb9420904 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Fri, 1 Mar 2013 17:31:26 -0500 Subject: [PATCH] Fix some 2-space indents --- .../management/commands/delete_course.py | 21 +- .../xmodule/modulestore/store_utilities.py | 198 +++++++++--------- .../xmodule/modulestore/xml_exporter.py | 64 +++--- .../django_comment_client/forum/views.py | 55 +++-- 4 files changed, 167 insertions(+), 171 deletions(-) diff --git a/cms/djangoapps/contentstore/management/commands/delete_course.py b/cms/djangoapps/contentstore/management/commands/delete_course.py index e3cac7de02..789226db1a 100644 --- a/cms/djangoapps/contentstore/management/commands/delete_course.py +++ b/cms/djangoapps/contentstore/management/commands/delete_course.py @@ -17,8 +17,7 @@ from auth.authz import _delete_course_group class Command(BaseCommand): - help = \ -'''Delete a MongoDB backed course''' + help = '''Delete a MongoDB backed course''' def handle(self, *args, **options): if len(args) != 1 and len(args) != 2: @@ -28,19 +27,19 @@ class Command(BaseCommand): commit = False if len(args) == 2: - commit = args[1] == 'commit' + commit = args[1] == 'commit' if commit: - print 'Actually going to delete the course from DB....' + print 'Actually going to delete the course from DB....' ms = modulestore('direct') cs = contentstore() if query_yes_no("Deleting course {0}. Confirm?".format(loc_str), default="no"): - if query_yes_no("Are you sure. This action cannot be undone!", default="no"): - loc = CourseDescriptor.id_to_location(loc_str) - if delete_course(ms, cs, loc, commit) == True: - print 'removing User permissions from course....' - # in the django layer, we need to remove all the user permissions groups associated with this course - if commit: - _delete_course_group(loc) + if query_yes_no("Are you sure. This action cannot be undone!", default="no"): + loc = CourseDescriptor.id_to_location(loc_str) + if delete_course(ms, cs, loc, commit) == True: + print 'removing User permissions from course....' + # in the django layer, we need to remove all the user permissions groups associated with this course + if commit: + _delete_course_group(loc) diff --git a/common/lib/xmodule/xmodule/modulestore/store_utilities.py b/common/lib/xmodule/xmodule/modulestore/store_utilities.py index f08fdd639a..5146ac18c8 100644 --- a/common/lib/xmodule/xmodule/modulestore/store_utilities.py +++ b/common/lib/xmodule/xmodule/modulestore/store_utilities.py @@ -5,132 +5,132 @@ from xmodule.modulestore.mongo import MongoModuleStore def clone_course(modulestore, contentstore, source_location, dest_location, delete_original=False): - # first check to see if the modulestore is Mongo backed - if not isinstance(modulestore, MongoModuleStore): - raise Exception("Expected a MongoModuleStore in the runtime. Aborting....") + # first check to see if the modulestore is Mongo backed + if not isinstance(modulestore, MongoModuleStore): + raise Exception("Expected a MongoModuleStore in the runtime. Aborting....") - # check to see if the dest_location exists as an empty course - # we need an empty course because the app layers manage the permissions and users - if not modulestore.has_item(dest_location): - raise Exception("An empty course at {0} must have already been created. Aborting...".format(dest_location)) + # check to see if the dest_location exists as an empty course + # we need an empty course because the app layers manage the permissions and users + if not modulestore.has_item(dest_location): + raise Exception("An empty course at {0} must have already been created. Aborting...".format(dest_location)) - # verify that the dest_location really is an empty course, which means only one - dest_modules = modulestore.get_items([dest_location.tag, dest_location.org, dest_location.course, None, None, None]) + # verify that the dest_location really is an empty course, which means only one + dest_modules = modulestore.get_items([dest_location.tag, dest_location.org, dest_location.course, None, None, None]) - if len(dest_modules) != 1: - raise Exception("Course at destination {0} is not an empty course. You can only clone into an empty course. Aborting...".format(dest_location)) + if len(dest_modules) != 1: + raise Exception("Course at destination {0} is not an empty course. You can only clone into an empty course. Aborting...".format(dest_location)) - # check to see if the source course is actually there - if not modulestore.has_item(source_location): - raise Exception("Cannot find a course at {0}. Aborting".format(source_location)) + # check to see if the source course is actually there + if not modulestore.has_item(source_location): + raise Exception("Cannot find a course at {0}. Aborting".format(source_location)) - # Get all modules under this namespace which is (tag, org, course) tuple + # Get all modules under this namespace which is (tag, org, course) tuple - modules = modulestore.get_items([source_location.tag, source_location.org, source_location.course, None, None, None]) + modules = modulestore.get_items([source_location.tag, source_location.org, source_location.course, None, None, None]) - for module in modules: - original_loc = Location(module.location) + for module in modules: + original_loc = Location(module.location) - if original_loc.category != 'course': - module.location = module.location._replace(tag=dest_location.tag, org=dest_location.org, - course=dest_location.course) - else: - # on the course module we also have to update the module name - module.location = module.location._replace(tag=dest_location.tag, org=dest_location.org, - course=dest_location.course, name=dest_location.name) + if original_loc.category != 'course': + module.location = module.location._replace(tag=dest_location.tag, org=dest_location.org, + course=dest_location.course) + else: + # on the course module we also have to update the module name + module.location = module.location._replace(tag=dest_location.tag, org=dest_location.org, + course=dest_location.course, name=dest_location.name) - print "Cloning module {0} to {1}....".format(original_loc, module.location) + print "Cloning module {0} to {1}....".format(original_loc, module.location) - if 'data' in module.definition: - modulestore.update_item(module.location, module.definition['data']) + if 'data' in module.definition: + modulestore.update_item(module.location, module.definition['data']) - # repoint children - if 'children' in module.definition: - new_children = [] - for child_loc_url in module.definition['children']: - child_loc = Location(child_loc_url) - child_loc = child_loc._replace(tag=dest_location.tag, org=dest_location.org, - course=dest_location.course) - new_children = new_children + [child_loc.url()] + # repoint children + if 'children' in module.definition: + new_children = [] + for child_loc_url in module.definition['children']: + child_loc = Location(child_loc_url) + child_loc = child_loc._replace(tag=dest_location.tag, org=dest_location.org, + course=dest_location.course) + new_children = new_children + [child_loc.url()] - modulestore.update_children(module.location, new_children) + modulestore.update_children(module.location, new_children) - # save metadata - modulestore.update_metadata(module.location, module.metadata) + # save metadata + modulestore.update_metadata(module.location, module.metadata) - # now iterate through all of the assets and clone them - # first the thumbnails - thumbs = contentstore.get_all_content_thumbnails_for_course(source_location) - for thumb in thumbs: - thumb_loc = Location(thumb["_id"]) - content = contentstore.find(thumb_loc) - content.location = content.location._replace(org=dest_location.org, - course=dest_location.course) + # now iterate through all of the assets and clone them + # first the thumbnails + thumbs = contentstore.get_all_content_thumbnails_for_course(source_location) + for thumb in thumbs: + thumb_loc = Location(thumb["_id"]) + content = contentstore.find(thumb_loc) + content.location = content.location._replace(org=dest_location.org, + course=dest_location.course) - print "Cloning thumbnail {0} to {1}".format(thumb_loc, content.location) + print "Cloning thumbnail {0} to {1}".format(thumb_loc, content.location) - contentstore.save(content) + contentstore.save(content) - # now iterate through all of the assets, also updating the thumbnail pointer + # now iterate through all of the assets, also updating the thumbnail pointer - assets = contentstore.get_all_content_for_course(source_location) - for asset in assets: - asset_loc = Location(asset["_id"]) - content = contentstore.find(asset_loc) - content.location = content.location._replace(org=dest_location.org, - course=dest_location.course) + assets = contentstore.get_all_content_for_course(source_location) + for asset in assets: + asset_loc = Location(asset["_id"]) + content = contentstore.find(asset_loc) + content.location = content.location._replace(org=dest_location.org, + course=dest_location.course) - # be sure to update the pointer to the thumbnail - if content.thumbnail_location is not None: - content.thumbnail_location = content.thumbnail_location._replace(org=dest_location.org, - course=dest_location.course) + # be sure to update the pointer to the thumbnail + if content.thumbnail_location is not None: + content.thumbnail_location = content.thumbnail_location._replace(org=dest_location.org, + course=dest_location.course) - print "Cloning asset {0} to {1}".format(asset_loc, content.location) + print "Cloning asset {0} to {1}".format(asset_loc, content.location) - contentstore.save(content) + contentstore.save(content) - return True + return True def delete_course(modulestore, contentstore, source_location, commit = False): # first check to see if the modulestore is Mongo backed - if not isinstance(modulestore, MongoModuleStore): - raise Exception("Expected a MongoModuleStore in the runtime. Aborting....") + if not isinstance(modulestore, MongoModuleStore): + raise Exception("Expected a MongoModuleStore in the runtime. Aborting....") - # check to see if the source course is actually there - if not modulestore.has_item(source_location): - raise Exception("Cannot find a course at {0}. Aborting".format(source_location)) + # check to see if the source course is actually there + if not modulestore.has_item(source_location): + raise Exception("Cannot find a course at {0}. Aborting".format(source_location)) - # first delete all of the thumbnails - thumbs = contentstore.get_all_content_thumbnails_for_course(source_location) - for thumb in thumbs: - thumb_loc = Location(thumb["_id"]) - id = StaticContent.get_id_from_location(thumb_loc) - print "Deleting {0}...".format(id) + # first delete all of the thumbnails + thumbs = contentstore.get_all_content_thumbnails_for_course(source_location) + for thumb in thumbs: + thumb_loc = Location(thumb["_id"]) + id = StaticContent.get_id_from_location(thumb_loc) + print "Deleting {0}...".format(id) + if commit: + contentstore.delete(id) + + # then delete all of the assets + assets = contentstore.get_all_content_for_course(source_location) + for asset in assets: + asset_loc = Location(asset["_id"]) + id = StaticContent.get_id_from_location(asset_loc) + print "Deleting {0}...".format(id) + if commit: + contentstore.delete(id) + + # then delete all course modules + modules = modulestore.get_items([source_location.tag, source_location.org, source_location.course, None, None, None]) + + for module in modules: + if module.category != 'course': # save deleting the course module for last + print "Deleting {0}...".format(module.location) + if commit: + modulestore.delete_item(module.location) + + # finally delete the top-level course module itself + print "Deleting {0}...".format(source_location) if commit: - contentstore.delete(id) + modulestore.delete_item(source_location) - # then delete all of the assets - assets = contentstore.get_all_content_for_course(source_location) - for asset in assets: - asset_loc = Location(asset["_id"]) - id = StaticContent.get_id_from_location(asset_loc) - print "Deleting {0}...".format(id) - if commit: - contentstore.delete(id) - - # then delete all course modules - modules = modulestore.get_items([source_location.tag, source_location.org, source_location.course, None, None, None]) - - for module in modules: - if module.category != 'course': # save deleting the course module for last - print "Deleting {0}...".format(module.location) - if commit: - modulestore.delete_item(module.location) - - # finally delete the top-level course module itself - print "Deleting {0}...".format(source_location) - if commit: - modulestore.delete_item(source_location) - - return True + return True diff --git a/common/lib/xmodule/xmodule/modulestore/xml_exporter.py b/common/lib/xmodule/xmodule/modulestore/xml_exporter.py index 55844116c6..edf4708687 100644 --- a/common/lib/xmodule/xmodule/modulestore/xml_exporter.py +++ b/common/lib/xmodule/xmodule/modulestore/xml_exporter.py @@ -7,47 +7,47 @@ from json import dumps def export_to_xml(modulestore, contentstore, course_location, root_dir, course_dir): - course = modulestore.get_item(course_location) + course = modulestore.get_item(course_location) - fs = OSFS(root_dir) - export_fs = fs.makeopendir(course_dir) + fs = OSFS(root_dir) + export_fs = fs.makeopendir(course_dir) - xml = course.export_to_xml(export_fs) - with export_fs.open('course.xml', 'w') as course_xml: - course_xml.write(xml) + xml = course.export_to_xml(export_fs) + with export_fs.open('course.xml', 'w') as course_xml: + course_xml.write(xml) - # export the static assets - contentstore.export_all_for_course(course_location, root_dir + '/' + course_dir + '/static/') + # export the static assets + contentstore.export_all_for_course(course_location, root_dir + '/' + course_dir + '/static/') - # export the static tabs - export_extra_content(export_fs, modulestore, course_location, 'static_tab', 'tabs', '.html') + # export the static tabs + export_extra_content(export_fs, modulestore, course_location, 'static_tab', 'tabs', '.html') - # export the custom tags - export_extra_content(export_fs, modulestore, course_location, 'custom_tag_template', 'custom_tags') + # export the custom tags + export_extra_content(export_fs, modulestore, course_location, 'custom_tag_template', 'custom_tags') - # export the course updates - export_extra_content(export_fs, modulestore, course_location, 'course_info', 'info', '.html') + # export the course updates + export_extra_content(export_fs, modulestore, course_location, 'course_info', 'info', '.html') - # export the grading policy - policies_dir = export_fs.makeopendir('policies') - course_run_policy_dir = policies_dir.makeopendir(course.location.name) - if 'grading_policy' in course.definition['data']: - with course_run_policy_dir.open('grading_policy.json', 'w') as grading_policy: - grading_policy.write(dumps(course.definition['data']['grading_policy'])) + # export the grading policy + policies_dir = export_fs.makeopendir('policies') + course_run_policy_dir = policies_dir.makeopendir(course.location.name) + if 'grading_policy' in course.definition['data']: + with course_run_policy_dir.open('grading_policy.json', 'w') as grading_policy: + grading_policy.write(dumps(course.definition['data']['grading_policy'])) - # export all of the course metadata in policy.json - with course_run_policy_dir.open('policy.json', 'w') as course_policy: - policy = {} - policy = {'course/' + course.location.name: course.metadata} - course_policy.write(dumps(policy)) + # export all of the course metadata in policy.json + with course_run_policy_dir.open('policy.json', 'w') as course_policy: + policy = {} + policy = {'course/' + course.location.name: course.metadata} + course_policy.write(dumps(policy)) def export_extra_content(export_fs, modulestore, course_location, category_type, dirname, file_suffix=''): - query_loc = Location('i4x', course_location.org, course_location.course, category_type, None) - items = modulestore.get_items(query_loc) + query_loc = Location('i4x', course_location.org, course_location.course, category_type, None) + items = modulestore.get_items(query_loc) - if len(items) > 0: - item_dir = export_fs.makeopendir(dirname) - for item in items: - with item_dir.open(item.location.name + file_suffix, 'w') as item_file: - item_file.write(item.definition['data'].encode('utf8')) + if len(items) > 0: + item_dir = export_fs.makeopendir(dirname) + for item in items: + with item_dir.open(item.location.name + file_suffix, 'w') as item_file: + item_file.write(item.definition['data'].encode('utf8')) diff --git a/lms/djangoapps/django_comment_client/forum/views.py b/lms/djangoapps/django_comment_client/forum/views.py index d4a6d0cc38..a3120c563a 100644 --- a/lms/djangoapps/django_comment_client/forum/views.py +++ b/lms/djangoapps/django_comment_client/forum/views.py @@ -11,7 +11,7 @@ from django.contrib.auth.models import User from mitxmako.shortcuts import render_to_response, render_to_string from courseware.courses import get_course_with_access -from course_groups.cohorts import is_course_cohorted, get_cohort_id, is_commentable_cohorted, get_cohorted_commentables, get_cohort, get_course_cohorts, get_cohort_by_id +from course_groups.cohorts import is_course_cohorted, get_cohort_id, is_commentable_cohorted, get_cohorted_commentables, get_cohort, get_course_cohorts, get_cohort_by_id from courseware.access import has_access from urllib import urlencode @@ -64,24 +64,23 @@ def get_threads(request, course_id, discussion_id=None, per_page=THREADS_PER_PAG #there are 2 dimensions to consider when executing a search with respect to group id #is user a moderator #did the user request a group - + #if the user requested a group explicitly, give them that group, othewrise, if mod, show all, else if student, use cohort - + group_id = request.GET.get('group_id') - + if group_id == "all": group_id = None - + if not group_id: if not cached_has_permission(request.user, "see_all_cohorts", course_id): group_id = get_cohort_id(request.user, course_id) - + if group_id: default_query_params["group_id"] = group_id - #so by default, a moderator sees all items, and a student sees his cohort - + query_params = merge_dict(default_query_params, strip_none(extract(request.GET, ['page', 'sort_key', @@ -89,7 +88,7 @@ def get_threads(request, course_id, discussion_id=None, per_page=THREADS_PER_PAG 'tags', 'commentable_ids']))) threads, page, num_pages = cc.Thread.search(query_params) - + #now add the group name if the thread has a group id for thread in threads: if thread.get('group_id'): @@ -106,7 +105,6 @@ def get_threads(request, course_id, discussion_id=None, per_page=THREADS_PER_PAG def inline_discussion(request, course_id, discussion_id): - """ Renders JSON for DiscussionModules """ @@ -128,7 +126,7 @@ def inline_discussion(request, course_id, discussion_id): allow_anonymous = course.metadata.get("allow_anonymous", True) allow_anonymous_to_peers = course.metadata.get("allow_anonymous_to_peers", False) - + #since inline is all one commentable, only show or allow the choice of cohorts #if the commentable is cohorted, otherwise everything is not cohorted #and no one has the option of choosing a cohort @@ -138,18 +136,18 @@ def inline_discussion(request, course_id, discussion_id): cohorts_list = list() if is_cohorted: - cohorts_list.append({'name':'All Groups','id':None}) - - #if you're a mod, send all cohorts and let you pick - - if is_moderator: - cohorts = get_course_cohorts(course_id) - for c in cohorts: - cohorts_list.append({'name':c.name, 'id':c.id}) - - else: - #students don't get to choose - cohorts_list = None + cohorts_list.append({'name':'All Groups','id':None}) + + #if you're a mod, send all cohorts and let you pick + + if is_moderator: + cohorts = get_course_cohorts(course_id) + for c in cohorts: + cohorts_list.append({'name':c.name, 'id':c.id}) + + else: + #students don't get to choose + cohorts_list = None return utils.JsonResponse({ 'discussion_data': map(utils.safe_content, threads), @@ -168,7 +166,6 @@ def inline_discussion(request, course_id, discussion_id): @login_required def forum_form_discussion(request, course_id): - """ Renders the main Discussion page, potentially filtered by a search query """ @@ -210,7 +207,7 @@ def forum_form_discussion(request, course_id): #) cohorts = get_course_cohorts(course_id) cohorted_commentables = get_cohorted_commentables(course_id) - + user_cohort_id = get_cohort_id(request.user, course_id) context = { @@ -233,7 +230,7 @@ def forum_form_discussion(request, course_id): 'is_course_cohorted': is_course_cohorted(course_id) } # print "start rendering.." - + return render_to_response('discussion/index.html', context) @login_required @@ -251,7 +248,7 @@ def single_thread(request, course_id, discussion_id, thread_id): if request.is_ajax(): courseware_context = get_courseware_context(thread, course) - + annotated_content_info = utils.get_annotated_content_infos(course_id, thread, request.user, user_info=user_info) context = {'thread': thread.to_dict(), 'course_id': course_id} # TODO: Remove completely or switch back to server side rendering @@ -282,7 +279,7 @@ def single_thread(request, course_id, discussion_id, thread_id): if courseware_context: thread.update(courseware_context) if thread.get('group_id') and not thread.get('group_name'): - thread['group_name'] = get_cohort_by_id(course_id, thread.get('group_id')).name + thread['group_name'] = get_cohort_by_id(course_id, thread.get('group_id')).name threads = [utils.safe_content(thread) for thread in threads] @@ -297,7 +294,7 @@ def single_thread(request, course_id, discussion_id, thread_id): #) annotated_content_info = utils.get_metadata_for_threads(course_id, threads, request.user, user_info) - + cohorts = get_course_cohorts(course_id) cohorted_commentables = get_cohorted_commentables(course_id) user_cohort = get_cohort_id(request.user, course_id)