Skip to content

Commit 53270f1

Browse files
committed
squash!: Remove use case for getting latest existing backup, now we always need to specify the task_id
1 parent db24393 commit 53270f1

4 files changed

Lines changed: 16 additions & 49 deletions

File tree

openedx/core/djangoapps/content_libraries/api/libraries.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -692,9 +692,8 @@ def revert_changes(library_key: LibraryLocatorV2, user_id: int | None = None) ->
692692

693693

694694
def get_backup_task_status(
695-
library_key: LibraryLocatorV2,
696695
user_id: int,
697-
task_id: str | None = None
696+
task_id: str
698697
) -> dict | None:
699698
"""
700699
Get the status of a library backup task.
@@ -711,12 +710,6 @@ def get_backup_task_status(
711710
task_status = UserTaskStatus.objects.get(task_id=task_id, user_id=user_id)
712711
except UserTaskStatus.DoesNotExist:
713712
return None
714-
else:
715-
# Get the latest task for this user and library
716-
task_status = UserTaskStatus.objects.filter(
717-
user_id=user_id,
718-
name__contains=str(library_key)
719-
).order_by('-created').first()
720713

721714
if not task_status:
722715
return None

openedx/core/djangoapps/content_libraries/rest_api/libraries.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -719,10 +719,9 @@ class LibraryBackupView(APIView):
719719
720720
**GET Parameters**
721721
722-
A GET request can include the following parameters:
722+
A GET request must include the following parameters:
723723
724-
* task_id: (optional) The UUID of the task to check. If not provided,
725-
the latest task for the library created by the calling user will be returned.
724+
* task_id: (required) The UUID of the task to check.
726725
727726
**GET Response Values**
728727
@@ -766,22 +765,24 @@ def post(self, request, lib_key_str):
766765
apidocs.query_parameter(
767766
'task_id',
768767
str,
769-
description="The ID of the backup task to retrieve. If not specified, retrieves the latest task."
768+
description="The ID of the backup task to retrieve."
770769
),
771770
],
772771
responses={200: LibraryBackupTaskStatusSerializer}
773772
)
774773
@convert_exceptions
775774
def get(self, request, lib_key_str):
776775
"""
777-
Get the status of the specified or latest backup task for the specified library.
776+
Get the status of the specified backup task for the specified library.
778777
"""
779778
library_key = LibraryLocatorV2.from_string(lib_key_str)
780779
# Using CAN_EDIT_THIS_CONTENT_LIBRARY permission for now. This should eventually become its own permission
781780
api.require_permission_for_library_key(library_key, request.user, permissions.CAN_EDIT_THIS_CONTENT_LIBRARY)
782781

783782
task_id = request.query_params.get('task_id', None)
784-
result = get_backup_task_status(library_key, request.user.id, task_id)
783+
if not task_id:
784+
raise ValidationError(detail={'task_id': _('This field is required.')})
785+
result = get_backup_task_status(request.user.id, task_id)
785786

786787
if not result:
787788
raise NotFound(detail="No backup found for this library.")

openedx/core/djangoapps/content_libraries/tests/base.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
URL_LIB_TEAM_USER = URL_LIB_TEAM + 'user/{username}/' # Add/edit/remove a user's permission to use this library
3333
URL_LIB_TEAM_GROUP = URL_LIB_TEAM + 'group/{group_name}/' # Add/edit/remove a group's permission to use this library
3434
URL_LIB_PASTE_CLIPBOARD = URL_LIB_DETAIL + 'paste_clipboard/' # Paste user clipboard (POST) containing Xblock data
35-
URL_LIB_BACKUP = URL_LIB_DETAIL + 'backup/' # Start or get status on a backup task for this library
35+
URL_LIB_BACKUP = URL_LIB_DETAIL + 'backup/' # Start a backup task for this library
36+
URL_LIB_BACKUP_GET = URL_LIB_BACKUP + '?{query_params}' # Get status on a backup task for this library
3637
URL_LIB_BLOCK = URL_PREFIX + 'blocks/{block_key}/' # Get data about a block, or delete it
3738
URL_LIB_BLOCK_PUBLISH = URL_LIB_BLOCK + 'publish/' # Publish changes from a specified XBlock
3839
URL_LIB_BLOCK_OLX = URL_LIB_BLOCK + 'olx/' # Get or set the OLX of the specified XBlock
@@ -328,7 +329,7 @@ def _start_library_backup_task(self, lib_key, expect_response=200):
328329
def _get_library_backup_task(self, lib_key, task_id, expect_response=200):
329330
""" Get the status of a backup task for this library """
330331
query_params = urlencode({"task_id": task_id})
331-
url = URL_LIB_BACKUP.format(lib_key=lib_key, query_params=query_params)
332+
url = URL_LIB_BACKUP_GET.format(lib_key=lib_key, query_params=query_params)
332333
return self._api('get', url, None, expect_response)
333334

334335
def _render_block_view(self, block_key, view_name, version=None, expect_response=200):

openedx/core/djangoapps/content_libraries/tests/test_api.py

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,11 +1329,11 @@ def setUp(self) -> None:
13291329
self.wrong_task_id = '11111111-1111-1111-1111-111111111111'
13301330

13311331
def test_get_backup_task_status_no_task(self) -> None:
1332-
status = api.get_backup_task_status(self.lib1.library_key, self.user.id)
1332+
status = api.get_backup_task_status(self.user.id, None)
13331333
assert status is None
13341334

13351335
def test_get_backup_task_status_wrong_task_id(self) -> None:
1336-
status = api.get_backup_task_status(self.lib1.library_key, self.user.id, task_id=self.wrong_task_id)
1336+
status = api.get_backup_task_status(self.user.id, task_id=self.wrong_task_id)
13371337
assert status is None
13381338

13391339
def test_get_backup_task_status_in_progress(self) -> None:
@@ -1351,7 +1351,7 @@ def test_get_backup_task_status_in_progress(self) -> None:
13511351
) as mock_get:
13521352
mock_get.return_value = mock_task
13531353

1354-
status = api.get_backup_task_status(self.lib1.library_key, self.user.id, task_id=task_id)
1354+
status = api.get_backup_task_status(self.user.id, task_id=task_id)
13551355
assert status is not None
13561356
assert status['state'] == UserTaskStatus.IN_PROGRESS
13571357
assert status['url'] is None
@@ -1379,39 +1379,11 @@ def test_get_backup_task_status_succeeded(self) -> None:
13791379
mock_get.return_value = mock_task
13801380
mock_artifact_get.return_value = mock_artifact
13811381

1382-
status = api.get_backup_task_status(self.lib1.library_key, self.user.id, task_id=task_id)
1382+
status = api.get_backup_task_status(self.user.id, task_id=task_id)
13831383
assert status is not None
13841384
assert status['state'] == UserTaskStatus.SUCCEEDED
13851385
assert status['url'] == "/media/user_tasks/2025/10/01/library-libOEXCSPROB_mOw1rPL.zip"
13861386

1387-
def test_get_backup_task_status_latest_task(self) -> None:
1388-
# Test getting the latest task when no task_id is provided
1389-
mock_task = UserTaskStatus(
1390-
task_id=str(uuid.uuid4()),
1391-
user_id=self.user.id,
1392-
name=f"Export of {self.lib1.library_key}",
1393-
state=UserTaskStatus.PENDING
1394-
)
1395-
1396-
mock_queryset = mock.Mock()
1397-
mock_queryset.order_by.return_value.first.return_value = mock_task
1398-
1399-
with mock.patch(
1400-
'openedx.core.djangoapps.content_libraries.api.libraries.UserTaskStatus.objects.filter'
1401-
) as mock_filter:
1402-
mock_filter.return_value = mock_queryset
1403-
1404-
status = api.get_backup_task_status(self.lib1.library_key, self.user.id)
1405-
assert status is not None
1406-
assert status['state'] == UserTaskStatus.PENDING
1407-
assert status['url'] is None
1408-
1409-
# Verify the filter was called with correct parameters
1410-
mock_filter.assert_called_once_with(
1411-
user_id=self.user.id,
1412-
name__contains=str(self.lib1.library_key)
1413-
)
1414-
14151387
def test_get_backup_task_status_failed(self) -> None:
14161388
# Create a mock UserTaskStatus in FAILED state
14171389
task_id = str(uuid.uuid4())
@@ -1427,7 +1399,7 @@ def test_get_backup_task_status_failed(self) -> None:
14271399
) as mock_get:
14281400
mock_get.return_value = mock_task
14291401

1430-
status = api.get_backup_task_status(self.lib1.library_key, self.user.id, task_id=task_id)
1402+
status = api.get_backup_task_status(self.user.id, task_id=task_id)
14311403
assert status is not None
14321404
assert status['state'] == UserTaskStatus.FAILED
14331405
assert status['url'] is None

0 commit comments

Comments
 (0)