diff --git a/cotisations/forms.py b/cotisations/forms.py index d73ad6a3..a9f47f28 100644 --- a/cotisations/forms.py +++ b/cotisations/forms.py @@ -131,6 +131,14 @@ class SelectUserArticleForm( required=True ) + def __init__(self, *args, **kwargs): + self_subscription = kwargs.pop('is_self_subscription', False) + super(SelectUserArticleForm, self).__init__(*args, **kwargs) + if self_subscription: + self.fields['article'].queryset = Article.objects.filter( + Q(type_user='All') | Q(type_user='Adherent') + ).filter(allow_self_subscription=True) + class SelectClubArticleForm(Form): """ @@ -150,6 +158,14 @@ class SelectClubArticleForm(Form): required=True ) + def __init__(self, *args, **kwargs): + self_subscription = kwargs.pop('is_self_subscription', False) + super(SelectClubArticleForm, self).__init__(*args, **kwargs) + if self_subscription: + self.fields['article'].queryset = Article.objects.filter( + Q(type_user='All') | Q(type_user='Club') + ).filter(allow_self_subscription=True) + # TODO : change Facture to Invoice class NewFactureFormPdf(Form): diff --git a/cotisations/migrations/0031_article_allow_self_subscription.py b/cotisations/migrations/0031_article_allow_self_subscription.py new file mode 100644 index 00000000..64764edf --- /dev/null +++ b/cotisations/migrations/0031_article_allow_self_subscription.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2018-06-17 17:13 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('cotisations', '0030_paiement_allow_self_subscription'), + ] + + operations = [ + migrations.AddField( + model_name='article', + name='allow_self_subscription', + field=models.BooleanField(default=False, verbose_name='Is available for self subscription'), + ), + ] diff --git a/cotisations/models.py b/cotisations/models.py index 5c3a310a..fe741d8f 100644 --- a/cotisations/models.py +++ b/cotisations/models.py @@ -518,6 +518,10 @@ class Article(RevMixin, AclMixin, models.Model): max_length=255, verbose_name=_l("Type of cotisation") ) + allow_self_subscription = models.BooleanField( + default=False, + verbose_name=_l("Is available for self subscription") + ) unique_together = ('name', 'type_user') diff --git a/cotisations/views.py b/cotisations/views.py index 1b8fc80b..9d2b60a4 100644 --- a/cotisations/views.py +++ b/cotisations/views.py @@ -99,18 +99,32 @@ def new_facture(request, user, userid): Q(type_user='All') | Q(type_user=request.user.class_name) ) # Building the invocie form and the article formset + is_self_subscription = False if not request.user.has_perm('cotisations.add_facture') and OptionalUser.get_cached_value('allow_self_subscription'): + is_self_subscription = True + article_list = article_list.filter(allow_self_subscription=True) allowed_payment = Paiement.objects.filter(allow_self_subscription=True) invoice_form = NewFactureForm(request.POST or None, instance=invoice, allowed_payment=allowed_payment) + elif not OptionalUser.get_cached_value('allow_self_subscription'): + messages.error( + request, + _("You cannot subscribe. Please ask to the staff.") + ) + return redirect(reverse( + 'users:profil', + kwargs={'userid': userid} + )) else: invoice_form = NewFactureForm(request.POST or None, instance=invoice) if request.user.is_class_club: article_formset = formset_factory(SelectClubArticleForm)( - request.POST or None + request.POST or None, + form_kwargs={'is_self_subscription':is_self_subscription} ) else: article_formset = formset_factory(SelectUserArticleForm)( - request.POST or None + request.POST or None, + form_kwargs={'is_self_subscription':is_self_subscription} ) if invoice_form.is_valid() and article_formset.is_valid():