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.

This commit is contained in:
依瑪貓 2020-08-25 07:47:20 +08:00
parent 388ff18461
commit ddb449262e
2 changed files with 13 additions and 12 deletions

View File

@ -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],

View File

@ -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)