Commit 161ce720 authored by Hugo LEVY-FALK's avatar Hugo LEVY-FALK

Optimisation des requêtes pour obtenir les paiements et articles disponibles.

parent 78b950c3
......@@ -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):
"""
......
......@@ -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):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment