From 093e585b795f05b82149320035b7b7d3540053ef Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Tue, 24 Mar 2020 17:14:29 +0100 Subject: [PATCH] We don't need to create proxies for special transactions that don't require a remittance --- apps/treasury/apps.py | 10 ++++++++-- apps/treasury/signals.py | 4 ++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/apps/treasury/apps.py b/apps/treasury/apps.py index 14ca36e7..6e07a5f5 100644 --- a/apps/treasury/apps.py +++ b/apps/treasury/apps.py @@ -2,8 +2,10 @@ # SPDX-License-Identifier: GPL-3.0-or-later from django.apps import AppConfig +from django.db.models import Q from django.db.models.signals import post_save, post_migrate from django.utils.translation import gettext_lazy as _ +from note.models import NoteSpecial class TreasuryConfig(AppConfig): @@ -21,8 +23,12 @@ class TreasuryConfig(AppConfig): post_save.connect(signals.save_special_transaction, sender=SpecialTransaction) def setup_specialtransactions_proxies(**kwargs): - # If the treasury app was disabled, we ensure that each special transaction is linked to a proxy - for transaction in SpecialTransaction.objects.filter(specialtransactionproxy=None): + # If the treasury app was disabled for any reason during a certain amount of time, + # we ensure that each special transaction is linked to a proxy + for transaction in SpecialTransaction.objects.filter( + source__in=NoteSpecial.objects.filter(~Q(remittancetype=None)), + specialtransactionproxy=None, + ): SpecialTransactionProxy.objects.create(transaction=transaction, remittance=None) post_migrate.connect(setup_specialtransactions_proxies, sender=SpecialTransactionProxy) diff --git a/apps/treasury/signals.py b/apps/treasury/signals.py index e1183d6b..54c19c09 100644 --- a/apps/treasury/signals.py +++ b/apps/treasury/signals.py @@ -1,12 +1,12 @@ # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later -from treasury.models import SpecialTransactionProxy +from treasury.models import SpecialTransactionProxy, RemittanceType def save_special_transaction(instance, created, **kwargs): """ When a special transaction is created, we create its linked proxy """ - if created: + if created and RemittanceType.objects.filter(note=instance.source).exists(): SpecialTransactionProxy.objects.create(transaction=instance, remittance=None).save()