From 6a59a2c7b728c646381942303b2e9df033b23186 Mon Sep 17 00:00:00 2001 From: Zaman Afzal Date: Wed, 6 Dec 2023 15:00:32 +0500 Subject: [PATCH] Override the default save method of ``EnterpriseCustomerPluginConfiguration`` to update only changed fields (#1960) * refactor: change save method of EnterpriseCustomerPluginConfiguration to update only changed fields --- CHANGELOG.rst | 3 +++ enterprise/__init__.py | 2 +- .../commands/update_config_last_errored_at.py | 4 +++- .../integrated_channel/models.py | 20 +++++++++++++++++-- 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index f5ed4ff4ef..557ebc7f2c 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -15,6 +15,9 @@ Change Log Unreleased ---------- +[4.8.7] +-------- +refactor: Override the default save method of ``EnterpriseCustomerPluginConfiguration`` to update only changed fields [4.8.6] -------- diff --git a/enterprise/__init__.py b/enterprise/__init__.py index e61935bbee..f5de6fc19a 100644 --- a/enterprise/__init__.py +++ b/enterprise/__init__.py @@ -2,4 +2,4 @@ Your project description goes here. """ -__version__ = "4.8.6" +__version__ = "4.8.7" diff --git a/enterprise/management/commands/update_config_last_errored_at.py b/enterprise/management/commands/update_config_last_errored_at.py index 898377beb5..e006e6d483 100644 --- a/enterprise/management/commands/update_config_last_errored_at.py +++ b/enterprise/management/commands/update_config_last_errored_at.py @@ -111,7 +111,9 @@ def update_config_last_errored_at(self): config.id, channel_code, config.enterprise_customer.uuid ) ) - config.save() + config.save(update_fields=["last_learner_sync_errored_at", "last_content_sync_errored_at", + "last_sync_errored_at"]) + except Exception as exc: LOGGER.exception('update_config_last_errored_at', exc_info=exc) raise exc diff --git a/integrated_channels/integrated_channel/models.py b/integrated_channels/integrated_channel/models.py index e81dee4de8..4e20d2a43f 100644 --- a/integrated_channels/integrated_channel/models.py +++ b/integrated_channels/integrated_channel/models.py @@ -273,31 +273,47 @@ def update_content_synced_at(self, action_happened_at, was_successful): """ Given the last time a Content record sync was attempted and status update the appropriate timestamps. """ + update_fields = [] if self.last_sync_attempted_at is None or action_happened_at > self.last_sync_attempted_at: self.last_sync_attempted_at = action_happened_at + update_fields.append('last_sync_attempted_at') if self.last_content_sync_attempted_at is None or action_happened_at > self.last_content_sync_attempted_at: self.last_content_sync_attempted_at = action_happened_at + update_fields.append('last_content_sync_attempted_at') if not was_successful: if self.last_sync_errored_at is None or action_happened_at > self.last_sync_errored_at: self.last_sync_errored_at = action_happened_at + update_fields.append('last_sync_errored_at') if self.last_content_sync_errored_at is None or action_happened_at > self.last_content_sync_errored_at: self.last_content_sync_errored_at = action_happened_at - return self.save() + update_fields.append('last_content_sync_errored_at') + if update_fields: + return self.save(update_fields=update_fields) + else: + return self def update_learner_synced_at(self, action_happened_at, was_successful): """ Given the last time a Learner record sync was attempted and status update the appropriate timestamps. """ + update_fields = [] if self.last_sync_attempted_at is None or action_happened_at > self.last_sync_attempted_at: self.last_sync_attempted_at = action_happened_at + update_fields.append('last_sync_attempted_at') if self.last_learner_sync_attempted_at is None or action_happened_at > self.last_learner_sync_attempted_at: self.last_learner_sync_attempted_at = action_happened_at + update_fields.append('last_learner_sync_attempted_at') if not was_successful: if self.last_sync_errored_at is None or action_happened_at > self.last_sync_errored_at: self.last_sync_errored_at = action_happened_at + update_fields.append('last_sync_errored_at') if self.last_learner_sync_errored_at is None or action_happened_at > self.last_learner_sync_errored_at: self.last_learner_sync_errored_at = action_happened_at - return self.save() + update_fields.append('last_learner_sync_errored_at') + if update_fields: + return self.save(update_fields=update_fields) + else: + return self @property def is_valid(self):