Skip to content

Commit a5a49d8

Browse files
authored
Merge pull request #77 from wesokes/hotfix/from-2.0.0
bulk create fix
2 parents f0cf74d + 0574c8e commit a5a49d8

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed

entity_emailer/models.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,20 @@ def create_emails(self, email_params_list):
4747

4848
# Build list of recipient through relationships to create
4949
recipients_to_create = []
50+
51+
# Keep track of unique pairs to avoid unique constraint on through relationship
52+
email_entity_pairs = set()
5053
for i, recipient_entities in enumerate(recipient_entities_per_email):
5154
for recipient_entity in recipient_entities:
52-
recipients_to_create.append(
53-
Email.recipients.through(
54-
email_id=emails[i].id,
55-
entity_id=recipient_entity.id,
55+
if (emails[i].id, recipient_entity.id) not in email_entity_pairs:
56+
email_entity_pairs.add((emails[i].id, recipient_entity.id))
57+
58+
recipients_to_create.append(
59+
Email.recipients.through(
60+
email_id=emails[i].id,
61+
entity_id=recipient_entity.id,
62+
)
5663
)
57-
)
5864

5965
# Bulk create the recipient relationships
6066
Email.recipients.through.objects.bulk_create(recipients_to_create)

entity_emailer/tests/interface_tests.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,15 @@ def test_multiple_events_only_following_false(self):
296296

297297
@freeze_time('2013-1-2')
298298
def test_bulk_multiple_events_only_following_false(self):
299+
"""
300+
Handles bulk creating events and tests the unique constraint of the duplicated subscription which would cause
301+
a bulk create error if it wasn't handled
302+
"""
299303
source = G(Source)
300304
e = G(Entity)
301305
other_e = G(Entity)
302306

307+
G(Subscription, entity=e, source=source, medium=self.email_medium, only_following=False)
303308
G(Subscription, entity=e, source=source, medium=self.email_medium, only_following=False)
304309
G(Subscription, entity=other_e, source=source, medium=self.email_medium, only_following=False)
305310
email_context = {
@@ -318,6 +323,35 @@ def test_bulk_multiple_events_only_following_false(self):
318323
self.assertEquals(email.subject, '')
319324
self.assertEquals(email.scheduled, datetime(2013, 1, 2))
320325

326+
@freeze_time('2013-1-2')
327+
def test_bulk_multiple_events_only_following_true(self):
328+
"""
329+
Handles bulk creating events and tests the unique constraint of the duplicated subscription which would cause
330+
a bulk create error if it wasn't handled
331+
"""
332+
source = G(Source)
333+
e = G(Entity)
334+
other_e = G(Entity)
335+
336+
G(Subscription, entity=e, source=source, medium=self.email_medium, only_following=True)
337+
G(Subscription, entity=e, source=source, medium=self.email_medium, only_following=True)
338+
G(Subscription, entity=other_e, source=source, medium=self.email_medium, only_following=True)
339+
email_context = {
340+
'entity_emailer_template': 'template',
341+
'entity_emailer_subject': 'hi',
342+
}
343+
G(Event, source=source, context=email_context)
344+
event = G(Event, source=source, context=email_context)
345+
G(EventActor, event=event, entity=e)
346+
347+
EntityEmailerInterface.bulk_convert_events_to_emails()
348+
349+
email = Email.objects.get()
350+
self.assertEquals(set(email.recipients.all()), set([e]))
351+
self.assertEquals(email.event.context, email_context)
352+
self.assertEquals(email.subject, '')
353+
self.assertEquals(email.scheduled, datetime(2013, 1, 2))
354+
321355
@freeze_time('2013-1-2')
322356
def test_multiple_events_only_following_true(self):
323357
source = G(Source)

entity_emailer/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '2.0.0'
1+
__version__ = '2.0.0.1'

release_notes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Release Notes
22
=============
33

4+
v2.0.0.1
5+
------
6+
* Fix unique constraint when bulk creating emails
7+
48
v2.0.0
59
------
610
* Added bulk interface for converting to emails

0 commit comments

Comments
 (0)