diff --git a/portail/forms.py b/portail/forms.py index f144d756..27769c01 100644 --- a/portail/forms.py +++ b/portail/forms.py @@ -21,7 +21,10 @@ from django import forms from django.contrib.auth.forms import UserCreationForm +from django.db.models import Q from django.utils.translation import gettext_lazy as _ + +from cotisations.models import Article, Paiement from re2o.widgets import AutocompleteModelWidget from users.models import Adherent @@ -49,3 +52,15 @@ class AdherentForm(UserCreationForm): }, ), } + + +class MembershipForm(forms.Form): + payment_method = forms.ModelChoiceField( + Paiement.objects.filter(available_for_everyone=True), + ) + + article = forms.ModelChoiceField( + Article.objects.filter(Q(duration_connection__gt=0) | Q(duration_days_connection__gt=0), + available_for_everyone=True, + need_membership=False), + ) diff --git a/portail/templates/portail/signup.html b/portail/templates/portail/signup.html index db939352..76925715 100644 --- a/portail/templates/portail/signup.html +++ b/portail/templates/portail/signup.html @@ -11,6 +11,7 @@
{% csrf_token %} {% bootstrap_form form %} + {% bootstrap_form membership_form %} {% trans "Confirm" as tr_confirm %} {% bootstrap_button tr_confirm button_type="submit" icon='ok' button_class='btn-success' %}
diff --git a/portail/views.py b/portail/views.py index f81b2fe2..0710c0c2 100644 --- a/portail/views.py +++ b/portail/views.py @@ -19,12 +19,14 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +from cotisations.models import Facture, Vente +from cotisations.utils import find_payment_method from django.contrib.auth import login from django.db import transaction from django.urls import reverse_lazy from django.views.generic import CreateView -from .forms import AdherentForm +from .forms import AdherentForm, MembershipForm class SignUpView(CreateView): @@ -33,13 +35,51 @@ class SignUpView(CreateView): def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) + context["membership_form"] = MembershipForm(self.request.POST or None) return context @transaction.atomic def form_valid(self, form): - ret = super().form_valid(form) + membership_form = MembershipForm(self.request.POST or None) + + if not membership_form.is_valid(): + return self.form_invalid(form) + + form.save() + + user = form.instance login(self.request, form.instance) - return ret + + payment_method = membership_form.cleaned_data["payment_method"] + article = membership_form.cleaned_data["article"] + + true_payment_method = find_payment_method(payment_method) + if hasattr(true_payment_method, "check_price"): + price_ok, msg = true_payment_method.check_price(article.prix, user) + if not price_ok: + membership_form.add_error(None, msg) + return self.form_invalid(membership_form) + + invoice = Facture.objects.create( + user=user, + paiement=payment_method, + ) + + Vente.objects.create( + facture=invoice, + name=article.name, + prix=article.prix, + duration_connection=article.duration_connection, + duration_days_connection=article.duration_days_connection, + duration_membership=article.duration_membership, + duration_days_membership=article.duration_days_membership, + number=1, + ) + + super().form_valid(form) + + # POOP CODE, pliz Re2o + return payment_method.end_payment(invoice, self.request) def get_success_url(self): return reverse_lazy("users:profil", args=(self.object.pk,))