Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ContentMetadata.bulk_update() changes the modified value #890

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading