This commit is contained in:
Yohann D'ANELLO 2020-02-04 01:18:03 +01:00
parent f02d1ab41c
commit c4a60633f8
9 changed files with 127 additions and 11 deletions

View File

@ -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):
"""

View File

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

View File

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

View File

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

View File

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

View File

@ -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/<int:pk>/',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/<str:template_type>/',views.ConsoView.as_view(),name='consos'),
path('consos/',views.ConsoView.as_view(),name='consos'),
]

View File

@ -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):
"""
@ -46,3 +47,30 @@ class TransactionTemplateUpdateView(LoginRequiredMixin,UpdateView):
"""
model = TransactionTemplate
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'),))

View File

@ -46,7 +46,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
<div class="collapse navbar-collapse" id="navbarNavDropdown">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="#"><i class="fa fa-coffee"></i> Consos</a>
<a class="nav-link" href="{% url 'note:consos' %}"><i class="fa fa-coffee"></i> Consos</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="{% url 'member:club_list' %}"><i class="fa fa-users"></i> Clubs</a>

View File

@ -0,0 +1,41 @@
{% extends "base.html" %}
{% load i18n static pretty_money %}
{% block content %}
<fieldset class="module aligned">
{% for type in template_types %}
<a href="{% url 'note:consos' template_type=type %}"><button>{{ type }}</button></a>
{% endfor %}
</fieldset>
<form method="post" onsubmit="window.onbeforeunload=null">{% csrf_token %}
{% if form.non_field_errors %}
<p class="errornote">
{% for error in form.non_field_errors %}
{{ error }}
{% endfor %}
</p>
{% endif %}
<fieldset class="module aligned">
{% for field in form %}
<div class="form-row{% if field.errors %} errors{% endif %}">
{{ field.errors }}
<div>
{{ field.label_tag }}
{% if field.is_readonly %}
<div class="readonly">{{ field.contents }}</div>
{% else %}
{{ field }}
{% endif %}
{% if field.field.help_text %}
<div class="help">{{ field.field.help_text|safe }}</div>
{% endif %}
</div>
</div>
{% endfor %}
{% for button in buttons %}
<button name="button" value="{{ button.name }}">{{ button.name }} ({{ button.amount | pretty_money }})</button>
{% endfor %}
</fieldset>
</form>
{% endblock %}