Skip to content

Commit

Permalink
fix: ContentMetadata.bulk_update() changes the modified value
Browse files Browse the repository at this point in the history
  • Loading branch information
iloveagent57 committed Jul 25, 2024
1 parent 2ad9da1 commit 5f53a30
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
22 changes: 22 additions & 0 deletions enterprise_catalog/apps/catalog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -556,6 +576,8 @@ class ContentMetadata(TimeStampedModel):

history = HistoricalRecords()

objects = ContentMetadataManager()

class Meta:
verbose_name = _("Content Metadata")
verbose_name_plural = _("Content Metadata")
Expand Down
17 changes: 17 additions & 0 deletions enterprise_catalog/apps/catalog/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 5f53a30

Please sign in to comment.