From c2fbb1bcdfd8461f77181e7c0140164905fe14fd Mon Sep 17 00:00:00 2001 From: Benjamin Graillot Date: Mon, 24 Feb 2020 10:36:04 +0100 Subject: [PATCH] Polymorphic transaction --- apps/note/admin.py | 20 +++++++------------- apps/note/forms.py | 9 +++++---- apps/note/models/__init__.py | 5 +++-- apps/note/models/transactions.py | 29 ++--------------------------- apps/note/views.py | 5 +++-- 5 files changed, 20 insertions(+), 48 deletions(-) diff --git a/apps/note/admin.py b/apps/note/admin.py index e8966d44..52c1cc17 100644 --- a/apps/note/admin.py +++ b/apps/note/admin.py @@ -7,7 +7,8 @@ from polymorphic.admin import PolymorphicChildModelAdmin, \ PolymorphicChildModelFilter, PolymorphicParentModelAdmin from .models.notes import Alias, Note, NoteClub, NoteSpecial, NoteUser -from .models.transactions import Transaction, TemplateCategory, TransactionTemplate, TransactionType +from .models.transactions import Transaction, TemplateCategory, TransactionTemplate, \ + TemplateTransaction, MembershipTransaction class AliasInlines(admin.TabularInline): @@ -97,13 +98,14 @@ class NoteUserAdmin(PolymorphicChildModelAdmin): @admin.register(Transaction) -class TransactionAdmin(admin.ModelAdmin): +class TransactionAdmin(PolymorphicParentModelAdmin): """ Admin customisation for Transaction """ + child_models = (TemplateTransaction, MembershipTransaction) list_display = ('created_at', 'poly_source', 'poly_destination', - 'quantity', 'amount', 'transaction_type', 'valid') - list_filter = ('transaction_type', 'valid') + 'quantity', 'amount', 'valid') + list_filter = ('valid',) autocomplete_fields = ( 'source', 'destination', @@ -132,7 +134,7 @@ class TransactionAdmin(admin.ModelAdmin): """ if obj: # user is editing an existing object return 'created_at', 'source', 'destination', 'quantity',\ - 'amount', 'transaction_type' + 'amount' return [] @@ -161,11 +163,3 @@ class TemplateCategoryAdmin(admin.ModelAdmin): """ list_display = ('name', ) list_filter = ('name', ) - -@admin.register(TransactionType) -class TransactionTypeAdmin(admin.ModelAdmin): - """ - Admin customisation for TransactionTemplate - """ - list_display = ('name', ) - list_filter = ('name', ) diff --git a/apps/note/forms.py b/apps/note/forms.py index e4fd344c..2f49d88f 100644 --- a/apps/note/forms.py +++ b/apps/note/forms.py @@ -4,7 +4,7 @@ from dal import autocomplete from django import forms -from .models import Transaction, TransactionTemplate +from .models import Transaction, TransactionTemplate, TemplateTransaction class TransactionTemplateForm(forms.ModelForm): @@ -71,12 +71,13 @@ class ConsoForm(forms.ModelForm): name=self.data['button']).get() self.instance.destination = button.destination self.instance.amount = button.amount - self.instance.transaction_type = 'bouton' - self.instance.reason = button.name + self.instance.reason = '{} ({})'.format(button.name, button.category) + self.instance.name = button.name + self.instance.category = button.category super().save(commit) class Meta: - model = Transaction + model = TemplateTransaction fields = ('source', ) # Le champ d'utilisateur est remplacé par un champ d'auto-complétion. diff --git a/apps/note/models/__init__.py b/apps/note/models/__init__.py index bac4f0ba..081b31a7 100644 --- a/apps/note/models/__init__.py +++ b/apps/note/models/__init__.py @@ -3,11 +3,12 @@ from .notes import Alias, Note, NoteClub, NoteSpecial, NoteUser from .transactions import MembershipTransaction, Transaction, \ - TemplateCategory, TransactionTemplate, TransactionType + TemplateCategory, TransactionTemplate, TemplateTransaction __all__ = [ # Notes 'Alias', 'Note', 'NoteClub', 'NoteSpecial', 'NoteUser', # Transactions - 'MembershipTransaction', 'Transaction', 'TemplateCategory', 'TransactionTemplate','TransactionType', + 'MembershipTransaction', 'Transaction', 'TemplateCategory', 'TransactionTemplate', + 'TemplateTransaction', ] diff --git a/apps/note/models/transactions.py b/apps/note/models/transactions.py index a8650145..c99b5538 100644 --- a/apps/note/models/transactions.py +++ b/apps/note/models/transactions.py @@ -5,6 +5,7 @@ from django.db import models from django.utils import timezone from django.utils.translation import gettext_lazy as _ from django.urls import reverse +from polymorphic.models import PolymorphicModel from .notes import Note, NoteClub @@ -77,27 +78,7 @@ class TransactionTemplate(models.Model): return reverse('note:template_update', args=(self.pk, )) -class TransactionType(models.Model): - """ - Defined a recurrent transaction category - - Example: food, softs, ... - """ - name = models.CharField( - verbose_name=_("name"), - max_length=31, - unique=True, - ) - - class Meta: - verbose_name = _("transaction type") - verbose_name_plural = _("transaction types") - - def __str__(self): - return str(self.name) - - -class Transaction(models.Model): +class Transaction(PolymorphicModel): """ General transaction between two :model:`note.Note` @@ -128,12 +109,6 @@ class Transaction(models.Model): default=1, ) amount = models.PositiveIntegerField(verbose_name=_('amount'), ) - transaction_type = models.ForeignKey( - TransactionType, - on_delete=models.PROTECT, - verbose_name=_('type'), - max_length=31, - ) reason = models.CharField( verbose_name=_('reason'), max_length=255, diff --git a/apps/note/views.py b/apps/note/views.py index c42c243f..75577a2e 100644 --- a/apps/note/views.py +++ b/apps/note/views.py @@ -8,7 +8,7 @@ from django.urls import reverse from django.utils.translation import gettext_lazy as _ from django.views.generic import CreateView, ListView, UpdateView -from .models import Transaction, TransactionTemplate, Alias +from .models import Transaction, TransactionTemplate, Alias, TemplateTransaction from .forms import TransactionForm, TransactionTemplateForm, ConsoForm @@ -129,7 +129,7 @@ class ConsoView(LoginRequiredMixin, CreateView): """ Consume """ - model = Transaction + model = TemplateTransaction template_name = "note/conso_form.html" form_class = ConsoForm @@ -152,3 +152,4 @@ class ConsoView(LoginRequiredMixin, CreateView): When clicking a button, reload the same page """ return reverse('note:consos') +