Skip to content

Commit 17364de

Browse files
committed
🐛(back) fix management command to rename documents
After some tests on preprod, the management commands was not working as the documents have blank filenames. Fixing the command by using the title to create the filename instead.
1 parent 8dc9d6d commit 17364de

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

src/backend/marsha/core/management/commands/rename_documents.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def validate_filename(self, value):
3636

3737
value = value.replace("/", "_")
3838
value = value.replace("\\", "_")
39+
value = value.replace(" ", "_")
3940
value = value.lstrip(".")
4041

4142
return value
@@ -58,7 +59,7 @@ def handle(self, *args, **options):
5859
"Key": file_key_src,
5960
}
6061

61-
filename = self.validate_filename(document.filename)
62+
filename = self.validate_filename(document.title + extension)
6263

6364
# Override document filename with the validated S3-compatible filename
6465
if filename != document.filename:

src/backend/marsha/core/tests/management_commands/test_rename_documents.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
from marsha.core.utils import time_utils
1717

1818

19+
# pylint: disable=too-many-locals
20+
21+
1922
class RenameDocumentsTestCase(TestCase):
2023
"""
2124
Test the ``rename_documents`` command.
@@ -31,17 +34,18 @@ def test_rename_documents(self, mock_exists):
3134

3235
with Stubber(rename_documents.s3_client) as s3_client_stubber:
3336
# Generate some documents
34-
# (<original filename>, <expected and cleaned>)
37+
# (<original title>, <expected and cleaned filename>)
3538
filenames = [
36-
("normal_filename.pdf", "normal_filename.pdf"),
37-
("weird\\file/name.pdf", "weird_file_name.pdf"),
38-
(".hidden_file", "hidden_file"),
39+
("normal_filename", "normal_filename.pdf"),
40+
("weird\\file name", "weird_file_name.pdf"),
41+
(".hidden_file", "hidden_file.pdf"),
3942
]
4043

4144
documents = []
4245
for filename_src, _ in filenames:
4346
document = DocumentFactory(
44-
filename=filename_src,
47+
title=filename_src,
48+
extension="pdf",
4549
uploaded_on=now,
4650
upload_state=READY,
4751
storage_location=AWS_S3,
@@ -57,8 +61,9 @@ def test_rename_documents(self, mock_exists):
5761

5862
file_key_src = f"aws/{document.id}/document/{stamp}{extension}"
5963

64+
filename = document.title + extension
6065
sanitized_filename = rename_documents.Command().validate_filename(
61-
document.filename
66+
filename
6267
)
6368
file_key_dest = f"aws/{document.id}/document/{sanitized_filename}"
6469

0 commit comments

Comments
 (0)