Commits

Luke Plant committed 6b9f0b1

Don't spam the booking account holder with multiple emails when places are confirmed together

  • Participants
  • Parent commits fdc03bb
  • Branches bookings

Comments (0)

Files changed (6)

File cciw/bookings/email.py

     mail.send_mail(subject, body, settings.SERVER_EMAIL, [settings.WEBMASTER_EMAIL])
 
 
-def send_place_confirmed_email(booking, **kwargs):
-    account = booking.account
-    emails = list(set([e for e in [account.email, booking.email] if e.strip() != '']))
-    if not emails:
+def send_places_confirmed_email(bookings, **kwargs):
+    if not bookings:
+        return
+    account = bookings[0].account
+    if account.email == '':
         return
 
     c = {
         'url_start': site_address_url_start(),
-        'booking': booking,
-        'camp': booking.camp,
+        'account': account,
+        'bookings': bookings,
         'payment_received': 'payment_received' in kwargs,
         }
     body = loader.render_to_string('cciw/bookings/place_confirmed_email.txt', c)
     subject = "CCIW booking - place confirmed"
-    mail.send_mail(subject, body, settings.SERVER_EMAIL, emails)
+    mail.send_mail(subject, body, settings.SERVER_EMAIL, [account.email])

File cciw/bookings/hooks.py

 
 from paypal.standard.ipn.signals import payment_was_successful
 
-from .signals import place_confirmed
-from .email import send_unrecognised_payment_email, send_place_confirmed_email
+from .signals import places_confirmed
+from .email import send_unrecognised_payment_email, send_places_confirmed_email
 from .models import BookingAccount
 
 #### Handlers #####
 
 ### Place confirmation ###
 
-def place_confirmed_handler(sender, **kwargs):
-    booking = sender
-    send_place_confirmed_email(booking, **kwargs)
+def places_confirmed_handler(sender, **kwargs):
+    bookings = sender
+    send_places_confirmed_email(bookings, **kwargs)
 
 
 #### Wiring ####
 
 payment_was_successful.connect(paypal_payment_received)
-place_confirmed.connect(place_confirmed_handler)
+places_confirmed.connect(places_confirmed_handler)

File cciw/bookings/models.py

 from cciw.cciwmain.models import Camp
 from cciw.cciwmain.utils import Lock
 
-from .signals import place_confirmed
+from .signals import places_confirmed
 
 # = Business rules =
 #
         candidate_bookings = list(self.bookings.unconfirmed()
                                   .order_by('booking_expires'))
         i = 0
+        confirmed_bookings = []
         while pot > 0 and i < len(candidate_bookings):
             b = candidate_bookings[i]
             if b.amount_due <= pot:
                 b.confirm()
                 b.save()
-                place_confirmed.send(b, payment_received=True)
+                confirmed_bookings.append(b)
                 pot -= b.amount_due
             i += 1
+        if confirmed_bookings:
+            places_confirmed.send(confirmed_bookings, payment_received=True)
+
 
 
 class BookingManager(models.Manager):

File cciw/bookings/signals.py

 from django.dispatch import Signal
 
-place_confirmed = Signal()
+places_confirmed = Signal()

File cciw/bookings/tests.py

         self.assertEqual(mail.outbox[0].to, [self.email])
         self.assertTrue("Thank you for your payment" in mail.outbox[0].body)
 
+    def test_only_one_email_for_multiple_places(self):
+        self.login()
+        self.create_place()
+        self.create_place({'name': 'Another Child'})
+
+        acc = BookingAccount.objects.get(email=self.email)
+        book_basket_now(acc.bookings.basket(self.camp.year))
+
+        mail.outbox = []
+        acc.receive_payment(acc.get_balance())
+
+        self.assertEqual(len(mail.outbox), 1)
+
+        self.assertEqual(mail.outbox[0].subject, "CCIW booking - place confirmed")
+        self.assertEqual(mail.outbox[0].to, [self.email])
+        self.assertTrue(self.place_details['name'] in mail.outbox[0].body)
+        self.assertTrue('Another Child' in mail.outbox[0].body)
+
+
 class TestAjaxViews(CreatePlaceMixin, TestCase):
     # Basic tests to ensure that the views that serve AJAX return something
     # sensible

File templates/cciw/bookings/place_confirmed_email.txt

 {% load url from future %}{% autoescape off %}
-{% if payment_received %}Thank you for your payment{% endif %}
-The following place on a CCIW camp has been confirmed:
+Dear {{ account.name }},
 
-Camper name: {{ booking.name }}
-Camp: {{ camp }}
-Dates: {{ camp.start_date|date:"d M Y" }} to {{ camp.end_date|date:"d M" }}
+{% if payment_received %}Thank you for your payment.
+{% endif %}
+{% if bookings|length == 1 %}The following place on a CCIW camp has been confirmed:
+{% else %}The following places on a CCIW camp have been confirmed:
+{%endif %}
+{% for b in bookings %}
+Name: {{ b.name }}
+Camp: {{ b.camp }}
+Dates: {{ b.camp.start_date|date:"d F Y" }} to {{ b.camp.end_date|date:"d F" }}
+{% endfor %}
 
 The camp leaders will be in contact with more details in due time.