Commit 53a40166 by cahrens

Utility method for converting string to bool.

parent a170c6f4
......@@ -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.
......
"""
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
"""
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))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment