From 98d04b9093b75024d5c3e07e7b01ef1079f52855 Mon Sep 17 00:00:00 2001 From: Emmy D'Anello Date: Sun, 18 Feb 2024 23:02:27 +0100 Subject: [PATCH] Make the payment group button work Signed-off-by: Emmy D'Anello --- .../templates/registration/payment_form.html | 8 +-- registration/urls.py | 3 + registration/views.py | 55 ++++++++++++++++++- 3 files changed, 59 insertions(+), 7 deletions(-) diff --git a/registration/templates/registration/payment_form.html b/registration/templates/registration/payment_form.html index 0cab8f3..6272c2a 100644 --- a/registration/templates/registration/payment_form.html +++ b/registration/templates/registration/payment_form.html @@ -28,9 +28,9 @@ You want finally that each member pays its own registration? Then click on the button: {% endblocktrans %} {% else %} {% blocktrans trimmed %} @@ -38,9 +38,9 @@ or your school will pay for all registrations? Then click on the button: {% endblocktrans %} {% endif %}

diff --git a/registration/urls.py b/registration/urls.py index caaf9e1..68ceeb4 100644 --- a/registration/urls.py +++ b/registration/urls.py @@ -5,6 +5,7 @@ from django.urls import path from .views import AddOrganizerView, AdultPhotoAuthorizationTemplateView, ChildPhotoAuthorizationTemplateView, \ InstructionsTemplateView, MyAccountDetailView, ParentalAuthorizationTemplateView, PaymentUpdateView, \ + PaymentUpdateGroupView, \ ResetAdminView, SignupView, UserDetailView, UserImpersonateView, UserListView, UserResendValidationEmailView, \ UserUpdateView, UserUploadHealthSheetView, UserUploadParentalAuthorizationView, UserUploadPhotoAuthorizationView, \ UserUploadVaccineSheetView, UserValidateView, UserValidationEmailSentView @@ -37,6 +38,8 @@ urlpatterns = [ path("user//upload-parental-authorization/", UserUploadParentalAuthorizationView.as_view(), name="upload_user_parental_authorization"), path("update-payment//", PaymentUpdateView.as_view(), name="update_payment"), + path("update-payment//toggle-group-mode/", PaymentUpdateGroupView.as_view(), + name="update_payment_group_mode"), path("user//impersonate/", UserImpersonateView.as_view(), name="user_impersonate"), path("user/list/", UserListView.as_view(), name="user_list"), path("reset-admin/", ResetAdminView.as_view(), name="reset_admin"), diff --git a/registration/views.py b/registration/views.py index a759384..5ea96df 100644 --- a/registration/views.py +++ b/registration/views.py @@ -460,9 +460,10 @@ class PaymentUpdateView(LoginRequiredMixin, UpdateView): data=self.request.POST or None, instance=self.object) - context['scholarship_form'] = PaymentForm(payment_type='scholarship', - data=self.request.POST or None, - instance=self.object) + if not self.object.grouped: + context['scholarship_form'] = PaymentForm(payment_type='scholarship', + data=self.request.POST or None, + instance=self.object) context['other_form'] = PaymentForm(payment_type='other', data=self.request.POST or None, @@ -481,6 +482,54 @@ class PaymentUpdateView(LoginRequiredMixin, UpdateView): return reverse_lazy("registration:user_detail", args=(self.object.registrations.first().user.pk,)) +class PaymentUpdateGroupView(LoginRequiredMixin, DetailView): + model = Payment + + def dispatch(self, request, *args, **kwargs): + if not self.request.user.is_authenticated or \ + not self.request.user.registration.is_admin \ + and (self.request.user.registration not in self.get_object().registrations.all() + or self.get_object().valid is not False): + return self.handle_no_permission() + return super().dispatch(request, *args, **kwargs) + + def get(self, request, *args, **kwargs): + payment = self.get_object() + + if payment.valid is not False: + raise PermissionDenied(_("This payment is already valid or pending validation.")) + + if payment.grouped: + registrations = list(payment.registrations.all()) + first_reg = registrations[0] + payment.registrations.set([first_reg]) + payment.grouped = False + tournament = first_reg.team.participation.tournament if not payment.final else Tournament.final_tournament() + payment.amount = tournament.price + payment.save() + for registration in registrations[1:]: + p = Payment.objects.create(type=payment.type, + grouped=False, + final=payment.final, + amount=tournament.price, + receipt=payment.receipt, + additional_information=payment.additional_information) + p.registrations.set([registration]) + p.save() + else: + reg = payment.registrations.get() + tournament = reg.team.participation.tournament if not payment.final else Tournament.final_tournament() + for student in reg.team.students.all(): + if student != reg: + Payment.objects.filter(registrations=student, final=payment.final).delete() + payment.registrations.add(student) + payment.amount = tournament.price * reg.team.students.count() + payment.grouped = True + payment.save() + + return redirect(reverse_lazy("registration:update_payment", args=(payment.pk,))) + + class PhotoAuthorizationView(LoginRequiredMixin, View): """ Display the sent photo authorization.