diff --git a/cotisations/forms.py b/cotisations/forms.py index 7285e5e5..d2a1e69e 100644 --- a/cotisations/forms.py +++ b/cotisations/forms.py @@ -1,6 +1,6 @@ from django import forms from django.forms import ModelForm -from .models import Article, Paiement, Facture +from .models import Article, Paiement, Facture, Banque class NewFactureForm(ModelForm): article = forms.ModelMultipleChoiceField(queryset=Article.objects.all(), label="Article") @@ -71,3 +71,18 @@ class DelPaiementForm(ModelForm): exclude = ['moyen'] model = Paiement +class BanqueForm(ModelForm): + class Meta: + model = Banque + fields = ['name'] + + def __init__(self, *args, **kwargs): + super(BanqueForm, self).__init__(*args, **kwargs) + self.fields['name'].label = 'Banque à ajouter' + +class DelBanqueForm(ModelForm): + banques = forms.ModelMultipleChoiceField(queryset=Banque.objects.all(), label="Banques actuelles", widget=forms.CheckboxSelectMultiple) + + class Meta: + exclude = ['name'] + model = Banque diff --git a/cotisations/templates/cotisations/sidebar.html b/cotisations/templates/cotisations/sidebar.html index 6f39d00f..be622d09 100644 --- a/cotisations/templates/cotisations/sidebar.html +++ b/cotisations/templates/cotisations/sidebar.html @@ -6,4 +6,6 @@

Retirer un article

Ajouter un moyen de paiement

Retirer un moyen de paiement

+

Ajouter une banque

+

Retirer une banque

{% endblock %} diff --git a/cotisations/urls.py b/cotisations/urls.py index 7589e5c1..fca9c331 100644 --- a/cotisations/urls.py +++ b/cotisations/urls.py @@ -9,6 +9,8 @@ urlpatterns = [ url(r'^del_article/$', views.del_article, name='del-article'), url(r'^add_paiement/$', views.add_paiement, name='add-paiement'), url(r'^del_paiement/$', views.del_paiement, name='del-paiement'), + url(r'^add_banque/$', views.add_banque, name='add-banque'), + url(r'^del_banque/$', views.del_banque, name='del-banque'), url(r'^$', views.index, name='index'), ] diff --git a/cotisations/views.py b/cotisations/views.py index 308eb588..fa72a46e 100644 --- a/cotisations/views.py +++ b/cotisations/views.py @@ -9,7 +9,7 @@ from django.contrib import messages from django.db.models import Max, ProtectedError from .models import Facture, Article, Cotisation, Article -from .forms import NewFactureForm, EditFactureForm, ArticleForm, DelArticleForm, PaiementForm, DelPaiementForm +from .forms import NewFactureForm, EditFactureForm, ArticleForm, DelArticleForm, PaiementForm, DelPaiementForm, BanqueForm, DelBanqueForm from users.models import User from dateutil.relativedelta import relativedelta @@ -120,6 +120,27 @@ def del_paiement(request): return redirect("/cotisations/") return form({'factureform': paiement}, 'cotisations/facture.html', request) +def add_banque(request): + banque = BanqueForm(request.POST or None) + if banque.is_valid(): + banque.save() + messages.success(request, "La banque a été ajoutée") + return redirect("/cotisations/") + return form({'factureform': banque}, 'cotisations/facture.html', request) + +def del_banque(request): + banque = DelBanqueForm(request.POST or None) + if banque.is_valid(): + banque_dels = banque.cleaned_data['banques'] + for banque_del in banque_dels: + try: + banque_del.delete() + messages.success(request, "La banque a été supprimée") + except ProtectedError: + messages.error(request, "La banque %s est affectée à au moins une facture, vous ne pouvez pas la supprimer" % banque_del) + return redirect("/cotisations/") + return form({'factureform': banque}, 'cotisations/facture.html', request) + def index(request): facture_list = Facture.objects.order_by('date').reverse() return render(request, 'cotisations/index.html', {'facture_list': facture_list})