Make dumpstuff more efficient

This commit is contained in:
Martin Pépin 2020-02-06 20:03:40 +01:00
parent 8baaa1d339
commit 23c1de0fc6
No known key found for this signature in database
GPG key ID: E7520278B1774448

View file

@ -33,19 +33,28 @@ def dump_articles(filename: str) -> None:
def dump_operations(filename: str, accounts_hashes: Dict[int, str]) -> None:
not_canceled_purchases = Operation.objects.filter(type=Operation.PURCHASE).filter(
canceled_at__isnull=True
not_canceled_purchases = (
Operation.objects.filter(type=Operation.PURCHASE)
.filter(canceled_at__isnull=True)
.values_list(
"amount",
"article__id",
"article_nb",
"group__at",
"group__is_cof",
"group__on_acc__id",
)
)
operations = [
{
"amount": str(operation.amount),
"article": operation.article.id,
"number": operation.article_nb,
"date": str(operation.group.at),
"is_cof": operation.group.is_cof,
"on_account": accounts_hashes[operation.group.on_acc.id],
"amount": str(amount),
"article": article_id,
"number": nb,
"date": str(at),
"is_cof": is_cof,
"on_account": accounts_hashes[on_acc],
}
for operation in not_canceled_purchases
for amount, article_id, nb, at, is_cof, on_acc in not_canceled_purchases
]
with open(filename, "w") as file:
json.dump(operations, file, indent=4)