Fix the account dump command.

- a missing `fail_silently` flag made the whole command fail if an invalid recipient is used (like closed utbm mail address)
- Not specifying the seller make the account detail pages crash.
This commit is contained in:
imperosol 2024-12-04 18:18:30 +01:00
parent b3eb7693e3
commit 007e17fd8b
2 changed files with 15 additions and 5 deletions

View File

@ -55,7 +55,9 @@ class Command(BaseCommand):
customer__user__in=reactivated_users
).delete()
self._dump_accounts({u.customer for u in users_to_dump})
self._send_mails(users_to_dump)
self.stdout.write("Accounts dumped")
nb_successful_mails = self._send_mails(users_to_dump)
self.stdout.write(f"{nb_successful_mails} were successfuly sent.")
self.stdout.write("Finished !")
@staticmethod
@ -103,13 +105,14 @@ class Command(BaseCommand):
if len(pending_dumps) != len(customer_ids):
raise ValueError("One or more accounts were not engaged in a dump process")
counter = Counter.objects.get(pk=settings.SITH_COUNTER_ACCOUNT_DUMP_ID)
seller = User.objects.get(pk=settings.SITH_ROOT_USER_ID)
sales = Selling.objects.bulk_create(
[
Selling(
label="Vidange compte inactif",
club=counter.club,
counter=counter,
seller=None,
seller=seller,
product=None,
customer=account,
quantity=1,
@ -134,8 +137,12 @@ class Command(BaseCommand):
Customer.objects.filter(pk__in=customer_ids).update(amount=0)
@staticmethod
def _send_mails(users: Iterable[User]):
"""Send the mails informing users that their account has been dumped."""
def _send_mails(users: Iterable[User]) -> int:
"""Send the mails informing users that their account has been dumped.
Returns:
The number of emails successfully sent.
"""
mails = [
(
_("Your AE account has been emptied"),
@ -145,4 +152,4 @@ class Command(BaseCommand):
)
for user in users
]
send_mass_mail(mails)
return send_mass_mail(mails, fail_silently=True)

View File

@ -351,6 +351,9 @@ SITH_SEMESTER_START_SPRING = (2, 15) # 15 February
# Used to determine the valid promos
SITH_SCHOOL_START_YEAR = 1999
# id of the Root account
SITH_ROOT_USER_ID = 0
SITH_GROUP_ROOT_ID = 1
SITH_GROUP_PUBLIC_ID = 2
SITH_GROUP_SUBSCRIBERS_ID = 3