1
0
mirror of https://gitlab.crans.org/bde/nk20 synced 2025-02-24 00:51:20 +00:00

Compare commits

...

4 Commits

Author SHA1 Message Date
9f0a22d3d1
There is not always an error
Signed-off-by: Yohann D'ANELLO <ynerant@crans.org>
2021-06-14 22:15:35 +02:00
f60691846b
Don't block valid payments
Signed-off-by: Yohann D'ANELLO <ynerant@crans.org>
2021-06-14 21:54:32 +02:00
8cf9dfb9b9
Reduce complexity of the validation of a user, add verbosity in comments
Signed-off-by: Yohann D'ANELLO <ynerant@crans.org>
2021-06-14 21:43:04 +02:00
c3ab61bd04
Factorize detection of uncomplete payment forms
Signed-off-by: Yohann D'ANELLO <ynerant@crans.org>
2021-06-14 21:39:29 +02:00
3 changed files with 52 additions and 34 deletions

View File

@ -625,9 +625,6 @@ class ClubAddMemberView(ProtectQuerysetMixin, ProtectedCreateView):
# Retrieve form data # Retrieve form data
credit_type = form.cleaned_data["credit_type"] credit_type = form.cleaned_data["credit_type"]
credit_amount = form.cleaned_data["credit_amount"] credit_amount = form.cleaned_data["credit_amount"]
last_name = form.cleaned_data["last_name"]
first_name = form.cleaned_data["first_name"]
bank = form.cleaned_data["bank"]
soge = form.cleaned_data["soge"] and not user.profile.soge and (club.name == "BDE" or club.name == "Kfet") soge = form.cleaned_data["soge"] and not user.profile.soge and (club.name == "BDE" or club.name == "Kfet")
if not credit_type: if not credit_type:
@ -674,17 +671,9 @@ class ClubAddMemberView(ProtectQuerysetMixin, ProtectedCreateView):
.format(form.instance.club.membership_end)) .format(form.instance.club.membership_end))
error = True error = True
if credit_amount: if credit_amount and not SpecialTransaction.validate_payment_form(form):
if not last_name or not first_name or (not bank and credit_type.special_type == "Chèque"): # Check that special information for payment are filled
if not last_name: error = True
form.add_error('last_name', _("This field is required."))
error = True
if not first_name:
form.add_error('first_name', _("This field is required."))
error = True
if not bank and credit_type.special_type == "Chèque":
form.add_error('bank', _("This field is required."))
error = True
return not error return not error

View File

@ -333,6 +333,36 @@ class SpecialTransaction(Transaction):
self.clean() self.clean()
super().save(*args, **kwargs) super().save(*args, **kwargs)
@staticmethod
def validate_payment_form(form):
"""
Ensure that last name and first name are filled for a form that creates a SpecialTransaction,
and check that if the user pays with a check, then the bank field is filled.
Return True iff there is no error.
Whenever there is an error, they are inserted in the form errors.
"""
credit_type = form.cleaned_data["credit_type"]
last_name = form.cleaned_data["last_name"]
first_name = form.cleaned_data["first_name"]
bank = form.cleaned_data["bank"]
error = False
if not last_name or not first_name or (not bank and credit_type.special_type == "Chèque"):
if not last_name:
form.add_error('last_name', _("This field is required."))
error = True
if not first_name:
form.add_error('first_name', _("This field is required."))
error = True
if not bank and credit_type.special_type == "Chèque":
form.add_error('bank', _("This field is required."))
error = True
return not error
class Meta: class Meta:
verbose_name = _("Special transaction") verbose_name = _("Special transaction")
verbose_name_plural = _("Special transactions") verbose_name_plural = _("Special transactions")

View File

@ -248,9 +248,13 @@ class FutureUserDetailView(ProtectQuerysetMixin, LoginRequiredMixin, FormMixin,
@transaction.atomic @transaction.atomic
def form_valid(self, form): def form_valid(self, form):
"""
Finally validate the registration, with creating the membership.
"""
user = self.get_object() user = self.get_object()
if Alias.objects.filter(normalized_name=Alias.normalize(user.username)).exists(): if Alias.objects.filter(normalized_name=Alias.normalize(user.username)).exists():
# Don't try to hack an existing account.
form.add_error(None, _("An alias with a similar name already exists.")) form.add_error(None, _("An alias with a similar name already exists."))
return self.form_invalid(form) return self.form_invalid(form)
@ -265,31 +269,32 @@ class FutureUserDetailView(ProtectQuerysetMixin, LoginRequiredMixin, FormMixin,
join_kfet = form.cleaned_data["join_kfet"] join_kfet = form.cleaned_data["join_kfet"]
if soge: if soge:
# If Société Générale pays the inscription, the user joins the two clubs # If Société Générale pays the inscription, the user automatically joins the two clubs.
join_bde = True join_bde = True
join_kfet = True join_kfet = True
if not join_bde: if not join_bde:
# This software belongs to the BDE.
form.add_error('join_bde', _("You must join the BDE.")) form.add_error('join_bde', _("You must join the BDE."))
return super().form_invalid(form) return super().form_invalid(form)
# Calculate required registration fee
fee = 0 fee = 0
bde = Club.objects.get(name="BDE") bde = Club.objects.get(name="BDE")
bde_fee = bde.membership_fee_paid if user.profile.paid else bde.membership_fee_unpaid bde_fee = bde.membership_fee_paid if user.profile.paid else bde.membership_fee_unpaid
if join_bde: # This is mandatory.
fee += bde_fee fee += bde_fee if join_bde else 0
kfet = Club.objects.get(name="Kfet") kfet = Club.objects.get(name="Kfet")
kfet_fee = kfet.membership_fee_paid if user.profile.paid else kfet.membership_fee_unpaid kfet_fee = kfet.membership_fee_paid if user.profile.paid else kfet.membership_fee_unpaid
if join_kfet: # Add extra fee for the full membership
fee += kfet_fee fee += kfet_fee if join_kfet else 0
if soge: # If the bank pays, then we don't credit now. Treasurers will validate the transaction
# If the bank pays, then we don't credit now. Treasurers will validate the transaction # and credit the note later.
# and credit the note later. credit_type = None if soge else credit_type
credit_type = None
if credit_type is None: # If the user does not select any payment method, then no credit will be performed.
credit_amount = 0 credit_amount = 0 if credit_type is None else credit_amount
if fee > credit_amount and not soge: if fee > credit_amount and not soge:
# Check if the user credits enough money # Check if the user credits enough money
@ -298,15 +303,9 @@ class FutureUserDetailView(ProtectQuerysetMixin, LoginRequiredMixin, FormMixin,
.format(pretty_money(fee))) .format(pretty_money(fee)))
return self.form_invalid(form) return self.form_invalid(form)
if credit_type is not None and credit_amount > 0: # Check that payment information are filled, like last name and first name
if not last_name or not first_name or (not bank and credit_type.special_type == "Chèque"): if credit_type is not None and credit_amount > 0 and not SpecialTransaction.validate_payment_form(form):
if not last_name: return self.form_invalid(form)
form.add_error('last_name', _("This field is required."))
if not first_name:
form.add_error('first_name', _("This field is required."))
if not bank and credit_type.special_type == "Chèque":
form.add_error('bank', _("This field is required."))
return self.form_invalid(form)
# Save the user and finally validate the registration # Save the user and finally validate the registration
# Saving the user creates the associated note # Saving the user creates the associated note