forked from DGNum/gestioCOF
Move bda.get_generic_user + optimize send_rappels
This commit is contained in:
parent
5c11893059
commit
69aee6b8ac
2 changed files with 21 additions and 24 deletions
|
@ -1,10 +1 @@
|
|||
from django.contrib.auth.models import User
|
||||
|
||||
|
||||
def get_generic_user():
|
||||
generic, created = User.objects.get_or_create(username="bda_generic")
|
||||
if created:
|
||||
generic.email = "bda@ens.fr"
|
||||
generic.first_name = "Bureau des Arts"
|
||||
generic.save()
|
||||
return generic
|
||||
|
|
|
@ -7,11 +7,18 @@ from custommail.shortcuts import send_mass_custom_mail
|
|||
|
||||
from django.contrib.sites.models import Site
|
||||
from django.db import models
|
||||
from django.db.models import Count
|
||||
from django.contrib.auth.models import User
|
||||
from django.conf import settings
|
||||
from django.utils import timezone, formats
|
||||
|
||||
from . import get_generic_user
|
||||
|
||||
def get_generic_user():
|
||||
generic, _ = User.objects.get_or_create(
|
||||
username="bda_generic",
|
||||
defaults={"email": "bda@ens.fr", "first_name": "Bureau des arts"}
|
||||
)
|
||||
return generic
|
||||
|
||||
|
||||
class Tirage(models.Model):
|
||||
|
@ -98,30 +105,29 @@ class Spectacle(models.Model):
|
|||
Envoie un mail de rappel à toutes les personnes qui ont une place pour
|
||||
ce spectacle.
|
||||
"""
|
||||
# On récupère la liste des participants
|
||||
members = {}
|
||||
for attr in Attribution.objects.filter(spectacle=self).all():
|
||||
member = attr.participant.user
|
||||
if member.id in members:
|
||||
members[member.id][1] = 2
|
||||
else:
|
||||
members[member.id] = [member, 1]
|
||||
# On écrit un mail personnalisé à chaque participant
|
||||
# On récupère la liste des participants + le BdA
|
||||
members = list(
|
||||
User.objects
|
||||
.filter(participant__attributions=self)
|
||||
.annotate(nb_attr=Count("id")).order_by()
|
||||
)
|
||||
bda_generic = get_generic_user()
|
||||
members[-1] = [bda_generic, 1]
|
||||
bda_generic.nb_attr = 1
|
||||
members.append(bda_generic)
|
||||
# On écrit un mail personnalisé à chaque participant
|
||||
datatuple = [(
|
||||
'bda-rappel',
|
||||
{'member': member[0], 'nb_attr': member[1], 'show': self},
|
||||
{'member': member, "nb_attr": member.nb_attr, 'show': self},
|
||||
settings.MAIL_DATA['rappels']['FROM'],
|
||||
[member[0].email])
|
||||
for member in members.values()
|
||||
[member.email])
|
||||
for member in members
|
||||
]
|
||||
send_mass_custom_mail(datatuple)
|
||||
# On enregistre le fait que l'envoi a bien eu lieu
|
||||
self.rappel_sent = timezone.now()
|
||||
self.save()
|
||||
# On renvoie la liste des destinataires
|
||||
return map(lambda t: t[0], members.values())
|
||||
return members
|
||||
|
||||
@property
|
||||
def is_past(self):
|
||||
|
|
Loading…
Reference in a new issue