We don't need to create proxies for special transactions that don't require a remittance

This commit is contained in:
Yohann D'ANELLO 2020-03-24 17:14:29 +01:00
parent 4cf1047faa
commit 093e585b79
2 changed files with 10 additions and 4 deletions

View File

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

View File

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