diff --git a/enterprise_catalog/apps/catalog/models.py b/enterprise_catalog/apps/catalog/models.py index eb72ad045..d3f4e01db 100644 --- a/enterprise_catalog/apps/catalog/models.py +++ b/enterprise_catalog/apps/catalog/models.py @@ -493,6 +493,26 @@ def get_xapi_activity_id(self, content_resource, content_key): return xapi_activity_id +class ContentMetadataManager(models.Manager): + """ + Customer manager for ContentMetadata that forces the `modified` field + to be updated during `bulk_update()`. + """ + + def bulk_update(self, objs, fields, batch_size=None): + """ + Updates the `modified` time of each object, and then + does the usual bulk update, with `modified` as also + a field to save. + """ + last_modified = localized_utcnow() + for obj in objs: + obj.modified = last_modified + fields += ['modified'] + + super().bulk_update(objs, fields, batch_size=batch_size) + + class ContentMetadata(TimeStampedModel): """ Stores the JSON metadata for a piece of content, such as a course, course run, or program. @@ -556,6 +576,8 @@ class ContentMetadata(TimeStampedModel): history = HistoricalRecords() + objects = ContentMetadataManager() + class Meta: verbose_name = _("Content Metadata") verbose_name_plural = _("Content Metadata") diff --git a/enterprise_catalog/apps/catalog/tests/test_models.py b/enterprise_catalog/apps/catalog/tests/test_models.py index d87401066..b961686fd 100644 --- a/enterprise_catalog/apps/catalog/tests/test_models.py +++ b/enterprise_catalog/apps/catalog/tests/test_models.py @@ -573,3 +573,20 @@ def test_associate_content_metadata_with_query_guardrails(self, mock_client): catalog.catalog_query.modified = localized_utcnow() update_contentmetadata_from_discovery(catalog.catalog_query) assert len(catalog.catalog_query.contentmetadata_set.all()) == 1 + + def test_bulk_update_changes_modified_time(self): + """ + Test that `ContentMetadata.objects.bulk_update()` changes + the modified time of the updated records. + """ + original_modified_time = localized_utcnow() + records = factories.ContentMetadataFactory.create_batch(10, modified=original_modified_time) + + for record in records: + record.json_metadata['extra_stuff'] = 'foo' + + ContentMetadata.objects.bulk_update(records, ['json_metadata'], batch_size=10) + + for record in records: + record.refresh_from_db() + self.assertGreater(record.modified, original_modified_time)