Déplace la fonction d'envoi des mails

C'est désormais une méthode des spectacles et la vue `send_rappel`
permet de l'appeler.
This commit is contained in:
Martin Pépin 2016-06-10 23:53:29 +02:00
parent ab8afc000a
commit 8054e20cce
3 changed files with 56 additions and 58 deletions

View file

@ -1,58 +0,0 @@
# 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})

View file

@ -6,6 +6,14 @@ from django.db import models
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
from django.db.models.signals import post_save
from django.template import loader, Context
from django.core import mail
from django.conf import settings
def render_template(template_name, data):
tmpl = loader.get_template(template_name)
ctxt = Context(data)
return tmpl.render(ctxt)
class Tirage(models.Model):
title = models.CharField("Titre", max_length=300)
@ -55,6 +63,33 @@ class Spectacle(models.Model):
def __unicode__ (self):
return u"%s - %s, %s, %.02f" % (self.title, self.date_no_seconds(),
self.location, self.price)
def send_rappel(self):
# 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].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" % (self.title, self.date_no_seconds(),
self.location)
for member in members.values():
mail_body = render_template('mail-rappel.txt', {
'member': member,
'show': self})
mail_tot = mail.EmailMessage(mail_object, mail_body,
settings.RAPPEL_FROM, [member.email],
[], headers={'Reply-To': settings.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)
# On renvoie la liste des destinataires
return members.values()
PAYMENT_TYPES = (
("cash",u"Cash"),

View file

@ -337,3 +337,24 @@ def liste_spectacles_ics(request, tirage_id):
{"spectacles": spectacles, "tirage": tirage},
content_type="text/calendar")
@buro_required
def send_rappel(request, spectacle_id):
# Envoi des mails
show = get_object_or_404(Spectacle, id=spectacle_id)
show.send_rappel()
# 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})