Remove custommail in bda

This commit is contained in:
Ludovic Stephan 2020-09-15 11:49:05 +02:00
parent dc070278f7
commit f364928004
4 changed files with 93 additions and 76 deletions

View file

@ -1,10 +1,11 @@
from datetime import timedelta from datetime import timedelta
from custommail.shortcuts import send_mass_custom_mail
from dal.autocomplete import ModelSelect2 from dal.autocomplete import ModelSelect2
from django import forms from django import forms
from django.contrib import admin from django.contrib import admin
from django.core.mail import send_mass_mail
from django.db.models import Count, Q, Sum from django.db.models import Count, Q, Sum
from django.template import loader
from django.template.defaultfilters import pluralize from django.template.defaultfilters import pluralize
from django.utils import timezone from django.utils import timezone
@ -169,19 +170,23 @@ class ParticipantAdmin(ReadOnlyMixin, admin.ModelAdmin):
form = ParticipantAdminForm form = ParticipantAdminForm
def send_attribs(self, request, queryset): def send_attribs(self, request, queryset):
datatuple = [] emails = []
for member in queryset.all(): for member in queryset.all():
subject = "Résultats du tirage au sort"
attribs = member.attributions.all() attribs = member.attributions.all()
context = {"member": member.user} context = {"member": member.user}
shortname = ""
template_name = ""
if len(attribs) == 0: if len(attribs) == 0:
shortname = "bda-attributions-decus" template_name = "bda/mails/attributions-decus.txt"
else: else:
shortname = "bda-attributions" template_name = "bda/mails/attributions.txt"
context["places"] = attribs context["places"] = attribs
print(context)
datatuple.append((shortname, context, "bda@ens.fr", [member.user.email])) message = loader.render_to_string(template_name, context)
send_mass_custom_mail(datatuple) emails.append((subject, message, "bda@ens.fr", [member.user.email]))
send_mass_mail(emails)
count = len(queryset.all()) count = len(queryset.all())
if count == 1: if count == 1:
message_bit = "1 membre a" message_bit = "1 membre a"

View file

