diff --git a/temba/channels/models.py b/temba/channels/models.py index f6a6c211a1f..44a677c8ad3 100644 --- a/temba/channels/models.py +++ b/temba/channels/models.py @@ -659,8 +659,8 @@ def release(self, user, *, trigger_sync: bool = True): for incident in self.incidents.filter(ended_on=None): incident.end() - # any template translations for this channel are marked inactive - self.template_translations.filter(is_active=True).update(is_active=False) + # delete template translations for this channel + self.template_translations.all().delete() def delete(self): for trigger in self.triggers.all(): diff --git a/temba/channels/tests.py b/temba/channels/tests.py index 4f3bd8e7738..3bf0dca38ca 100644 --- a/temba/channels/tests.py +++ b/temba/channels/tests.py @@ -234,7 +234,7 @@ def test_release(self, mr_mocks): self.assertNotIn(channel1, flow.channel_dependencies.all()) self.assertEqual(0, channel1.triggers.filter(is_active=True).count()) self.assertEqual(0, channel1.incidents.filter(ended_on=None).count()) - self.assertEqual(0, channel1.template_translations.filter(is_active=True).count()) + self.assertEqual(0, channel1.template_translations.count()) # check that we queued a task to interrupt sessions tied to this channel self.assertEqual( diff --git a/temba/orgs/tests.py b/temba/orgs/tests.py index 82a2567bc1f..db50c4a4c12 100644 --- a/temba/orgs/tests.py +++ b/temba/orgs/tests.py @@ -1307,18 +1307,16 @@ def _create_flow_content(self, org, user, channels, contacts, groups, add) -> tu add(WebHookEvent.objects.create(org=org, resthook=resthook, data={})) - template_trans1 = add( - TemplateTranslation.get_or_create( - channels[0], - "hello", - locale="eng-US", - status=TemplateTranslation.STATUS_APPROVED, - external_id="1234", - external_locale="en_US", - namespace="foo_namespace", - components=[{"name": "body", "type": "body/text", "content": "Hello", "variables": {}, "params": []}], - variables=[], - ) + template_trans1 = TemplateTranslation.get_or_create( + channels[0], + "hello", + locale="eng-US", + status=TemplateTranslation.STATUS_APPROVED, + external_id="1234", + external_locale="en_US", + namespace="foo_namespace", + components=[{"name": "body", "type": "body/text", "content": "Hello", "variables": {}, "params": []}], + variables=[], ) add(template_trans1.template) flow1.template_dependencies.add(template_trans1.template) diff --git a/temba/templates/models.py b/temba/templates/models.py index b134a6f5937..20f33601911 100644 --- a/temba/templates/models.py +++ b/temba/templates/models.py @@ -119,6 +119,7 @@ def update_local(cls, channel, raw_templates: list): """ Updates the local translations against the fetched raw templates from the given channel """ + refreshed = [] for raw_template in raw_templates: translation = channel.template_type.update_local(channel, raw_template) @@ -126,21 +127,11 @@ def update_local(cls, channel, raw_templates: list): if translation: refreshed.append(translation) - # trim any translations we didn't keep - cls.trim(channel, refreshed) - - @classmethod - def trim(cls, channel, existing): - """ - Trims what channel templates exist for this channel based on the set of templates passed in - """ - ids = [tc.id for tc in existing] - - # mark any that weren't included as inactive - cls.objects.filter(channel=channel).exclude(id__in=ids).update(is_active=False) + # delete any template translations we didn't see + channel.template_translations.exclude(id__in=[tt.id for tt in refreshed]).delete() - # Make sure the seen one are active - cls.objects.filter(channel=channel, id__in=ids, is_active=False).update(is_active=True) + # TODO remove this once inactive translations are all hard deleted + cls.objects.filter(channel=channel, id__in=[tc.id for tc in refreshed], is_active=False).update(is_active=True) @classmethod def get_or_create( diff --git a/temba/templates/tests.py b/temba/templates/tests.py index 4779eb72a65..d184e5da475 100644 --- a/temba/templates/tests.py +++ b/temba/templates/tests.py @@ -83,21 +83,6 @@ def test_model(self): self.assertEqual(3, TemplateTranslation.objects.filter(channel=channel1).count()) self.assertEqual(1, TemplateTranslation.objects.filter(channel=channel2).count()) - # trim them - TemplateTranslation.trim(channel1, [hello_eng, hello_fra]) - - # non-included translations should be inactive now - hello_eng.refresh_from_db() - self.assertTrue(hello_eng.is_active) - hello_fra.refresh_from_db() - self.assertTrue(hello_fra.is_active) - goodbye_fra.refresh_from_db() - self.assertFalse(goodbye_fra.is_active) - - # but not for other channels - goodbye_fra_other_channel.refresh_from_db() - self.assertTrue(hello_eng.is_active) - def test_update_local(self): channel = self.create_channel("WA", "Channel 1", "1234") @@ -145,8 +130,7 @@ def test_update_local(self): ) self.assertEqual({"hello", "goodbye"}, set(Template.objects.values_list("name", flat=True))) - self.assertEqual(1, TemplateTranslation.objects.filter(channel=channel, is_active=True).count()) - self.assertEqual(2, TemplateTranslation.objects.filter(channel=channel, is_active=False).count()) + self.assertEqual(1, channel.template_translations.count()) @patch("temba.templates.models.TemplateTranslation.update_local") @patch("temba.channels.types.twilio_whatsapp.TwilioWhatsappType.fetch_templates")