diff --git a/participation/apps.py b/participation/apps.py index 23bd437..4b0839a 100644 --- a/participation/apps.py +++ b/participation/apps.py @@ -12,8 +12,9 @@ class ParticipationConfig(AppConfig): name = 'participation' def ready(self): - from participation.signals import create_notes, create_team_participation, update_mailing_list - pre_save.connect(update_mailing_list, "participation.Team") - post_save.connect(create_team_participation, "participation.Team") - post_save.connect(create_notes, "participation.Passage") - post_save.connect(create_notes, "participation.Pool") + from participation import signals + pre_save.connect(signals.update_mailing_list, "participation.Team") + post_save.connect(signals.create_team_participation, "participation.Team") + post_save.connect(signals.create_payments, "participation.Participation") + post_save.connect(signals.create_notes, "participation.Passage") + post_save.connect(signals.create_notes, "participation.Pool") diff --git a/participation/signals.py b/participation/signals.py index 119ab16..5044e5c 100644 --- a/participation/signals.py +++ b/participation/signals.py @@ -2,7 +2,8 @@ # SPDX-License-Identifier: GPL-3.0-or-later from typing import Union -from participation.models import Note, Participation, Passage, Pool, Team +from participation.models import Note, Participation, Passage, Pool, Team, Tournament +from registration.models import Payment from tfjm.lists import get_sympa_client @@ -36,6 +37,41 @@ def update_mailing_list(instance: Team, raw, **_): f"{coach.user.first_name} {coach.user.last_name}") +def create_payments(instance: Participation, created, raw, **_): + """ + When a participation got created, create an associated payment. + """ + if instance.valid and not raw: + for student in instance.team.students.all(): + payment_qs = Payment.objects.filter(registrations=student, final=False) + if payment_qs.exists(): + payment = payment_qs.get() + else: + payment = Payment.objects.create() + payment.registrations.add(student) + payment.save() + payment.amount = instance.tournament.price + if payment.amount == 0: + payment.type = "free" + payment.valid = True + payment.save() + + if instance.final: + for student in instance.team.students.all(): + payment_qs = Payment.objects.filter(registrations=student, final=True) + if payment_qs.exists(): + payment = payment_qs.get() + else: + payment = Payment.objects.create(final=True) + payment.registrations.add(student) + payment.save() + payment.amount = Tournament.final_tournament().price + if payment.amount == 0: + payment.type = "free" + payment.valid = True + payment.save() + + def create_notes(instance: Union[Passage, Pool], raw, **_): if not raw: if isinstance(instance, Pool): diff --git a/participation/views.py b/participation/views.py index 832dc34..e39eb64 100644 --- a/participation/views.py +++ b/participation/views.py @@ -249,20 +249,6 @@ class TeamDetailView(LoginRequiredMixin, FormMixin, ProcessFormView, DetailView) mail_html = render_to_string("participation/mails/team_validated.html", mail_context) send_mail("[TFJM²] Équipe validée", mail_plain, None, [self.object.email], html_message=mail_html) - for student in self.object.students.all(): - payment_qs = Payment.objects.filter(registrations=student) - if payment_qs.exists(): - payment = payment_qs.get() - else: - payment = Payment.objects.create() - payment.registrations.add(student) - payment.save() - payment.amount = self.object.participation.tournament.price - if payment.amount == 0: - payment.type = "free" - payment.valid = True - payment.save() - elif "invalidate" in self.request.POST: self.object.participation.valid = None self.object.participation.save()