From 161ce72042445d0e2f51ad2c19c1d2499a1ea216 Mon Sep 17 00:00:00 2001 From: Hugo LEVY-FALK Date: Fri, 13 Jul 2018 16:14:08 +0200 Subject: [PATCH] =?UTF-8?q?Optimisation=20des=20requ=C3=AAtes=20pour=20obt?= =?UTF-8?q?enir=20les=20paiements=20et=20articles=20disponibles.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cotisations/forms.py | 24 +++++++----------------- cotisations/models.py | 16 ++++++++++++++-- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/cotisations/forms.py b/cotisations/forms.py index e1dd9e28..5d432849 100644 --- a/cotisations/forms.py +++ b/cotisations/forms.py @@ -60,9 +60,7 @@ class FactureForm(FieldPermissionFormMixin, FormRevMixin, ModelForm): super(FactureForm, self).__init__(*args, prefix=prefix, **kwargs) self.fields['paiement'].empty_label = \ _("Select a payment method") - self.fields['paiement'].queryset = Paiement.objects.filter( - pk__in=map(lambda x: x.pk, Paiement.find_allowed_payments(user)) - ) + self.fields['paiement'].queryset = Paiement.find_allowed_payments(user) if not creation: self.fields['user'].label = _("Member") self.fields['user'].empty_label = \ @@ -106,9 +104,7 @@ class SelectUserArticleForm(FormRevMixin, Form): def __init__(self, *args, **kwargs): user = kwargs.pop('user') super(SelectUserArticleForm, self).__init__(*args, **kwargs) - self.fields['article'].queryset = Article.objects.filter( - pk__in=map(lambda x: x.pk, Article.find_allowed_articles(user)) - ) + self.fields['article'].queryset = Article.find_allowed_articles(user) class SelectClubArticleForm(Form): @@ -129,12 +125,9 @@ class SelectClubArticleForm(Form): required=True ) - def __init__(self, *args, **kwargs): - user = kwargs.pop('user') + def __init__(self, user, *args, **kwargs): super(SelectClubArticleForm, self).__init__(*args, **kwargs) - self.fields['article'].queryset = Article.objects.filter( - pk__in=map(lambda x: x.pk, Article.find_allowed_articles(user)) - ) + self.fields['article'].queryset = Article.find_allowed_articles(user) # TODO : change Facture to Invoice @@ -284,15 +277,12 @@ class RechargeForm(FormRevMixin, Form): label=_l("Payment method") ) - def __init__(self, *args, **kwargs): - self.user = kwargs.pop('user') + def __init__(self, *args, user=None, **kwargs): + self.user = user super(RechargeForm, self).__init__(*args, **kwargs) self.fields['payment'].empty_label = \ _("Select a payment method") - self.fields['payment'].queryset = Paiement.objects.filter( - pk__in=map(lambda x: x.pk, - Paiement.find_allowed_payments(self.user)) - ) + self.fields['payment'].queryset = Paiement.find_allowed_payments(user) def clean_value(self): """ diff --git a/cotisations/models.py b/cotisations/models.py index 5cd30e5d..5b20c055 100644 --- a/cotisations/models.py +++ b/cotisations/models.py @@ -567,7 +567,13 @@ class Article(RevMixin, AclMixin, models.Model): @classmethod def find_allowed_articles(cls, user): - return [p for p in cls.objects.all() if p.can_buy_article(user)[0]] + """Finds every allowed articles for an user. + + :param user: The user requesting articles. + """ + if user.has_perm('cotisations.buy_every_article'): + return cls.objects.all() + return cls.objects.filter(available_for_everyone=True) class Banque(RevMixin, AclMixin, models.Model): @@ -726,7 +732,13 @@ class Paiement(RevMixin, AclMixin, models.Model): @classmethod def find_allowed_payments(cls, user): - return [p for p in cls.objects.all() if p.can_use_payment(user)[0]] + """Finds every allowed payments for an user. + + :param user: The user requesting payment methods. + """ + if user.has_perm('cotisations.use_every_payment'): + return cls.objects.all() + return cls.objects.filter(available_for_everyone=True) class Cotisation(RevMixin, AclMixin, models.Model):