From 356c8588e78586f0a1dc99a4412816888c6ff8fe Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Wed, 1 Apr 2020 17:00:06 +0200 Subject: [PATCH] Users that are not member of the club Kfet can't have a negative balance --- apps/member/models.py | 4 ++-- apps/member/views.py | 21 +++++++++++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/apps/member/models.py b/apps/member/models.py index c2f27473..0af52736 100644 --- a/apps/member/models.py +++ b/apps/member/models.py @@ -228,8 +228,8 @@ class Membership(models.Model): if Membership.objects.filter( user=self.user, club=self.club, - date_start__lte=datetime.datetime.now().date(), - date_end__gte=datetime.datetime.now().date(), + date_start__lte=self.date_start, + date_end__gte=self.date_start, ).exists(): raise ValidationError(_('User is already a member of the club')) diff --git a/apps/member/views.py b/apps/member/views.py index 243ff265..d3a1848f 100644 --- a/apps/member/views.py +++ b/apps/member/views.py @@ -358,8 +358,25 @@ class ClubAddMemberView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView): def form_valid(self, form): club = Club.objects.filter(PermissionBackend.filter_queryset(self.request.user, Club, "view"))\ .get(pk=self.kwargs["pk"]) + user = self.request.user form.instance.club = club + if user.profile.paid: + fee = club.membership_fee_paid + else: + fee = club.membership_fee_unpaid + if user.note.balance < fee and not Membership.objects.filter( + club=2, + user=user, + date_start__lte=datetime.now().date(), + date_end__gte=datetime.now().date(), + ).exists(): + # Users without a valid Kfet membership can't have a negative balance. + # Club 2 = Kfet (hard-code :'( ) + # TODO Send a notification to the user (with a mail?) to tell her/him to credit her/his note + form.add_error('user', + _("This user don't have enough money to join this club, and can't have a negative balance.")) + 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) @@ -368,8 +385,8 @@ class ClubAddMemberView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView): if Membership.objects.filter( user=form.instance.user, club=club, - date_start__lte=datetime.now().date(), - date_end__gte=datetime.now().date(), + date_start__lte=form.instance.date_start, + date_end__gte=form.instance.date_start, ).exists(): form.add_error('user', _('User is already a member of the club')) return super().form_invalid(form)