From c4a60633f805f7921ece5c1305c348cc0f1f8441 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Tue, 4 Feb 2020 01:18:03 +0100 Subject: [PATCH] Consos --- apps/member/models.py | 3 +++ apps/note/admin.py | 11 ++++++++- apps/note/forms.py | 15 +++++++++++- apps/note/models/__init__.py | 4 ++-- apps/note/models/transactions.py | 24 +++++++++++++++++-- apps/note/urls.py | 4 +++- apps/note/views.py | 34 +++++++++++++++++++++++--- templates/base.html | 2 +- templates/note/conso_form.html | 41 ++++++++++++++++++++++++++++++++ 9 files changed, 127 insertions(+), 11 deletions(-) create mode 100644 templates/note/conso_form.html diff --git a/apps/member/models.py b/apps/member/models.py index 883f9b49..35b7027c 100644 --- a/apps/member/models.py +++ b/apps/member/models.py @@ -118,6 +118,9 @@ class Role(models.Model): verbose_name = _('role') verbose_name_plural = _('roles') + def __str__(self): + return str(self.name) + class Membership(models.Model): """ diff --git a/apps/note/admin.py b/apps/note/admin.py index 298b91c0..0b2461d0 100644 --- a/apps/note/admin.py +++ b/apps/note/admin.py @@ -8,7 +8,7 @@ from polymorphic.admin import PolymorphicChildModelAdmin, \ PolymorphicChildModelFilter, PolymorphicParentModelAdmin from .models.notes import Alias, Note, NoteClub, NoteSpecial, NoteUser -from .models.transactions import Transaction, TransactionTemplate +from .models.transactions import Transaction, TransactionCategory, TransactionTemplate class AliasInlines(admin.TabularInline): @@ -146,3 +146,12 @@ class TransactionTemplateAdmin(admin.ModelAdmin): return str(obj.destination) poly_destination.short_description = _('destination') + + +@admin.register(TransactionCategory) +class TransactionCategoryAdmin(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 d74fa5b4..d861345b 100644 --- a/apps/note/forms.py +++ b/apps/note/forms.py @@ -1,9 +1,22 @@ #!/usr/bin/env python from django import forms -from .models import TransactionTemplate +from .models import TransactionTemplate, Transaction class TransactionTemplateForm(forms.ModelForm): class Meta: model = TransactionTemplate fields ='__all__' + +class ConsoForm(forms.ModelForm): + def save(self, commit=True): + button: TransactionTemplate = TransactionTemplate.objects.filter(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 + super().save(commit) + + class Meta: + model = Transaction + fields = ('source',) diff --git a/apps/note/models/__init__.py b/apps/note/models/__init__.py index b00572ce..ee290bb5 100644 --- a/apps/note/models/__init__.py +++ b/apps/note/models/__init__.py @@ -4,11 +4,11 @@ from .notes import Alias, Note, NoteClub, NoteSpecial, NoteUser from .transactions import MembershipTransaction, Transaction, \ - TransactionTemplate + TransactionCategory, TransactionTemplate __all__ = [ # Notes 'Alias', 'Note', 'NoteClub', 'NoteSpecial', 'NoteUser', # Transactions - 'MembershipTransaction', 'Transaction', 'TransactionTemplate', + 'MembershipTransaction', 'Transaction', 'TransactionCategory', 'TransactionTemplate', ] diff --git a/apps/note/models/transactions.py b/apps/note/models/transactions.py index 4ce23311..5c242255 100644 --- a/apps/note/models/transactions.py +++ b/apps/note/models/transactions.py @@ -13,10 +13,28 @@ from .notes import Note,NoteClub Defines transactions """ +class TransactionCategory(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 category") + verbose_name_plural = _("transaction categories") + + def __str__(self): + return str(self.name) class TransactionTemplate(models.Model): """ - Defined a reccurent transaction + Defined a recurrent transaction associated to selling something (a burger, a beer, ...) """ @@ -35,7 +53,9 @@ class TransactionTemplate(models.Model): verbose_name=_('amount'), help_text=_('in centimes'), ) - template_type = models.CharField( + template_type = models.ForeignKey( + TransactionCategory, + on_delete=models.PROTECT, verbose_name=_('type'), max_length=31 ) diff --git a/apps/note/urls.py b/apps/note/urls.py index 5e423d46..59bb4672 100644 --- a/apps/note/urls.py +++ b/apps/note/urls.py @@ -11,5 +11,7 @@ urlpatterns = [ path('transfer/', views.TransactionCreate.as_view(), name='transfer'), path('buttons/create/',views.TransactionTemplateCreateView.as_view(),name='template_create'), path('buttons/update//',views.TransactionTemplateUpdateView.as_view(),name='template_update'), - path('buttons/',views.TransactionTemplateListView.as_view(),name='template_list') + path('buttons/',views.TransactionTemplateListView.as_view(),name='template_list'), + path('consos//',views.ConsoView.as_view(),name='consos'), + path('consos/',views.ConsoView.as_view(),name='consos'), ] diff --git a/apps/note/views.py b/apps/note/views.py index 08f4f630..ce27832c 100644 --- a/apps/note/views.py +++ b/apps/note/views.py @@ -3,11 +3,12 @@ # SPDX-License-Identifier: GPL-3.0-or-later from django.contrib.auth.mixins import LoginRequiredMixin +from django.urls import reverse_lazy, reverse from django.utils.translation import gettext_lazy as _ from django.views.generic import CreateView, ListView, DetailView, UpdateView -from .models import Transaction,TransactionTemplate -from .forms import TransactionTemplateForm +from .models import Transaction,TransactionCategory,TransactionTemplate +from .forms import TransactionTemplateForm, ConsoForm class TransactionCreate(LoginRequiredMixin, CreateView): """ @@ -45,4 +46,31 @@ class TransactionTemplateUpdateView(LoginRequiredMixin,UpdateView): """ """ model = TransactionTemplate - form_class=TransactionTemplateForm + form_class = TransactionTemplateForm + +class ConsoView(LoginRequiredMixin,CreateView): + """ + Consume + """ + model = Transaction + template_name = "note/conso_form.html" + form_class = ConsoForm + + def get_context_data(self, **kwargs): + """ + Add some context variables in template such as page title + """ + context = super().get_context_data(**kwargs) + context['template_types'] = TransactionCategory.objects.all() + + if 'template_type' not in self.kwargs.keys(): + return context + + template_type = TransactionCategory.objects.filter(name=self.kwargs.get('template_type')).get() + context['buttons'] = TransactionTemplate.objects.filter(template_type=template_type) + context['title'] = template_type + + return context + + def get_success_url(self): + return reverse('note:consos',args=(self.kwargs.get('template_type'),)) diff --git a/templates/base.html b/templates/base.html index 5c857593..96f56881 100644 --- a/templates/base.html +++ b/templates/base.html @@ -46,7 +46,7 @@ SPDX-License-Identifier: GPL-3.0-or-later