From 884a7d0f08e18258f172354c365e1600dddf36d4 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Mon, 23 Mar 2020 21:15:59 +0100 Subject: [PATCH] Use a proxy for special transactions in treasury app for modularity (not a clean way, but without any other solution...) --- apps/note/models/__init__.py | 4 ++-- apps/note/models/transactions.py | 8 -------- apps/treasury/models.py | 26 ++++++++++++++++++++++---- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/apps/note/models/__init__.py b/apps/note/models/__init__.py index 8f1921f9..e9c8a0a9 100644 --- a/apps/note/models/__init__.py +++ b/apps/note/models/__init__.py @@ -3,12 +3,12 @@ from .notes import Alias, Note, NoteClub, NoteSpecial, NoteUser from .transactions import MembershipTransaction, Transaction, \ - TemplateCategory, TransactionTemplate, RecurrentTransaction + TemplateCategory, TransactionTemplate, RecurrentTransaction, SpecialTransaction __all__ = [ # Notes 'Alias', 'Note', 'NoteClub', 'NoteSpecial', 'NoteUser', # Transactions 'MembershipTransaction', 'Transaction', 'TemplateCategory', 'TransactionTemplate', - 'RecurrentTransaction', + 'RecurrentTransaction', 'SpecialTransaction', ] diff --git a/apps/note/models/transactions.py b/apps/note/models/transactions.py index 6dbf3504..0e40edf6 100644 --- a/apps/note/models/transactions.py +++ b/apps/note/models/transactions.py @@ -6,7 +6,6 @@ from django.urls import reverse from django.utils import timezone from django.utils.translation import gettext_lazy as _ from polymorphic.models import PolymorphicModel -from treasury.models import Remittance from .notes import Note, NoteClub, NoteSpecial @@ -210,13 +209,6 @@ class SpecialTransaction(Transaction): blank=True, ) - remittance = models.ForeignKey( - Remittance, - on_delete=models.PROTECT, - null=True, - verbose_name=_("Remittance"), - ) - @property def type(self): return _('Credit') if isinstance(self.source, NoteSpecial) else _("Debit") diff --git a/apps/treasury/models.py b/apps/treasury/models.py index 52b25157..359a3219 100644 --- a/apps/treasury/models.py +++ b/apps/treasury/models.py @@ -5,7 +5,7 @@ from django.core.exceptions import ValidationError from django.db import models from django.db.models import Q from django.utils.translation import gettext_lazy as _ -from note.models import NoteSpecial +from note.models import NoteSpecial, SpecialTransaction class Invoice(models.Model): @@ -110,18 +110,36 @@ class Remittance(models.Model): verbose_name=_("Closed"), ) + @property + def transactions(self): + return SpecialTransaction.objects.filter(specialtransactionproxy__remittance=self) + @property def size(self): - return self.specialtransaction_set.count() + return self.transactions.count() @property def amount(self): - return sum(transaction.total for transaction in self.specialtransaction_set.all()) + return sum(transaction.total for transaction in self.transactions.all()) def full_clean(self, exclude=None, validate_unique=True): ret = super().full_clean(exclude, validate_unique) - if self.specialtransaction_set.filter(~Q(source=self.type)).exists(): + if self.transactions.filter(~Q(source=self.type)).exists(): raise ValidationError("All transactions in a remittance must have the same type") return ret + + +class SpecialTransactionProxy(models.Model): + transaction = models.OneToOneField( + SpecialTransaction, + on_delete=models.CASCADE, + ) + + remittance = models.ForeignKey( + Remittance, + on_delete=models.PROTECT, + null=True, + verbose_name=_("Remittance"), + )