@ -2,14 +2,14 @@ import calendar
import random import random
from datetime import timedelta from datetime import timedelta
from custommail.models import CustomMail
from custommail.shortcuts import send_mass_custom_mail
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.contrib.sites.models import Site from django.contrib.sites.models import Site
from django.core import mail from django.core import mail
from django.core.mail import EmailMessage, send_mass_mail
from django.db import models from django.db import models
from django.db.models import Count, Exists from django.db.models import Count, Exists
from django.template import loader
from django.utils import formats, timezone from django.utils import formats, timezone
@ -116,16 +116,19 @@ class Spectacle(models.Model):
bda_generic.nb_attr = 1 bda_generic.nb_attr = 1
members.append(bda_generic) members.append(bda_generic)
# On écrit un mail personnalisé à chaque participant # On écrit un mail personnalisé à chaque participant
datatuple = [ mails = [
( (
"bda-rappel", str(self),
{"member": member, "nb_attr": member.nb_attr, "show": self}, loader.render_to_string(
"bda/mails/rappel.txt",
context={"member": member, "nb_attr": member.nb_attr, "show": self},
),
settings.MAIL_DATA["rappels"]["FROM"], settings.MAIL_DATA["rappels"]["FROM"],
[member.email], [member.email],
) )
for member in members for member in members
] ]
send_mass_custom_mail(datatuple) send_mass_mail(mails)
# On enregistre le fait que l'envoi a bien eu lieu # On enregistre le fait que l'envoi a bien eu lieu
self.rappel_sent = timezone.now() self.rappel_sent = timezone.now()
self.save() self.save()
@ -348,21 +351,24 @@ class SpectacleRevente(models.Model):
BdA-Revente à tous les intéressés. BdA-Revente à tous les intéressés.
""" """
inscrits = self.attribution.spectacle.subscribed.select_related("user") inscrits = self.attribution.spectacle.subscribed.select_related("user")
datatuple = [ mails = [
( (
"bda-revente", "BdA-Revente : {}".format(self.attribution.spectacle.title),
{ loader.render_to_string(
"member": participant.user, "bda/mails/revente-new.txt",
"show": self.attribution.spectacle, context={
"revente": self, "member": participant.user,
"site": Site.objects.get_current(), "show": self.attribution.spectacle,
}, "revente": self,
"site": Site.objects.get_current(),
},
),
settings.MAIL_DATA["revente"]["FROM"], settings.MAIL_DATA["revente"]["FROM"],
[participant.user.email], [participant.user.email],
) )
for participant in inscrits for participant in inscrits
] ]
send_mass_custom_mail(datatuple) send_mass_mail(mails)
self.notif_sent = True self.notif_sent = True
self.notif_time = timezone.now() self.notif_time = timezone.now()
self.save() self.save()
@ -373,20 +379,23 @@ class SpectacleRevente(models.Model):
leur indiquer qu'il est désormais disponible au shotgun. leur indiquer qu'il est désormais disponible au shotgun.
""" """
inscrits = self.attribution.spectacle.subscribed.select_related("user") inscrits = self.attribution.spectacle.subscribed.select_related("user")
datatuple = [ mails = [
( (
"bda-shotgun", "BdA-Revente : {}".format(self.attribution.spectacle.title),
{ loader.render_to_string(
"member": participant.user, "bda/mails/revente-shotgun.txt",
"show": self.attribution.spectacle, context={
"site": Site.objects.get_current(), "member": participant.user,
}, "show": self.attribution.spectacle,
"site": Site.objects.get_current(),
},
),
settings.MAIL_DATA["revente"]["FROM"], settings.MAIL_DATA["revente"]["FROM"],
[participant.user.email], [participant.user.email],
) )
for participant in inscrits for participant in inscrits
] ]
send_mass_custom_mail(datatuple) send_mass_mail(mails)
self.notif_sent = True self.notif_sent = True
self.notif_time = timezone.now() self.notif_time = timezone.now()
# Flag inutile, sauf si l'horloge interne merde # Flag inutile, sauf si l'horloge interne merde
@ -418,31 +427,30 @@ class SpectacleRevente(models.Model):
"show": spectacle, "show": spectacle,
} }
c_mails_qs = CustomMail.objects.filter( subject = "BdA-Revente : {}".format(spectacle.title)
shortname__in=[
"bda-revente-winner",
"bda-revente-loser",
"bda-revente-seller",
]
)
c_mails = {cm.shortname: cm for cm in c_mails_qs}
mails.append( mails.append(
c_mails["bda-revente-winner"].get_message( EmailMessage(
context, subject=subject,
body=loader.render_to_string(
"bda/mails/revente-tirage-winner.txt",
context=context,
),
from_email=settings.MAIL_DATA["revente"]["FROM"], from_email=settings.MAIL_DATA["revente"]["FROM"],
to=[winner.user.email], to=[winner.user.email],
) )
) )
mails.append( mails.append(
c_mails["bda-revente-seller"].get_message( EmailMessage(
context, subject=subject,
body=loader.render_to_string(
"bda/mails/revente-tirage-seller.txt",
context=context,
),
from_email=settings.MAIL_DATA["revente"]["FROM"], from_email=settings.MAIL_DATA["revente"]["FROM"],
to=[seller.user.email], to=[seller.user.email],
reply_to=[winner.user.email], reply_to=[winner.user.email],
) ),
) )
# Envoie un mail aux perdants # Envoie un mail aux perdants
@ -452,11 +460,15 @@ class SpectacleRevente(models.Model):
new_context["acheteur"] = inscrit.user new_context["acheteur"] = inscrit.user
mails.append( mails.append(
c_mails["bda-revente-loser"].get_message( EmailMessage(
new_context, subject=subject,
body=loader.render_to_string(
"bda/mails/revente-tirage-loser.txt",
context=new_context,
),
from_email=settings.MAIL_DATA["revente"]["FROM"], from_email=settings.MAIL_DATA["revente"]["FROM"],
to=[inscrit.user.email], to=[inscrit.user.email],
) ),
) )
mail_conn = mail.get_connection() mail_conn = mail.get_connection()

View file

@ -26,13 +26,6 @@
<hr \> <hr \>
<p>
<em>Note :</em> le template de ce mail peut être modifié à
<a href="{% url 'admin:custommail_custommail_change' custommail.pk %}">cette adresse</a>
</p>
<hr \>
<h3>Forme des mails</h3> <h3>Forme des mails</h3>
<h4>Une seule place</h4> <h4>Une seule place</h4>

View file

