Skip to content

Commit

Permalink
feat: TNL-11812 reinstate file update methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernard Szabo committed Jan 24, 2025
1 parent f807eb9 commit de1b99f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
39 changes: 32 additions & 7 deletions cms/djangoapps/contentstore/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1226,16 +1226,21 @@ def _retry_validation(url_list, course_key, retry_count=3):
for i in range(0, retry_count):
if retry_list:
LOGGER.debug(f'[Link Check] retry attempt #{i + 1}')
validated_url_list = asyncio.run(
_validate_urls_access_in_batches(retry_list, course_key, batch_size=100)
)
filetered_url_list, retry_list = _filter_by_status(validated_url_list)
results.extend(filetered_url_list)

retry_list = _retry_validation_and_filter(course_key, results, retry_list)
results.extend(retry_list)

return results


def _retry_validation_and_filter(course_key, results, retry_list):
validated_url_list = asyncio.run(
_validate_urls_access_in_batches(retry_list, course_key, batch_size=100)
)
filtered_url_list, retry_list = _filter_by_status(validated_url_list)
results.extend(filtered_url_list)
return retry_list


def _filter_by_status(results):
"""
Filter results by status.
Expand All @@ -1262,6 +1267,15 @@ def _filter_by_status(results):

return filtered_results, retry_list

def _save_broken_links_file(artifact, file_to_save):
artifact.file.save(name=os.path.basename(file_to_save.name), content=File(file_to_save))
artifact.save()
return True

def _write_broken_links_to_file(broken_or_locked_urls, broken_links_file):
with open(broken_links_file.name, 'w') as file:
json.dump(broken_or_locked_urls, file, indent=4)

def _check_broken_links(task_instance, user_id, course_key_string, language):
"""
Checks for broken links in a course. Store the results in a file.
Expand All @@ -1281,7 +1295,18 @@ def _check_broken_links(task_instance, user_id, course_key_string, language):

try:
task_instance.status.increment_completed_steps()
_record_broken_links(task_instance, broken_or_locked_urls, course_key)

file_name = str(course_key)
broken_links_file = NamedTemporaryFile(prefix=file_name + '.', suffix='.json')
LOGGER.debug(f'[Link Check] json file being generated at {broken_links_file.name}')

with open(broken_links_file.name, 'w') as file:
json.dump(broken_or_locked_urls, file, indent=4)

_write_broken_links_to_file(broken_or_locked_urls, broken_links_file)

artifact = UserTaskArtifact(status=task_instance.status, name='BrokenLinks')
_save_broken_links_file(artifact, broken_links_file)

# catch all exceptions so we can record useful error messages
except Exception as e: # pylint: disable=broad-except
Expand Down
4 changes: 1 addition & 3 deletions cms/djangoapps/contentstore/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
from openedx.core.djangoapps.embargo.models import Country, CountryAccessRule, RestrictedCourse
from xmodule.modulestore import ModuleStoreEnum
from xmodule.modulestore.django import modulestore # lint-amnesty, pylint: disable=wrong-import-order
from xmodule.modulestore.tests.django_utils import TEST_DATA_SPLIT_MODULESTORE
from xmodule.modulestore.tests.django_utils import TEST_DATA_SPLIT_MODULESTORE, ModuleStoreTestCase
from xmodule.modulestore.tests.factories import CourseFactory # lint-amnesty, pylint: disable=wrong-import-order
from celery import Task

Expand Down Expand Up @@ -244,7 +244,6 @@ def setUp(self):
]

@mock.patch('cms.djangoapps.contentstore.tasks.UserTaskArtifact', autospec=True)
@mock.patch('cms.djangoapps.contentstore.tasks.UserTaskStatus', autospec=True)
@mock.patch('cms.djangoapps.contentstore.tasks._scan_course_for_links')
@mock.patch('cms.djangoapps.contentstore.tasks._save_broken_links_file', autospec=True)
@mock.patch('cms.djangoapps.contentstore.tasks._write_broken_links_to_file', autospec=True)
Expand All @@ -253,7 +252,6 @@ def test_check_broken_links_stores_broken_and_locked_urls(
mock_write_broken_links_to_file,
mock_save_broken_links_file,
mock_scan_course_for_links,
_mock_user_task_status,
mock_user_task_artifact
):
'''
Expand Down

0 comments on commit de1b99f

Please sign in to comment.