2020-10-11 15:12:54 +00:00
|
|
|
from corres2math.lists import get_sympa_client
|
2020-09-22 17:37:37 +00:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
|
2020-10-31 12:41:16 +00:00
|
|
|
from corres2math.matrix import Matrix
|
2020-09-24 09:44:43 +00:00
|
|
|
from .models import AdminRegistration, Registration
|
2020-09-22 10:27:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
def set_username(instance, **_):
|
2020-10-30 18:46:46 +00:00
|
|
|
"""
|
|
|
|
Ensure that the user username is always equal to the user email address.
|
|
|
|
"""
|
2020-09-22 10:27:03 +00:00
|
|
|
instance.username = instance.email
|
|
|
|
|
|
|
|
|
2020-09-22 17:37:37 +00:00
|
|
|
def send_email_link(instance, **_):
|
2020-10-30 18:46:46 +00:00
|
|
|
"""
|
|
|
|
If the email address got changed, send a new validation link
|
|
|
|
and update the registration status in the team mailing list.
|
|
|
|
"""
|
2020-09-22 17:37:37 +00:00
|
|
|
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()
|
|
|
|
|
2020-10-11 15:10:59 +00:00
|
|
|
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,
|
2020-10-11 14:57:11 +00:00
|
|
|
f"{instance.first_name} {instance.last_name}")
|
|
|
|
|
2020-09-22 17:37:37 +00:00
|
|
|
|
2020-09-22 10:27:03 +00:00
|
|
|
def create_admin_registration(instance, **_):
|
2020-10-30 18:46:46 +00:00
|
|
|
"""
|
|
|
|
When a super user got created through console,
|
|
|
|
ensure that an admin registration is created.
|
|
|
|
"""
|
2020-09-22 10:27:03 +00:00
|
|
|
if instance.is_superuser:
|
|
|
|
AdminRegistration.objects.get_or_create(user=instance)
|
2020-10-31 12:41:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
def invite_to_public_rooms(instance: Registration, **_):
|
|
|
|
"""
|
|
|
|
When a user got registered, automatically invite the Matrix user into public rooms.
|
|
|
|
"""
|
|
|
|
if not instance.pk:
|
|
|
|
Matrix.invite("#annonces:correspondances-maths.fr", f"@{instance.matrix_username}:correspondances-maths.fr")
|
|
|
|
Matrix.invite("#faq:correspondances-maths.fr", f"@{instance.matrix_username}:correspondances-maths.fr")
|