From 9dd43d0c6fd9df72341d72c9c2a0a632315913e2 Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Sat, 28 Oct 2017 06:02:53 +0200 Subject: [PATCH] Adpatation des articles en fonction du type user --- cotisations/forms.py | 19 +++++++++++++++++-- cotisations/views.py | 29 +++++++++++++++++++++++------ 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/cotisations/forms.py b/cotisations/forms.py index 76a67975..354da1f1 100644 --- a/cotisations/forms.py +++ b/cotisations/forms.py @@ -38,6 +38,7 @@ ArticleForm, BanqueForm, PaiementForm permettent aux admin d'ajouter, from __future__ import unicode_literals from django import forms +from django.db.models import Q from django.forms import ModelForm, Form from django.core.validators import MinValueValidator from .models import Article, Paiement, Facture, Banque @@ -90,10 +91,24 @@ class CreditSoldeForm(NewFactureForm): montant = forms.DecimalField(max_digits=5, decimal_places=2, required=True) -class SelectArticleForm(Form): +class SelectUserArticleForm(Form): """Selection d'un article lors de la creation d'une facture""" article = forms.ModelChoiceField( - queryset=Article.objects.all(), + queryset=Article.objects.filter(Q(type_user='All') | Q(type_user='Adherent')), + label="Article", + required=True + ) + quantity = forms.IntegerField( + label="Quantité", + validators=[MinValueValidator(1)], + required=True + ) + + +class SelectClubArticleForm(Form): + """Selection d'un article lors de la creation d'une facture""" + article = forms.ModelChoiceField( + queryset=Article.objects.filter(Q(type_user='All') | Q(type_user='Club')), label="Article", required=True ) diff --git a/cotisations/views.py b/cotisations/views.py index 2331e34b..943367ae 100644 --- a/cotisations/views.py +++ b/cotisations/views.py @@ -31,6 +31,7 @@ from django.contrib.auth.decorators import login_required, permission_required from django.contrib import messages from django.db.models import ProtectedError from django.db import transaction +from django.db.models import Q from django.forms import modelformset_factory, formset_factory from django.utils import timezone from reversion import revisions as reversion @@ -43,10 +44,21 @@ from re2o.views import form from re2o.utils import SortTable from preferences.models import OptionalUser, AssoOption, GeneralOption from .models import Facture, Article, Vente, Paiement, Banque -from .forms import NewFactureForm, TrezEditFactureForm, EditFactureForm -from .forms import ArticleForm, DelArticleForm, PaiementForm, DelPaiementForm -from .forms import BanqueForm, DelBanqueForm, NewFactureFormPdf -from .forms import SelectArticleForm, CreditSoldeForm +from .forms import ( + NewFactureForm, + TrezEditFactureForm, + EditFactureForm, + ArticleForm, + DelArticleForm, + PaiementForm, + DelPaiementForm, + BanqueForm, + DelBanqueForm, + NewFactureFormPdf, + SelectUserArticleForm, + SelectClubArticleForm, + CreditSoldeForm +) from .tex import render_tex @@ -67,10 +79,15 @@ def new_facture(request, userid): return redirect("/cotisations/") facture = Facture(user=user) # Le template a besoin de connaitre les articles pour le js - article_list = Article.objects.all() + article_list = Article.objects.filter( + Q(type_user='All') | Q(type_user=request.user.class_name) + ) # On envoie la form fature et un formset d'articles facture_form = NewFactureForm(request.POST or None, instance=facture) - article_formset = formset_factory(SelectArticleForm)(request.POST or None) + if request.user.is_class_club: + article_formset = formset_factory(SelectClubArticleForm)(request.POST or None) + else: + article_formset = formset_factory(SelectUserArticleForm)(request.POST or None) if facture_form.is_valid() and article_formset.is_valid(): new_facture_instance = facture_form.save(commit=False) articles = article_formset