From 4909a7f09c817985476cd6516c146f5f53f4d082 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Mon, 16 Mar 2020 13:14:06 +0100 Subject: [PATCH] Transaction types are now properties --- apps/note/models/transactions.py | 32 +++++++++++++++------------- static/js/consos.js | 1 - templates/note/transaction_form.html | 8 ++----- 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/apps/note/models/transactions.py b/apps/note/models/transactions.py index 880baaed..86c00737 100644 --- a/apps/note/models/transactions.py +++ b/apps/note/models/transactions.py @@ -7,7 +7,7 @@ from django.utils import timezone from django.utils.translation import gettext_lazy as _ from polymorphic.models import PolymorphicModel -from .notes import Note, NoteClub +from .notes import Note, NoteClub, NoteSpecial """ Defines transactions @@ -111,20 +111,6 @@ class Transaction(PolymorphicModel): verbose_name=_('amount'), ) - type = models.CharField( - verbose_name=_('type'), - choices=( - ('gift', _('Gift')), - ('transfer', _('Transfer')), - ('template', _('Template')), - ('credit', _('Credit')), - ('debit', _('Debit')), - ('membership', _('membership transaction')), - ), - default='transfer', - max_length=10, - ) - reason = models.CharField( verbose_name=_('reason'), max_length=255, @@ -175,6 +161,10 @@ class Transaction(PolymorphicModel): def total(self): return self.amount * self.quantity + @property + def type(self): + return _('Transfer') + class TemplateTransaction(Transaction): """ @@ -191,6 +181,10 @@ class TemplateTransaction(Transaction): on_delete=models.PROTECT, ) + @property + def type(self): + return _('Template') + class SpecialTransaction(Transaction): """ @@ -213,6 +207,10 @@ class SpecialTransaction(Transaction): blank=True, ) + @property + def type(self): + return _('Credit') if isinstance(self.source, NoteSpecial) else _("Debit") + class MembershipTransaction(Transaction): """ @@ -229,3 +227,7 @@ class MembershipTransaction(Transaction): class Meta: verbose_name = _("membership transaction") verbose_name_plural = _("membership transactions") + + @property + def type(self): + return _('membership transaction') diff --git a/static/js/consos.js b/static/js/consos.js index 9fb0a8cb..5f7a314a 100644 --- a/static/js/consos.js +++ b/static/js/consos.js @@ -193,7 +193,6 @@ function consume(source, dest, quantity, amount, reason, type, category, templat "valid": true, "polymorphic_ctype": type, "resourcetype": "TemplateTransaction", - "type": "template", "source": source, "destination": dest, "category": category, diff --git a/templates/note/transaction_form.html b/templates/note/transaction_form.html index 4cbf4e99..37cccee0 100644 --- a/templates/note/transaction_form.html +++ b/templates/note/transaction_form.html @@ -201,8 +201,8 @@ SPDX-License-Identifier: GPL-2.0-or-later $("#type_transfer").removeAttr('checked'); $("#type_credit").removeAttr('checked'); $("#type_debit").removeAttr('checked'); - $("label[for='type_gift']").attr('class', 'btn btn-sm btn-outline-primary'); $("label[for='type_transfer']").attr('class', 'btn btn-sm btn-outline-primary'); + $("label[for='type_credit']").attr('class', 'btn btn-sm btn-outline-primary'); $("label[for='type_debit']").attr('class', 'btn btn-sm btn-outline-primary'); }); @@ -246,7 +246,6 @@ SPDX-License-Identifier: GPL-2.0-or-later "valid": true, "polymorphic_ctype": {{ polymorphic_ctype }}, "resourcetype": "Transaction", - "type": "gift", "source": {{ user.note.id }}, "destination": dest.id }, function () { @@ -276,7 +275,6 @@ SPDX-License-Identifier: GPL-2.0-or-later "valid": true, "polymorphic_ctype": {{ polymorphic_ctype }}, "resourcetype": "Transaction", - "type": "transfer", "source": source.id, "destination": dest.id }, function () { @@ -298,11 +296,10 @@ SPDX-License-Identifier: GPL-2.0-or-later let special_note = $("#credit_type").val(); let user_note = dests_notes_display[0].id; let given_reason = $("#reason").val(); - let source, dest, reason, type; + let source, dest, reason; if ($("#type_credit").is(':checked')) { source = special_note; dest = user_note; - type = "credit"; reason = "Crédit " + $("#credit_type option:selected").text().toLowerCase(); if (given_reason.length > 0) reason += " (" + given_reason + ")"; @@ -324,7 +321,6 @@ SPDX-License-Identifier: GPL-2.0-or-later "valid": true, "polymorphic_ctype": {{ special_polymorphic_ctype }}, "resourcetype": "SpecialTransaction", - "type": type, "source": source, "destination": dest, "last_name": $("#last_name").val(),