1
0
mirror of https://gitlab.com/animath/si/plateforme.git synced 2024-11-30 03:33:03 +00:00
plateforme-tfjm2/registration/signals.py

57 lines
2.1 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
2020-12-28 18:19:01 +00:00
from django.contrib.auth.models import User
2020-12-27 10:49:54 +00:00
from tfjm.lists import get_sympa_client
from .models import Payment, Registration, VolunteerRegistration
2020-12-27 10:49:54 +00:00
def set_username(instance, **_):
"""
Ensure that the user username is always equal to the user email address.
"""
instance.username = instance.email
def send_email_link(instance, **_):
"""
If the email address got changed, send a new validation link
and update the registration status in the team mailing list.
"""
if instance.pk:
old_instance = User.objects.get(pk=instance.pk)
if old_instance.email != instance.email:
registration = Registration.objects.get(user=instance)
registration.email_confirmed = False
registration.save()
registration.user = instance
registration.send_email_validation_link()
if registration.participates and registration.team:
get_sympa_client().unsubscribe(old_instance.email, f"equipe-{registration.team.trigram.lower()}", False)
get_sympa_client().subscribe(instance.email, f"equipe-{registration.team.trigram.lower()}", False,
f"{instance.first_name} {instance.last_name}")
def create_admin_registration(instance, **_):
"""
When a super user got created through console,
ensure that an admin registration is created.
"""
if instance.is_superuser:
VolunteerRegistration.objects.get_or_create(user=instance, admin=True)
2020-12-27 10:49:54 +00:00
def create_payment(instance: Registration, raw, **_):
2020-12-27 10:49:54 +00:00
"""
2021-01-18 17:13:58 +00:00
When a user is saved, create the associated payment.
For a free tournament, the payment is valid.
2020-12-27 10:49:54 +00:00
"""
if instance.participates and not raw:
2021-01-18 17:13:58 +00:00
payment = Payment.objects.get_or_create(registration=instance)[0]
if instance.team and instance.team.participation.valid and instance.team.participation.tournament.price == 0:
payment.valid = True
payment.type = "free"
payment.save()