From 5a021e95db1c0475fff3e594633baf9efaba1b48 Mon Sep 17 00:00:00 2001 From: Wes Okes Date: Thu, 11 Feb 2021 15:48:18 -0500 Subject: [PATCH 1/3] bulk create fix --- entity_emailer/models.py | 16 ++++++++---- entity_emailer/tests/interface_tests.py | 34 +++++++++++++++++++++++++ entity_emailer/version.py | 2 +- release_notes.rst | 5 ++++ 4 files changed, 51 insertions(+), 6 deletions(-) diff --git a/entity_emailer/models.py b/entity_emailer/models.py index c61a593..e46c4ba 100644 --- a/entity_emailer/models.py +++ b/entity_emailer/models.py @@ -47,14 +47,20 @@ def create_emails(self, email_params_list): # Build list of recipient through relationships to create recipients_to_create = [] + + # Keep track of unique pairs to avoid unique constraint on through relationship + email_entity_pairs = set() for i, recipient_entities in enumerate(recipient_entities_per_email): for recipient_entity in recipient_entities: - recipients_to_create.append( - Email.recipients.through( - email_id=emails[i].id, - entity_id=recipient_entity.id, + if (emails[i].id, recipient_entity.id) not in email_entity_pairs: + email_entity_pairs.add((emails[i].id, recipient_entity.id)) + + recipients_to_create.append( + Email.recipients.through( + email_id=emails[i].id, + entity_id=recipient_entity.id, + ) ) - ) # Bulk create the recipient relationships Email.recipients.through.objects.bulk_create(recipients_to_create) diff --git a/entity_emailer/tests/interface_tests.py b/entity_emailer/tests/interface_tests.py index 79a4ff2..08a93ca 100644 --- a/entity_emailer/tests/interface_tests.py +++ b/entity_emailer/tests/interface_tests.py @@ -296,10 +296,15 @@ def test_multiple_events_only_following_false(self): @freeze_time('2013-1-2') def test_bulk_multiple_events_only_following_false(self): + """ + Handles bulk creating events and tests the unique constraint of the duplicated subscription which would cause + a bulk create error if it wasn't handled + """ source = G(Source) e = G(Entity) other_e = G(Entity) + G(Subscription, entity=e, source=source, medium=self.email_medium, only_following=False) G(Subscription, entity=e, source=source, medium=self.email_medium, only_following=False) G(Subscription, entity=other_e, source=source, medium=self.email_medium, only_following=False) email_context = { @@ -318,6 +323,35 @@ def test_bulk_multiple_events_only_following_false(self): self.assertEquals(email.subject, '') self.assertEquals(email.scheduled, datetime(2013, 1, 2)) + @freeze_time('2013-1-2') + def test_bulk_multiple_events_only_following_true(self): + """ + Handles bulk creating events and tests the unique constraint of the duplicated subscription which would cause + a bulk create error if it wasn't handled + """ + source = G(Source) + e = G(Entity) + other_e = G(Entity) + + G(Subscription, entity=e, source=source, medium=self.email_medium, only_following=True) + G(Subscription, entity=e, source=source, medium=self.email_medium, only_following=True) + G(Subscription, entity=other_e, source=source, medium=self.email_medium, only_following=True) + email_context = { + 'entity_emailer_template': 'template', + 'entity_emailer_subject': 'hi', + } + G(Event, source=source, context=email_context) + event = G(Event, source=source, context=email_context) + G(EventActor, event=event, entity=e) + + EntityEmailerInterface.bulk_convert_events_to_emails() + + email = Email.objects.get() + self.assertEquals(set(email.recipients.all()), set([e])) + self.assertEquals(email.event.context, email_context) + self.assertEquals(email.subject, '') + self.assertEquals(email.scheduled, datetime(2013, 1, 2)) + @freeze_time('2013-1-2') def test_multiple_events_only_following_true(self): source = G(Source) diff --git a/entity_emailer/version.py b/entity_emailer/version.py index afced14..668c344 100644 --- a/entity_emailer/version.py +++ b/entity_emailer/version.py @@ -1 +1 @@ -__version__ = '2.0.0' +__version__ = '2.0.2' diff --git a/release_notes.rst b/release_notes.rst index 30d5525..1581a84 100644 --- a/release_notes.rst +++ b/release_notes.rst @@ -1,6 +1,11 @@ Release Notes ============= +v2.0.2 +------ +* Fix unique constraint when bulk creating emails +* This wipes 2.0.1, which needs to be added back later + v2.0.0 ------ * Added bulk interface for converting to emails From 18c04f0f68ac7ada628d1628aa095f5a52759f5a Mon Sep 17 00:00:00 2001 From: Wes Okes Date: Thu, 11 Feb 2021 16:00:40 -0500 Subject: [PATCH 2/3] vresion --- release_notes.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/release_notes.rst b/release_notes.rst index 1581a84..7937202 100644 --- a/release_notes.rst +++ b/release_notes.rst @@ -1,10 +1,9 @@ Release Notes ============= -v2.0.2 +v2.0.0.1 ------ * Fix unique constraint when bulk creating emails -* This wipes 2.0.1, which needs to be added back later v2.0.0 ------ From 0574c8e246a6941e48ec0b73d309f6b150d17c2e Mon Sep 17 00:00:00 2001 From: Wes Okes Date: Thu, 11 Feb 2021 16:01:41 -0500 Subject: [PATCH 3/3] version --- entity_emailer/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entity_emailer/version.py b/entity_emailer/version.py index 668c344..cdfc74d 100644 --- a/entity_emailer/version.py +++ b/entity_emailer/version.py @@ -1 +1 @@ -__version__ = '2.0.2' +__version__ = '2.0.0.1'