8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-12-27 01:13:46 +00:00

Bouge les modelforms dans forms pour éviter les imports circulaires

This commit is contained in:
Gabriel Detraz 2016-07-06 21:23:05 +02:00
parent fc78790c59
commit 21f0631d3b
3 changed files with 75 additions and 71 deletions

73
cotisations/forms.py Normal file
View file

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

View file

@ -1,6 +1,4 @@
from django.db import models from django.db import models
from django import forms
from django.forms import ModelForm
class Facture(models.Model): class Facture(models.Model):
@ -46,71 +44,3 @@ class Cotisation(models.Model):
def __str__(self): def __str__(self):
return str(self.facture) 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

View file

@ -8,7 +8,8 @@ from django.template import Context, RequestContext, loader
from django.contrib import messages from django.contrib import messages
from django.db.models import Max, ProtectedError 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 users.models import User
from dateutil.relativedelta import relativedelta from dateutil.relativedelta import relativedelta