From db67598b25d0d87be468201687b352dc334364f1 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Tue, 14 Apr 2020 04:46:52 +0200 Subject: [PATCH] Validate WEI memberships --- apps/member/views.py | 4 ++- apps/wei/models.py | 22 +++++++++++- apps/wei/views.py | 50 +++++++++++++++++++++++++-- templates/wei/weimembership_form.html | 13 +++++++ 4 files changed, 85 insertions(+), 4 deletions(-) diff --git a/apps/member/views.py b/apps/member/views.py index 381314b2..34919597 100644 --- a/apps/member/views.py +++ b/apps/member/views.py @@ -544,6 +544,8 @@ class ClubAddMemberView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView): valid=True, ) + ret = super().form_valid(form) + # If Société générale pays, then we store the information: the bank can't pay twice to a same person. if soge: user.profile.soge = True @@ -573,7 +575,7 @@ class ClubAddMemberView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView): membership.roles.add(Role.objects.get(name="Adhérent Kfet")) membership.save() - return super().form_valid(form) + return ret def get_success_url(self): return reverse_lazy('member:club_detail', kwargs={'pk': self.object.club.id}) diff --git a/apps/wei/models.py b/apps/wei/models.py index 24a1667d..2a351cee 100644 --- a/apps/wei/models.py +++ b/apps/wei/models.py @@ -7,7 +7,7 @@ from django.contrib.auth.models import User from django.db import models from django.utils.translation import gettext_lazy as _ from member.models import Role, Club, Membership -from note.models import NoteSpecial +from note.models import NoteSpecial, MembershipTransaction class WEIClub(Club): @@ -268,3 +268,23 @@ class WEIMembership(Membership): class Meta: verbose_name = _("WEI membership") verbose_name_plural = _("WEI memberships") + + def make_transaction(self): + """ + Create Membership transaction associated to this membership. + """ + if not self.fee or MembershipTransaction.objects.filter(membership=self).exists(): + return + + if self.fee: + transaction = MembershipTransaction( + membership=self, + source=self.user.note, + destination=self.club.note, + quantity=1, + amount=self.fee, + reason="Adhésion WEI " + self.club.name, + valid=not self.registration.soge_credit # Soge transactions are by default invalidated + ) + transaction._force_save = True + transaction.save(force_insert=True) diff --git a/apps/wei/views.py b/apps/wei/views.py index d21b1680..d84b6d62 100644 --- a/apps/wei/views.py +++ b/apps/wei/views.py @@ -1,13 +1,14 @@ # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later -from datetime import datetime +from datetime import datetime, date from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.models import User from django.db.models import Q from django.urls import reverse_lazy from django.views.generic import DetailView, UpdateView, CreateView +from django.utils.translation import gettext_lazy as _ from django_tables2 import SingleTableView from member.models import Membership, Club from note.models import Transaction, NoteClub @@ -302,9 +303,54 @@ class WEIValidateRegistrationView(ProtectQuerysetMixin, LoginRequiredMixin, Crea context["club"] = registration.wei context["fee"] = registration.wei.membership_fee_paid if registration.user.profile.paid \ else registration.wei.membership_fee_unpaid + context["kfet_member"] = Membership.objects.filter( + club__name="Kfet", + user=registration.user, + date_start__lte=datetime.now().date(), + date_end__gte=datetime.now().date(), + ).exists() return context + def form_valid(self, form): + """ + Create membership, check that all is good, make transactions + """ + registration = WEIRegistration.objects.get(pk=self.kwargs["pk"]) + club = registration.wei + user = registration.user + + membership = form.instance + membership.user = user + membership.club = club + membership.date_start = min(date.today(), club.date_start) + membership.registration = registration + + if user.profile.paid: + fee = club.membership_fee_paid + else: + fee = club.membership_fee_unpaid + + if not registration.soge_credit and user.note.balance < fee: + # Users must have money before registering to the WEI. + # TODO Send a notification to the user (with a mail?) to tell her/him to credit her/his note + form.add_error('bus', + _("This user don't have enough money to join this club, and can't have a negative balance.")) + return super().form_invalid(form) + + if not registration.caution_check and True: # TODO: Replace it with "is 2A+" + form.add_error('bus', _("This user didn't give her/his caution check.")) + return super().form_invalid(form) + + if club.parent_club is not None: + if not Membership.objects.filter(user=form.instance.user, club=club.parent_club).exists(): + form.add_error('user', _('User is not a member of the parent club') + ' ' + club.parent_club.name) + return super().form_invalid(form) + + # Now, all is fine, the membership can be created. + + return super().form_valid(form) + def get_success_url(self): self.object.refresh_from_db() - return reverse_lazy("wei:wei_detail", kwargs={"pk": self.object.wei.pk}) + return reverse_lazy("wei:wei_detail", kwargs={"pk": self.object.club.pk}) diff --git a/templates/wei/weimembership_form.html b/templates/wei/weimembership_form.html index 1006bf78..c7add31f 100644 --- a/templates/wei/weimembership_form.html +++ b/templates/wei/weimembership_form.html @@ -121,6 +121,19 @@ {% endif %} + {% if not kfet_member %} +
+ {% url 'registration:future_user_detail' pk=registration.user.pk as future_user_detail %} + {% url 'member:club_detail' pk=club.parent_club.parent_club.pk as club_detail %} + {% blocktrans %} + This user is not a member of the Kfet club. Please adhere + here if he/she is in her/his first year + or here if he/she was an old member before you validate + the registration of the WEI. + {% endblocktrans %} +
+ {% endif %} +
{% csrf_token %} {{ form|crispy }}