77import os
88import typing as t
99from dataclasses import dataclass
10- from datetime import datetime , timezone
10+ from datetime import datetime , timezone , UTC
1111from enum import Enum
1212from gettext import ngettext
1313
@@ -341,6 +341,7 @@ def _import_structure(
341341 openedx_content equivalent.
342342 """
343343 migration = source_data .migration
344+ migration_start_time = datetime .now (UTC )
344345 migration_context = _MigrationContext (
345346 used_component_keys = set (
346347 LibraryUsageLocatorV2 (target_library .key , block_type , block_id ) # type: ignore[abstract]
@@ -367,9 +368,13 @@ def _import_structure(
367368 repeat_handling_strategy = RepeatHandlingStrategy (migration .repeat_handling_strategy ),
368369 preserve_url_slugs = migration .preserve_url_slugs ,
369370 created_by = status .user_id ,
370- created_at = datetime . now ( timezone . utc ), # noqa: UP017
371+ created_at = migration_start_time ,
371372 )
372- with content_api .bulk_draft_changes_for (migration .target .id ) as change_log :
373+ with content_api .bulk_draft_changes_for (
374+ learning_package_id = migration .target .id ,
375+ changed_by = migration_context .created_by ,
376+ changed_at = migration_start_time ,
377+ ) as change_log :
373378 root_migrated_node = _migrate_node (
374379 context = migration_context ,
375380 source_node = root_node ,
@@ -390,6 +395,7 @@ def _import_structure(
390395 migration .target .id ,
391396 draft_qset = drafts_to_publish ,
392397 published_by = migration_context .created_by ,
398+ # published_at will be later than 'migration_start_time' as _migrate_node() may have taken quite a while.
393399 )
394400 return change_log , root_migrated_node
395401
@@ -424,7 +430,7 @@ def _populate_collection(user_id: int, migration: models.ModulestoreMigration) -
424430 )
425431 if block_target_pks :
426432 content_api .add_to_collection (
427- learning_package_id = migration .target .pk ,
433+ learning_package_id = migration .target .id ,
428434 collection_code = migration .target_collection .collection_code ,
429435 entities_qset = PublishableEntity .objects .filter (id__in = block_target_pks ),
430436 created_by = user_id ,
@@ -437,6 +443,7 @@ def _create_collection(
437443 library_key : LibraryLocatorV2 ,
438444 title : str ,
439445 course_name : str | None = None ,
446+ created_by : int | None = None ,
440447) -> Collection :
441448 """
442449 Creates a collection in the given library
@@ -462,6 +469,7 @@ def _create_collection(
462469 collection_key = modified_key ,
463470 title = f"{ title } { f'_{ attempt } ' if attempt > 0 else '' } " ,
464471 description = description ,
472+ created_by = created_by ,
465473 )
466474 except libraries_api .LibraryCollectionAlreadyExists :
467475 attempt += 1
@@ -721,6 +729,7 @@ def bulk_migrate_from_modulestore(
721729 library_key = target_library_locator ,
722730 title = legacy_root_list [i ].display_name ,
723731 course_name = legacy_root_list [i ].display_name if source_data .source .key .is_course else None ,
732+ created_by = user_id ,
724733 )
725734 )
726735 _populate_collection (user_id , migration )
@@ -981,7 +990,7 @@ def _migrate_component(
981990
982991 # Create the new component version for it
983992 component_version = libraries_api .set_library_block_olx (
984- target_key , new_olx_str = olx , paths_to_media = paths_to_media_ids ,
993+ target_key , new_olx_str = olx , paths_to_media = paths_to_media_ids , created_by = context . created_by ,
985994 )
986995
987996 # Note: Publishing happens after bulk_draft_changes_for exits, in _import_structure.
0 commit comments