1
0
mirror of https://gitlab.com/animath/si/plateforme.git synced 2025-06-24 04:28:48 +02:00

Create payments in a signal rather than in a view

Signed-off-by: Emmy D'Anello <emmy.danello@animath.fr>
This commit is contained in:
Emmy D'Anello
2024-02-24 09:39:04 +01:00
parent 9380fbaaf7
commit 6e35bdc0b3
3 changed files with 43 additions and 20 deletions

View File

@ -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):