@ -4,17 +4,17 @@ import random
import time import time
from collections import defaultdict from collections import defaultdict
from custommail.models import CustomMail
from custommail.shortcuts import send_custom_mail, send_mass_custom_mail
from django.conf import settings from django.conf import settings
from django.contrib import messages from django.contrib import messages
from django.core import serializers from django.core import serializers
from django.core.exceptions import NON_FIELD_ERRORS from django.core.exceptions import NON_FIELD_ERRORS
from django.core.mail import send_mail, send_mass_mail
from django.db import transaction from django.db import transaction
from django.db.models import Count, Prefetch from django.db.models import Count, Prefetch
from django.forms.models import inlineformset_factory from django.forms.models import inlineformset_factory
from django.http import HttpResponseBadRequest, HttpResponseRedirect, JsonResponse from django.http import HttpResponseBadRequest, HttpResponseRedirect, JsonResponse
from django.shortcuts import get_object_or_404, render from django.shortcuts import get_object_or_404, render
from django.template import loader
from django.template.defaultfilters import pluralize from django.template.defaultfilters import pluralize
from django.urls import reverse from django.urls import reverse
from django.utils import formats, timezone from django.utils import formats, timezone
@ -400,7 +400,7 @@ def revente_manage(request, tirage_id):
if "resell" in request.POST: if "resell" in request.POST:
resellform = ResellForm(participant, request.POST, prefix="resell") resellform = ResellForm(participant, request.POST, prefix="resell")
if resellform.is_valid(): if resellform.is_valid():
datatuple = [] mails = []
attributions = resellform.cleaned_data["attributions"] attributions = resellform.cleaned_data["attributions"]
with transaction.atomic(): with transaction.atomic():
for attribution in attributions: for attribution in attributions:
@ -415,16 +415,17 @@ def revente_manage(request, tirage_id):
"show": attribution.spectacle, "show": attribution.spectacle,
"revente": revente, "revente": revente,
} }
datatuple.append( mails.append(
( (
"bda-revente-new", "BdA-Revente : {}".format(attribution.spectacle),
context, loader.render_to_string(
"bda/mails/revente-seller.txt", context=context
),
settings.MAIL_DATA["revente"]["FROM"], settings.MAIL_DATA["revente"]["FROM"],
[participant.user.email], [participant.user.email],
) )
) )
revente.save() send_mass_mail(mails)
send_mass_custom_mail(datatuple)
# On annule une revente # On annule une revente
elif "annul" in request.POST: elif "annul" in request.POST:
annulform = AnnulForm(participant, request.POST, prefix="annul") annulform = AnnulForm(participant, request.POST, prefix="annul")
@ -643,12 +644,16 @@ def revente_buy(request, spectacle_id):
"acheteur": request.user, "acheteur": request.user,
"vendeur": revente.seller.user, "vendeur": revente.seller.user,
} }
send_custom_mail(
"bda-buy-shotgun", send_mail(
"bda@ens.fr", "BdA-Revente : {}".format(spectacle.title),
loader.render_to_string(
"bda/mails/revente-shotgun-seller.txt", context=context
),
request.user.email,
[revente.seller.user.email], [revente.seller.user.email],
context=context,
) )
return render( return render(
request, request,
"bda/revente/mail-success.html", "bda/revente/mail-success.html",
@ -751,19 +756,21 @@ class UnpaidParticipants(BuroRequiredMixin, ListView):
def send_rappel(request, spectacle_id): def send_rappel(request, spectacle_id):
show = get_object_or_404(Spectacle, id=spectacle_id) show = get_object_or_404(Spectacle, id=spectacle_id)
# Mails d'exemples # Mails d'exemples
custommail = CustomMail.objects.get(shortname="bda-rappel") subject = show.title
exemple_mail_1place = custommail.render( body_mail_1place = loader.render_to_string(
{"member": request.user, "show": show, "nb_attr": 1} "bda/mails/rappel.txt",
context={"member": request.user, "show": show, "nb_attr": 1},
) )
exemple_mail_2places = custommail.render( body_mail_2places = loader.render_to_string(
{"member": request.user, "show": show, "nb_attr": 2} "bda/mails/rappel.txt",
context={"member": request.user, "show": show, "nb_attr": 2},
) )
# Contexte # Contexte
ctxt = { ctxt = {
"show": show, "show": show,
"exemple_mail_1place": exemple_mail_1place, "exemple_mail_1place": (subject, body_mail_1place),
"exemple_mail_2places": exemple_mail_2places, "exemple_mail_2places": (subject, body_mail_2places),
"custommail": custommail,
} }
# Envoi confirmé # Envoi confirmé
if request.method == "POST": if request.method == "POST":