Merge branch 'aureplop/revente-reply_to' into 'master'

bda -- Set winner's email as Reply-to of the email…

Closes #179

See merge request !276
This commit is contained in:
Martin Pepin 2018-01-07 16:03:20 +01:00
commit 771b642a98
4 changed files with 141 additions and 20 deletions

View file

@ -6,12 +6,15 @@ from datetime import timedelta
from custommail.shortcuts import send_mass_custom_mail from custommail.shortcuts import send_mass_custom_mail
from django.contrib.sites.models import Site from django.contrib.sites.models import Site
from django.core import mail
from django.db import models from django.db import models
from django.db.models import Count from django.db.models import Count
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.conf import settings from django.conf import settings
from django.utils import timezone, formats from django.utils import timezone, formats
from custommail.models import CustomMail
def get_generic_user(): def get_generic_user():
generic, _ = User.objects.get_or_create( generic, _ = User.objects.get_or_create(
@ -328,37 +331,55 @@ class SpectacleRevente(models.Model):
# Envoie un mail au gagnant et au vendeur # Envoie un mail au gagnant et au vendeur
winner = random.choice(inscrits) winner = random.choice(inscrits)
self.soldTo = winner self.soldTo = winner
datatuple = []
mails = []
context = { context = {
'acheteur': winner.user, 'acheteur': winner.user,
'vendeur': seller.user, 'vendeur': seller.user,
'show': spectacle, 'show': spectacle,
} }
datatuple.append((
'bda-revente-winner', c_mails_qs = CustomMail.objects.filter(shortname__in=[
context, 'bda-revente-winner', 'bda-revente-loser',
settings.MAIL_DATA['revente']['FROM'],
[winner.user.email],
))
datatuple.append((
'bda-revente-seller', 'bda-revente-seller',
context, ])
settings.MAIL_DATA['revente']['FROM'],
[seller.user.email] c_mails = {cm.shortname: cm for cm in c_mails_qs}
))
mails.append(
c_mails['bda-revente-winner'].get_message(
context,
from_email=settings.MAIL_DATA['revente']['FROM'],
to=[winner.user.email],
)
)
mails.append(
c_mails['bda-revente-seller'].get_message(
context,
from_email=settings.MAIL_DATA['revente']['FROM'],
to=[seller.user.email],
reply_to=[winner.user.email],
)
)
# Envoie un mail aux perdants # Envoie un mail aux perdants
for inscrit in inscrits: for inscrit in inscrits:
if inscrit != winner: if inscrit != winner:
new_context = dict(context) new_context = dict(context)
new_context['acheteur'] = inscrit.user new_context['acheteur'] = inscrit.user
datatuple.append((
'bda-revente-loser', mails.append(
new_context, c_mails['bda-revente-loser'].get_message(
settings.MAIL_DATA['revente']['FROM'], new_context,
[inscrit.user.email] from_email=settings.MAIL_DATA['revente']['FROM'],
)) to=[inscrit.user.email],
send_mass_custom_mail(datatuple) )
)
mail_conn = mail.get_connection()
mail_conn.send_messages(mails)
# Si personne ne veut de la place, elle part au shotgun # Si personne ne veut de la place, elle part au shotgun
else: else:
self.shotgun = True self.shotgun = True

0
bda/tests/__init__.py Normal file
View file

100
bda/tests/test_models.py Normal file
View file

@ -0,0 +1,100 @@
from datetime import timedelta
from unittest import mock
from django.contrib.auth import get_user_model
from django.core import mail
from django.test import TestCase
from django.utils import timezone
from bda.models import (
Attribution, Participant, Salle, Spectacle, SpectacleRevente, Tirage,
)
User = get_user_model()
class SpectacleReventeTests(TestCase):
fixtures = ['gestioncof/management/data/custommail.json']
def setUp(self):
now = timezone.now()
self.t = Tirage.objects.create(
title='Tirage',
ouverture=now - timedelta(days=7),
fermeture=now - timedelta(days=3),
active=True,
)
self.s = Spectacle.objects.create(
title='Spectacle',
date=now + timedelta(days=20),
location=Salle.objects.create(name='Salle', address='Address'),
price=10.5,
slots=5,
tirage=self.t,
listing=False,
)
self.seller = Participant.objects.create(
user=User.objects.create(
username='seller', email='seller@mail.net'),
tirage=self.t,
)
self.p1 = Participant.objects.create(
user=User.objects.create(username='part1', email='part1@mail.net'),
tirage=self.t,
)
self.p2 = Participant.objects.create(
user=User.objects.create(username='part2', email='part2@mail.net'),
tirage=self.t,
)
self.p3 = Participant.objects.create(
user=User.objects.create(username='part3', email='part3@mail.net'),
tirage=self.t,
)
self.attr = Attribution.objects.create(
participant=self.seller,
spectacle=self.s,
)
self.rev = SpectacleRevente.objects.create(
attribution=self.attr,
seller=self.seller,
)
def test_tirage(self):
revente = self.rev
wanted_by = [self.p1, self.p2, self.p3]
revente.answered_mail = wanted_by
with mock.patch('bda.models.random.choice') as mc:
# Set winner to self.p1.
mc.return_value = self.p1
revente.tirage()
# Call to random.choice used participants in wanted_by.
mc_args, _ = mc.call_args
self.assertEqual(set(mc_args[0]), set(wanted_by))
self.assertEqual(revente.soldTo, self.p1)
self.assertTrue(revente.tirage_done)
mails = {m.to[0]: m for m in mail.outbox}
self.assertEqual(len(mails), 4)
m_seller = mails['seller@mail.net']
self.assertListEqual(m_seller.to, ['seller@mail.net'])
self.assertListEqual(m_seller.reply_to, ['part1@mail.net'])
m_winner = mails['part1@mail.net']
self.assertListEqual(m_winner.to, ['part1@mail.net'])
self.assertCountEqual(
[mails['part2@mail.net'].to, mails['part3@mail.net'].to],
[['part2@mail.net'], ['part3@mail.net']],
)

View file

@ -4,7 +4,7 @@ from django.contrib.auth.models import User
from django.test import TestCase, Client from django.test import TestCase, Client
from django.utils import timezone from django.utils import timezone
from .models import Tirage, Spectacle, Salle, CategorieSpectacle from bda.models import Tirage, Spectacle, Salle, CategorieSpectacle
class TestBdAViews(TestCase): class TestBdAViews(TestCase):