diff --git a/cms/djangoapps/contentstore/views/item.py b/cms/djangoapps/contentstore/views/item.py index a91ec4fad8..cd3a68cb24 100644 --- a/cms/djangoapps/contentstore/views/item.py +++ b/cms/djangoapps/contentstore/views/item.py @@ -12,6 +12,7 @@ from xmodule.modulestore.inheritance import own_metadata from xmodule.modulestore.exceptions import ItemNotFoundError, InvalidLocationError from util.json_request import expect_json, JsonResponse +from util.string_utils import str_to_bool from ..transcripts_utils import manage_video_subtitles_save @@ -74,12 +75,12 @@ def xblock_handler(request, tag=None, course_id=None, branch=None, version_guid= old_location = loc_mapper().translate_locator_to_location(location) if request.method == 'GET': - rewrite_static_links = request.GET.get('rewrite_url_links', 'True') in ['True', 'true'] + rewrite_static_links = str_to_bool(request.GET.get('rewrite_url_links', 'True')) rsp = _get_module_info(location, rewrite_static_links=rewrite_static_links) return JsonResponse(rsp) elif request.method == 'DELETE': - delete_children = bool(request.REQUEST.get('recurse', False)) - delete_all_versions = bool(request.REQUEST.get('all_versions', False)) + delete_children = str_to_bool(request.REQUEST.get('recurse', False)) + delete_all_versions = str_to_bool(request.REQUEST.get('all_versions', False)) return _delete_item_at_location(old_location, delete_children, delete_all_versions) else: # Since we have a course_id, we are updating an existing xblock. diff --git a/common/djangoapps/util/string_utils.py b/common/djangoapps/util/string_utils.py new file mode 100644 index 0000000000..1fd7a8b646 --- /dev/null +++ b/common/djangoapps/util/string_utils.py @@ -0,0 +1,15 @@ +""" +Utilities for string manipulation. +""" + +import ast + +def str_to_bool(str): + """ + Converts "true" (case-insensitive) to the boolean True. + Everything else will return False. + """ + try: + return ast.literal_eval(str.title()) + except: + return False diff --git a/common/djangoapps/util/tests/test_string_utils.py b/common/djangoapps/util/tests/test_string_utils.py new file mode 100644 index 0000000000..542ea562c1 --- /dev/null +++ b/common/djangoapps/util/tests/test_string_utils.py @@ -0,0 +1,26 @@ +""" +Tests for string_utils.py +""" + +from django.test import TestCase +from util.string_utils import str_to_bool + +class StringUtilsTest(TestCase): + """ + Tests for str_to_bool. + """ + def test_str_to_bool_true(self): + self.assertTrue(str_to_bool('True')) + self.assertTrue(str_to_bool('true')) + self.assertTrue(str_to_bool('trUe')) + + def test_str_to_bool_false(self): + self.assertFalse(str_to_bool('Tru')) + self.assertFalse(str_to_bool('False')) + self.assertFalse(str_to_bool('false')) + self.assertFalse(str_to_bool('')) + self.assertFalse(str_to_bool(None)) + self.assertFalse(str_to_bool('anything')) + self.assertFalse(str_to_bool([])) + self.assertFalse(str_to_bool({})) + self.assertFalse(str_to_bool(1))