1
0
mirror of https://gitlab.com/animath/si/plateforme.git synced 2024-11-26 14:07:08 +00:00
plateforme-tfjm2/participation/signals.py

84 lines
3.3 KiB
Python
Raw Normal View History

2020-12-27 10:49:54 +00:00
# Copyright (C) 2020 by Animath
# SPDX-License-Identifier: GPL-3.0-or-later
2021-01-14 17:21:22 +00:00
from typing import Union
2020-12-27 10:49:54 +00:00
from participation.models import Note, Participation, Passage, Pool, Team, Tournament
from registration.models import Payment
2020-12-28 18:19:01 +00:00
from tfjm.lists import get_sympa_client
2020-12-27 10:49:54 +00:00
def create_team_participation(instance, created, raw, **_):
2020-12-27 10:49:54 +00:00
"""
2021-01-12 14:42:32 +00:00
When a team got created, create an associated participation.
2020-12-27 10:49:54 +00:00
"""
if not raw:
participation = Participation.objects.get_or_create(team=instance)[0]
participation.save()
if not created:
participation.team.create_mailing_list()
2020-12-27 10:49:54 +00:00
def update_mailing_list(instance: Team, raw, **_):
2020-12-27 10:49:54 +00:00
"""
When a team name or trigram got updated, update mailing lists
2020-12-27 10:49:54 +00:00
"""
if instance.pk and not raw:
2020-12-27 10:49:54 +00:00
old_team = Team.objects.get(pk=instance.pk)
2021-01-16 21:29:10 +00:00
if old_team.trigram != instance.trigram:
2020-12-27 10:49:54 +00:00
# Delete old mailing list, create a new one
old_team.delete_mailing_list()
instance.create_mailing_list()
# Subscribe all team members in the mailing list
for student in instance.students.all():
get_sympa_client().subscribe(student.user.email, f"equipe-{instance.trigram.lower()}", False,
f"{student.user.first_name} {student.user.last_name}")
for coach in instance.coaches.all():
2020-12-27 10:49:54 +00:00
get_sympa_client().subscribe(coach.user.email, f"equipe-{instance.trigram.lower()}", False,
f"{coach.user.first_name} {coach.user.last_name}")
2021-01-14 17:21:22 +00:00
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):
for passage in instance.passages.all():
create_notes(passage, raw)
return
2021-01-14 17:21:22 +00:00
for jury in instance.pool.juries.all():
Note.objects.get_or_create(jury=jury, passage=instance)