From fc78790c59e8d979c873a507ba1f078866baaa8e Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Wed, 6 Jul 2016 20:57:31 +0200 Subject: [PATCH] =?UTF-8?q?Vues=20pour=20g=C3=A9rer=20la=20liste=20des=20a?= =?UTF-8?q?rticles?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cotisations/models.py | 32 ++++++++++++++++++++++++++++++++ cotisations/urls.py | 2 ++ cotisations/views.py | 21 +++++++++++++++++++-- 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/cotisations/models.py b/cotisations/models.py index a7e89292..a8d3a3bf 100644 --- a/cotisations/models.py +++ b/cotisations/models.py @@ -82,3 +82,35 @@ class EditFactureForm(NewFactureForm): self.fields['prix'].label = 'Prix unitaire' self.fields['user'].empty_label = "Séléctionner l'adhérent propriétaire" self.fields.pop('article') + +class ArticleForm(ModelForm): + class Meta: + model = Article + fields = '__all__' + + def __init__(self, *args, **kwargs): + super(ArticleForm, self).__init__(*args, **kwargs) + self.fields['name'].label = "Désignation de l'article" + +class DelArticleForm(ModelForm): + articles = forms.ModelMultipleChoiceField(queryset=Article.objects.all(), label="Articles actuels", widget=forms.CheckboxSelectMultiple) + + class Meta: + fields = ['articles'] + model = Article + +class PaiementForm(ModelForm): + class Meta: + model = Paiement + fields = ['moyen'] + + def __init__(self, *args, **kwargs): + super(PaiementForm, self).__init__(*args, **kwargs) + self.fields['moyen'].label = 'Moyen de paiement à ajouter' + +class DelPaiementForm(ModelForm): + paiements = forms.ModelMultipleChoiceField(queryset=Paiement.objects.all(), label="Moyens de paiement actuels", widget=forms.CheckboxSelectMultiple) + + class Meta: + exclude = ['moyen'] + model = Paiement diff --git a/cotisations/urls.py b/cotisations/urls.py index 3c0bbdf7..9e5e02a2 100644 --- a/cotisations/urls.py +++ b/cotisations/urls.py @@ -5,6 +5,8 @@ from . import views urlpatterns = [ url(r'^new_facture/(?P[0-9]+)$', views.new_facture, name='new-facture'), url(r'^edit_facture/(?P[0-9]+)$', views.edit_facture, name='edit-facture'), + url(r'^add_article/$', views.add_article, name='add-article'), + url(r'^del_article/$', views.del_article, name='del-article'), url(r'^$', views.index, name='index'), ] diff --git a/cotisations/views.py b/cotisations/views.py index d7d531e0..c1e545fd 100644 --- a/cotisations/views.py +++ b/cotisations/views.py @@ -6,9 +6,9 @@ from django.shortcuts import render_to_response, get_object_or_404 from django.core.context_processors import csrf from django.template import Context, RequestContext, loader from django.contrib import messages -from django.db.models import Max +from django.db.models import Max, ProtectedError -from cotisations.models import NewFactureForm, EditFactureForm, Facture, Article, Cotisation +from cotisations.models import NewFactureForm, EditFactureForm, Facture, Article, Cotisation, Article, ArticleForm, DelArticleForm, Paiement, PaiementForm, DelPaiementForm from users.models import User from dateutil.relativedelta import relativedelta @@ -81,6 +81,23 @@ def edit_facture(request, factureid): return redirect("/cotisations/") return form({'factureform': facture_form}, 'cotisations/facture.html', request) +def add_article(request): + article = ArticleForm(request.POST or None) + if article.is_valid(): + article.save() + messages.success(request, "L'article a été ajouté") + return redirect("/cotisations/") + return form({'factureform': article}, 'cotisations/facture.html', request) + +def del_article(request): + article = DelArticleForm(request.POST or None) + if article.is_valid(): + article_del = article.cleaned_data['articles'] + article_del.delete() + messages.success(request, "Le/les articles ont été supprimé") + return redirect("/cotisations/") + return form({'factureform': article}, '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})