From ddb449262ea8ba9039ff148597de26bf8f25340d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Tue, 25 Aug 2020 07:47:20 +0800 Subject: [PATCH] Revised the code to deal with the current user in the data models, so that the currently logged-in user can be supplied in the initial data. --- accounting/utils.py | 11 ++++------- mia_core/models.py | 14 +++++++++----- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/accounting/utils.py b/accounting/utils.py index 48cd103..5090b5c 100644 --- a/accounting/utils.py +++ b/accounting/utils.py @@ -155,8 +155,7 @@ class DataFiller: code = str(code) parent = None if len(code) == 1\ else Account.objects.get(code=code[:-1]) - account = Account(parent=parent, code=code) - account.current_user = self.user + account = Account(parent=parent, code=code, current_user=self.user) account.set_l10n_in("title", "zh-hant", data[1]) account.set_l10n_in("title", "en", data[2]) account.set_l10n_in("title", "zh-hans", data[3]) @@ -177,7 +176,7 @@ class DataFiller: date = timezone.localdate() + timezone.timedelta(days=date) order = Transaction.objects.filter(date=date).count() + 1 transaction = Transaction(pk=new_pk(Transaction), date=date, ord=order, - created_by=self.user, updated_by=self.user) + current_user=self.user) transaction.save() order = 1 for data in debit: @@ -189,8 +188,7 @@ class DataFiller: transaction.record_set.create(pk=new_pk(Record), is_credit=False, ord=order, account=account, summary=data[1], amount=data[2], - created_by=self.user, - updated_by=self.user) + current_user=self.user) order = order + 1 order = 1 for data in credit: @@ -202,8 +200,7 @@ class DataFiller: transaction.record_set.create(pk=new_pk(Record), is_credit=True, ord=order, account=account, summary=data[1], amount=data[2], - created_by=self.user, - updated_by=self.user) + current_user=self.user) order = order + 1 def add_income_transaction(self, date: Union[datetime.date, int], diff --git a/mia_core/models.py b/mia_core/models.py index bc5d4fc..a992e86 100644 --- a/mia_core/models.py +++ b/mia_core/models.py @@ -56,18 +56,22 @@ class StampedModel(models.Model): related_name="updated_%(app_label)s_%(class)s") def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) self.current_user = None + if "current_user" in kwargs: + self.current_user = kwargs["current_user"] + del kwargs["current_user"] + super().__init__(*args, **kwargs) def save(self, force_insert=False, force_update=False, using=None, update_fields=None): + if self.current_user is None: + raise AttributeError( + F"Missing current_user in {self.__class__.__name__}") try: self.created_by except ObjectDoesNotExist as e: - if self.current_user is not None: - self.created_by = self.current_user - if self.current_user is not None: - self.updated_by = self.current_user + self.created_by = self.current_user + self.updated_by = self.current_user super().save(force_insert=force_insert, force_update=force_update, using=using, update_fields=update_fields)