Skip to content

Commit

Permalink
Merge pull request #5360 from nyaruka/hard_delete_translations
Browse files Browse the repository at this point in the history
Hard delete template translations that no longer exist on the channel side
  • Loading branch information
rowanseymour authored Jul 5, 2024
2 parents 3235a29 + cd0b5c6 commit d12d578
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 46 deletions.
4 changes: 2 additions & 2 deletions temba/channels/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
2 changes: 1 addition & 1 deletion temba/channels/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
22 changes: 10 additions & 12 deletions temba/orgs/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 5 additions & 14 deletions temba/templates/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,28 +119,19 @@ 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)

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(
Expand Down
18 changes: 1 addition & 17 deletions temba/templates/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit d12d578

Please sign in to comment.