gestioCOF/bda/mails.py
2016-06-10 23:33:52 +02:00

58 lines
2 KiB
Python

# coding: utf-8
from django.shortcuts import get_object_or_404, render
from django.core import mail
from django.template import loader, Context
from gestioncof.decorators import buro_required
from bda.models import Spectacle, Attribution
def render_template(template_name, data):
tmpl = loader.get_template(template_name)
ctxt = Context(data)
return tmpl.render(ctxt)
@buro_required
def send_rappel(request, spectacle_id):
# On récupère la liste des participants
show = get_object_or_404(Spectacle, id=spectacle_id)
members = {}
for attr in Attribution.objects.filter(spectacle=show).all():
member = attr.participant.user
if member.id in members:
members[member.id].nb_attr = 2
else:
member.nb_attr = 1
members[member.id] = member
# On écrit un mail personnalisé à chaque participant
mails_to_send = []
mail_object = "%s - %s - %s" % (show.title, show.date_no_seconds(),
show.location)
for member in members.values():
mail_body = render_template('mail-rappel.txt', {
'member': member,
'show': show})
mail_tot = mail.EmailMessage(mail_object, mail_body,
RAPPEL_FROM, [member.email],
[], headers={'Reply-To': RAPPEL_REPLY_TO})
mails_to_send.append(mail_tot)
# On envoie les mails
connection = mail.get_connection(fail_silently=True)
connection.send_messages(mails_to_send)
# Mails d'exemples
fake_member = request.user
fake_member.nb_attr = 1
example1 = render_template('mail-rappel.txt', {
'member': fake_member,
'show': show})
fake_member.nb_attr = 2
example2 = render_template('mail-rappel.txt', {
'member': fake_member,
'show': show})
return render(request, "mails-rappel-sent.html", {
'members': members.values(),
'show': show,
'example1': example1,
'example2': example2})