Merge branch 'polymorphic_transaction' into 'import_nk15'

Polymorphic transaction

See merge request bde/nk20!20
This commit is contained in:
Pierre-antoine Comby 2020-02-24 10:36:04 +01:00
commit 277f4847d9
5 changed files with 20 additions and 48 deletions

View File

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

View File

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

View File

@ -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',
]

View File

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

View File

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