perf: Perfect solution to account version problem

pull/14723/head
feng 2024-12-24 10:13:57 +08:00 committed by feng626
parent a0b7c115ae
commit 458243abe5
1 changed files with 7 additions and 4 deletions

View File

@ -3,12 +3,14 @@ from django.utils.translation import gettext_lazy as _
from simple_history.models import HistoricalRecords from simple_history.models import HistoricalRecords
from assets.models.base import AbsConnectivity from assets.models.base import AbsConnectivity
from common.utils import lazyproperty from common.utils import lazyproperty, get_logger
from labels.mixins import LabeledMixin from labels.mixins import LabeledMixin
from .base import BaseAccount from .base import BaseAccount
from .mixins import VaultModelMixin from .mixins import VaultModelMixin
from ..const import Source from ..const import Source
logger = get_logger(__file__)
__all__ = ['Account', 'AccountHistoricalRecords'] __all__ = ['Account', 'AccountHistoricalRecords']
@ -26,7 +28,7 @@ class AccountHistoricalRecords(HistoricalRecords):
history_account = instance.history.first() history_account = instance.history.first()
if history_account is None: if history_account is None:
self.updated_version = 1 self.updated_version = 0
return super().post_save(instance, created, using=using, **kwargs) return super().post_save(instance, created, using=using, **kwargs)
history_attrs = {field: getattr(history_account, field) for field in check_fields} history_attrs = {field: getattr(history_account, field) for field in check_fields}
@ -38,12 +40,13 @@ class AccountHistoricalRecords(HistoricalRecords):
if not diff: if not diff:
return return
self.updated_version = history_account.version + 1 self.updated_version = history_account.version + 1
instance.version = self.updated_version
return super().post_save(instance, created, using=using, **kwargs) return super().post_save(instance, created, using=using, **kwargs)
def create_historical_record(self, instance, history_type, using=None): def create_historical_record(self, instance, history_type, using=None):
super().create_historical_record(instance, history_type, using=using) super().create_historical_record(instance, history_type, using=using)
if self.updated_version is not None: # Ignore deletion history_type: -
instance.version = self.updated_version if self.updated_version is not None and history_type != '-':
instance.save(update_fields=['version']) instance.save(update_fields=['version'])
def create_history_model(self, model, inherited): def create_history_model(self, model, inherited):