From 21f0631d3b0fda75d311f1d878c05e170e6ed49d Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Wed, 6 Jul 2016 21:23:05 +0200 Subject: [PATCH] =?UTF-8?q?Bouge=20les=20modelforms=20dans=20forms=20pour?= =?UTF-8?q?=20=C3=A9viter=20les=20imports=20circulaires?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cotisations/forms.py | 73 +++++++++++++++++++++++++++++++++++++++++++ cotisations/models.py | 70 ----------------------------------------- cotisations/views.py | 3 +- 3 files changed, 75 insertions(+), 71 deletions(-) create mode 100644 cotisations/forms.py diff --git a/cotisations/forms.py b/cotisations/forms.py new file mode 100644 index 00000000..7285e5e5 --- /dev/null +++ b/cotisations/forms.py @@ -0,0 +1,73 @@ +from django import forms +from django.forms import ModelForm +from .models import Article, Paiement, Facture + +class NewFactureForm(ModelForm): + article = forms.ModelMultipleChoiceField(queryset=Article.objects.all(), label="Article") + + def __init__(self, *args, **kwargs): + super(NewFactureForm, self).__init__(*args, **kwargs) + self.fields['number'].label = 'Quantité' + self.fields['cheque'].required = False + self.fields['banque'].required = False + self.fields['cheque'].label = 'Numero de chèque' + self.fields['banque'].empty_label = "Non renseigné" + self.fields['paiement'].empty_label = "Séléctionner un moyen de paiement" + + class Meta: + model = Facture + fields = ['paiement','banque','cheque','number'] + + def clean(self): + cleaned_data=super(NewFactureForm, self).clean() + paiement = cleaned_data.get("paiement") + cheque = cleaned_data.get("cheque") + banque = cleaned_data.get("banque") + if paiement.moyen=="chèque" and not (cheque and banque): + raise forms.ValidationError("Le numero de chèque et la banque sont obligatoires") + return cleaned_data + +class EditFactureForm(NewFactureForm): + class Meta(NewFactureForm.Meta): + fields = '__all__' + + def __init__(self, *args, **kwargs): + super(EditFactureForm, self).__init__(*args, **kwargs) + self.fields['user'].label = 'Adherent' + self.fields['name'].label = 'Designation' + 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/models.py b/cotisations/models.py index a8d3a3bf..db064d7f 100644 --- a/cotisations/models.py +++ b/cotisations/models.py @@ -1,6 +1,4 @@ from django.db import models -from django import forms -from django.forms import ModelForm class Facture(models.Model): @@ -46,71 +44,3 @@ class Cotisation(models.Model): def __str__(self): return str(self.facture) -class NewFactureForm(ModelForm): - article = forms.ModelMultipleChoiceField(queryset=Article.objects.all(), label="Article") - - def __init__(self, *args, **kwargs): - super(NewFactureForm, self).__init__(*args, **kwargs) - self.fields['number'].label = 'Quantité' - self.fields['cheque'].required = False - self.fields['banque'].required = False - self.fields['cheque'].label = 'Numero de chèque' - self.fields['banque'].empty_label = "Non renseigné" - self.fields['paiement'].empty_label = "Séléctionner un moyen de paiement" - - class Meta: - model = Facture - fields = ['paiement','banque','cheque','number'] - - def clean(self): - cleaned_data=super(NewFactureForm, self).clean() - paiement = cleaned_data.get("paiement") - cheque = cleaned_data.get("cheque") - banque = cleaned_data.get("banque") - if paiement.moyen=="chèque" and not (cheque and banque): - raise forms.ValidationError("Le numero de chèque et la banque sont obligatoires") - return cleaned_data - -class EditFactureForm(NewFactureForm): - class Meta(NewFactureForm.Meta): - fields = '__all__' - - def __init__(self, *args, **kwargs): - super(EditFactureForm, self).__init__(*args, **kwargs) - self.fields['user'].label = 'Adherent' - self.fields['name'].label = 'Designation' - 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/views.py b/cotisations/views.py index c1e545fd..3d8847f6 100644 --- a/cotisations/views.py +++ b/cotisations/views.py @@ -8,7 +8,8 @@ from django.template import Context, RequestContext, loader from django.contrib import messages from django.db.models import Max, ProtectedError -from cotisations.models import NewFactureForm, EditFactureForm, Facture, Article, Cotisation, Article, ArticleForm, DelArticleForm, Paiement, PaiementForm, DelPaiementForm +from .models import Facture, Article, Cotisation, Article +from .forms import NewFactureForm, EditFactureForm, ArticleForm, DelArticleForm, DelPaiementForm from users.models import User from dateutil.relativedelta import relativedelta