Moved the data preparation out of the transaction in the txn_store() view in the accounting application.

This commit is contained in:
依瑪貓 2020-08-03 22:55:18 +08:00
parent 1d7acef3e1
commit b749581162

View File

@ -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,8 +872,10 @@ 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)
with transaction.atomic():
# 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)\
@ -884,11 +887,6 @@ def txn_store(request, txn_type, txn=None):
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]:
record.delete()
for record in txn.records:
if record.pk is None:
record.pk = new_pk(Record)
@ -896,6 +894,14 @@ def txn_store(request, txn_type, txn=None):
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():
txn.save()
for record in to_delete:
record.delete()
for record in txn.records:
record.save()
url = reverse("accounting:transactions.show", args=(txn_type, txn))
url = str(UrlBuilder(url).set("r", request.GET.get("r")))