From 37f1b7a075538b5adc731f4a5de07c72df70341b Mon Sep 17 00:00:00 2001 From: rayzhou-bit Date: Thu, 9 Jan 2025 17:56:25 -0500 Subject: [PATCH] fix: studio url evaluation --- cms/djangoapps/contentstore/tasks.py | 31 ++++++++++++++++++---------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/cms/djangoapps/contentstore/tasks.py b/cms/djangoapps/contentstore/tasks.py index b047f3618b6..50feb400bec 100644 --- a/cms/djangoapps/contentstore/tasks.py +++ b/cms/djangoapps/contentstore/tasks.py @@ -1127,9 +1127,17 @@ def get_urls(content): regex = r'\s+(?:href|src)=["\'](?!#)([^"\']*)["\']' url_list = re.findall(regex, content) return url_list - + def is_studio_url(url): """Returns True if url is a studio url.""" + return is_studio_url_with_base(url) or is_studio_url_without_base(url) + + def is_studio_url_with_base(url): + """Returns True if url is a studio url with cms base.""" + return url.startswith('http://' + settings.CMS_BASE) or url.startswith('https://' + settings.CMS_BASE) + + def is_studio_url_without_base(url): + """Returns True if url is a studio url without cms base.""" return not url.startswith('http://') and not url.startswith('https://') def convert_to_standard_url(url, course_key): @@ -1140,14 +1148,14 @@ def convert_to_standard_url(url, course_key): /static/getting-started_x250.png /container/block-v1:edX+DemoX+Demo_Course+type@vertical+block@2152d4a4aadc4cb0af5256394a3d1fc7 """ - if is_studio_url(url): + if is_studio_url_without_base(url): if url.startswith('/static/'): processed_url = replace_static_urls(f'\"{url}\"', course_id=course_key)[1:-1] - return 'http://' + settings.CMS_BASE + processed_url + return 'https://' + settings.CMS_BASE + processed_url elif url.startswith('/'): - return 'http://' + settings.CMS_BASE + url + return 'https://' + settings.CMS_BASE + url else: - return 'http://' + settings.CMS_BASE + '/container/' + url + return 'https://' + settings.CMS_BASE + '/container/' + url else: return url @@ -1238,14 +1246,15 @@ def filter_by_status(results): filtered_results = [] retry_list = [] for result in results: - if result['status'] is None: - retry_list.append([result['block_id'], result['url']]) - elif result['status'] == 200: + status, block_id, url = result['status'], result['block_id'], result['url'] + if status is None: + retry_list.append([block_id, url]) + elif status == 200: continue - elif result['status'] == 403 and is_studio_url(result['url']): - filtered_results.append([result['block_id'], result['url'], True]) + elif status == 403 and is_studio_url(url): + filtered_results.append([block_id, url, True]) else: - filtered_results.append([result['block_id'], result['url'], False]) + filtered_results.append([block_id, url, False]) return filtered_results, retry_list