From b749581162823104aba53d649489290fbeb60799 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Mon, 3 Aug 2020 22:55:18 +0800 Subject: [PATCH] Moved the data preparation out of the transaction in the txn_store() view in the accounting application. --- accounting/views.py | 48 +++++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/accounting/views.py b/accounting/views.py index bb024aa..0bd61fd 100644 --- a/accounting/views.py +++ b/accounting/views.py @@ -863,6 +863,7 @@ def txn_store(request, txn_type, txn=None): url = reverse("accounting:transactions.edit", args=(txn_type, txn)) url = str(UrlBuilder(url).set("r", request.GET.get("r"))) return error_redirect(request, url, post) + if txn is None: txn = Transaction() fill_txn_from_post(txn_type, txn, post) @@ -871,31 +872,36 @@ def txn_store(request, txn_type, txn=None): url = str(UrlBuilder(url).set("r", request.GET.get("r"))) message = gettext_noop("This transaction was not modified.") return success_redirect(request, url, message) + + # Prepares the data + user = request.user + # TODO: Set transaction order when date changed. + if txn.pk is None: + max_ord = Transaction.objects\ + .filter(date=txn.date)\ + .annotate(max=Max("ord"))\ + .first() + txn.pk = new_pk(Transaction) + txn.ord = 1 if max_ord is None else max_ord.max + 1 + txn.created_at = Now() + txn.created_by = user + txn.updated_at = Now() + txn.updated_by = user + for record in txn.records: + if record.pk is None: + record.pk = new_pk(Record) + record.created_at = Now() + record.created_by = user + record.updated_at = Now() + record.updated_by = user + to_keep = [x.pk for x in txn.records if x.pk is not None] + to_delete = [x for x in txn.record_set.all() if x.pk not in to_keep] + # Runs the update with transaction.atomic(): - user = request.user - if txn.pk is None: - max_ord = Transaction.objects\ - .filter(date=txn.date)\ - .annotate(max=Max("ord"))\ - .first() - txn.pk = new_pk(Transaction) - txn.ord = 1 if max_ord is None else max_ord.max + 1 - txn.created_at = Now() - txn.created_by = user - txn.updated_at = Now() - txn.updated_by = user txn.save() - existing = [x.pk for x in txn.records if x.pk is not None] - for record in [x for x in txn.record_set.all() - if x.pk not in existing]: + for record in to_delete: record.delete() for record in txn.records: - if record.pk is None: - record.pk = new_pk(Record) - record.created_at = Now() - record.created_by = user - record.updated_at = Now() - record.updated_by = user record.save() url = reverse("accounting:transactions.show", args=(txn_type, txn)) url = str(UrlBuilder(url).set("r", request.GET.get("r")))