8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-22 19:33:11 +00:00

Correction du problème de vente d'articles + simplification views et forms

This commit is contained in:
Gabriel Detraz 2018-08-26 17:13:29 +02:00 committed by chirac
parent 48be064b94
commit 3eda283f64
3 changed files with 30 additions and 56 deletions

View file

@ -84,15 +84,13 @@ class FactureForm(FieldPermissionFormMixin, FormRevMixin, ModelForm):
return cleaned_data return cleaned_data
class SelectUserArticleForm(FormRevMixin, Form): class SelectArticleForm(FormRevMixin, Form):
""" """
Form used to select an article during the creation of an invoice for a Form used to select an article during the creation of an invoice for a
member. member.
""" """
article = forms.ModelChoiceField( article = forms.ModelChoiceField(
queryset=Article.objects.filter( queryset=Article.objects.none(),
Q(type_user='All') | Q(type_user='Adherent')
),
label=_("Article"), label=_("Article"),
required=True required=True
) )
@ -104,31 +102,9 @@ class SelectUserArticleForm(FormRevMixin, Form):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
user = kwargs.pop('user') user = kwargs.pop('user')
super(SelectUserArticleForm, self).__init__(*args, **kwargs) target_user = kwargs.pop('target_user')
self.fields['article'].queryset = Article.find_allowed_articles(user) super(SelectArticleForm, self).__init__(*args, **kwargs)
self.fields['article'].queryset = Article.find_allowed_articles(user, target_user)
class SelectClubArticleForm(Form):
"""
Form used to select an article during the creation of an invoice for a
club.
"""
article = forms.ModelChoiceField(
queryset=Article.objects.filter(
Q(type_user='All') | Q(type_user='Club')
),
label=_("Article"),
required=True
)
quantity = forms.IntegerField(
label=_("Quantity"),
validators=[MinValueValidator(1)],
required=True
)
def __init__(self, user, *args, **kwargs):
super(SelectClubArticleForm, self).__init__(*args, **kwargs)
self.fields['article'].queryset = Article.find_allowed_articles(user)
class CustomInvoiceForm(FormRevMixin, ModelForm): class CustomInvoiceForm(FormRevMixin, ModelForm):

View file

@ -222,7 +222,7 @@ class Facture(BaseInvoice):
return True, None return True, None
if len(Paiement.find_allowed_payments(user_request)) <= 0: if len(Paiement.find_allowed_payments(user_request)) <= 0:
return False, _("There are no payment method which you can use.") return False, _("There are no payment method which you can use.")
if len(Article.find_allowed_articles(user_request)) <= 0: if len(Article.find_allowed_articles(user_request, user_request)) <= 0:
return False, _("There are no article that you can buy.") return False, _("There are no article that you can buy.")
return True, None return True, None
@ -595,15 +595,24 @@ class Article(RevMixin, AclMixin, models.Model):
) )
@classmethod @classmethod
def find_allowed_articles(cls, user): def find_allowed_articles(cls, user, target_user):
"""Finds every allowed articles for an user. """Finds every allowed articles for an user, on a target user.
Args: Args:
user: The user requesting articles. user: The user requesting articles.
target_user: The user to sell articles
""" """
if target_user.is_class_club:
objects_pool = cls.objects.filter(
Q(type_user='All') | Q(type_user='Club')
)
else:
objects_pool = cls.objects.filter(
Q(type_user='All') | Q(type_user='Adherent')
)
if user.has_perm('cotisations.buy_every_article'): if user.has_perm('cotisations.buy_every_article'):
return cls.objects.all() return objects_pool
return cls.objects.filter(available_for_everyone=True) return objects_pool.filter(available_for_everyone=True)
class Banque(RevMixin, AclMixin, models.Model): class Banque(RevMixin, AclMixin, models.Model):

View file

@ -75,8 +75,7 @@ from .forms import (
DelPaiementForm, DelPaiementForm,
BanqueForm, BanqueForm,
DelBanqueForm, DelBanqueForm,
SelectUserArticleForm, SelectArticleForm,
SelectClubArticleForm,
RechargeForm, RechargeForm,
CustomInvoiceForm CustomInvoiceForm
) )
@ -110,15 +109,9 @@ def new_facture(request, user, userid):
creation=True creation=True
) )
if request.user.is_class_club: article_formset = formset_factory(SelectArticleForm)(
article_formset = formset_factory(SelectClubArticleForm)(
request.POST or None, request.POST or None,
form_kwargs={'user': request.user} form_kwargs={'user': request.user, 'target_user': user}
)
else:
article_formset = formset_factory(SelectUserArticleForm)(
request.POST or None,
form_kwargs={'user': request.user}
) )
if invoice_form.is_valid() and article_formset.is_valid(): if invoice_form.is_valid() and article_formset.is_valid():
@ -199,16 +192,12 @@ def new_custom_invoice(request):
) )
# Building the invocie form and the article formset # Building the invocie form and the article formset
invoice_form = CustomInvoiceForm(request.POST or None) invoice_form = CustomInvoiceForm(request.POST or None)
if request.user.is_class_club:
articles_formset = formset_factory(SelectClubArticleForm)( article_formset = formset_factory(SelectArticleForm)(
request.POST or None, request.POST or None,
form_kwargs={'user': request.user} form_kwargs={'user': request.user, 'target_user': user}
)
else:
articles_formset = formset_factory(SelectUserArticleForm)(
request.POST or None,
form_kwargs={'user': request.user}
) )
if invoice_form.is_valid() and articles_formset.is_valid(): if invoice_form.is_valid() and articles_formset.is_valid():
new_invoice_instance = invoice_form.save() new_invoice_instance = invoice_form.save()
for art_item in articles_formset: for art_item in articles_formset: