2024-02-07 01:26:49 +00:00
|
|
|
# Copyright (C) 2018-2024 by BDE ENS Paris-Saclay
|
2019-07-19 13:00:44 +00:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2020-10-04 18:26:43 +00:00
|
|
|
from django.utils import timezone
|
|
|
|
|
2019-07-19 13:00:44 +00:00
|
|
|
|
2020-04-05 04:40:03 +00:00
|
|
|
def save_user_note(instance, raw, **_kwargs):
|
2019-07-19 13:00:44 +00:00
|
|
|
"""
|
|
|
|
Hook to create and save a note when an user is updated
|
|
|
|
"""
|
2020-09-07 12:52:37 +00:00
|
|
|
if not raw and (instance.is_superuser or instance.profile.registration_valid)\
|
|
|
|
and not hasattr(instance, "_no_signal"):
|
2020-04-05 04:40:03 +00:00
|
|
|
# Create note only when the registration is validated
|
|
|
|
from note.models import NoteUser
|
|
|
|
NoteUser.objects.get_or_create(user=instance)
|
|
|
|
instance.note.save()
|
2019-07-19 13:00:44 +00:00
|
|
|
|
|
|
|
|
2020-04-12 00:43:22 +00:00
|
|
|
def save_club_note(instance, raw, **_kwargs):
|
2019-07-19 13:00:44 +00:00
|
|
|
"""
|
|
|
|
Hook to create and save a note when a club is updated
|
|
|
|
"""
|
2020-09-07 12:52:37 +00:00
|
|
|
# When provisionning data, do not try to autocreate
|
|
|
|
if not raw and not hasattr(instance, "_no_signal"):
|
|
|
|
from .models import NoteClub
|
|
|
|
NoteClub.objects.get_or_create(club=instance)
|
|
|
|
instance.note.save()
|
2020-09-06 18:18:59 +00:00
|
|
|
|
|
|
|
|
2020-10-04 18:26:43 +00:00
|
|
|
def pre_save_note(instance, raw, **_kwargs):
|
|
|
|
if not raw and instance.pk and not hasattr(instance, "_no_signal") and instance.balance < 0:
|
|
|
|
from note.models import Note
|
|
|
|
old_note = Note.objects.get(pk=instance.pk)
|
|
|
|
if old_note.balance >= 0:
|
|
|
|
# Passage en négatif
|
|
|
|
instance.last_negative = timezone.now()
|
|
|
|
instance.send_mail_negative_balance()
|
|
|
|
|
|
|
|
|
2020-09-06 18:18:59 +00:00
|
|
|
def delete_transaction(instance, **_kwargs):
|
|
|
|
"""
|
|
|
|
Whenever we want to delete a transaction (caution with this), we ensure the transaction is invalid first.
|
|
|
|
"""
|
2020-09-07 12:52:37 +00:00
|
|
|
if not hasattr(instance, "_no_signal"):
|
|
|
|
instance.valid = False
|
2021-02-22 10:54:23 +00:00
|
|
|
instance._force_save = True
|
2020-09-07 12:52:37 +00:00
|
|
|
instance.save()
|